annotate mod_block_registrations/mod_block_registrations.lua @ 6513:5fb466693e85

mod_storage_xmlarchive: Ensure list index files are removed using os.remove() on the list files leaves the new index files behind since util.datamanager does not know they are removed. Thanks Link Mauve
author Kim Alvefur <zash@zash.se>
date Tue, 07 Apr 2026 21:10:54 +0200
parents a1df76ba4f68
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local nodeprep = require "util.encodings".stringprep.nodeprep;
6404
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
2 local it = require "util.iterators";
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
6322
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
4 local normal = nodeprep;
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
5 if module:get_option_boolean("block_registrations_confusable", true) then
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
6 local confusable = require "util.encodings".confusable;
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
7 if not confusable then
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
8 module:log("error", "Prosody not built with ICU, confusables mapping is unavailable.");
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
9 module:log("error", "Rebuild or disable this feature with 'block_registrations_confusable = false'");
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
10 end
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
11 function normal(username)
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
12 if username then
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
13 username = nodeprep(username);
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
14 end
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
15 if username then
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
16 username = confusable.skeleton(username);
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
17 end
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
18 return username;
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
19 end
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
20 end
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
21
5065
368bf9b06484 mod_block_registrations: Expand default list of blocked usernames (RFC 2142)
Matthew Wild <mwild1@gmail.com>
parents: 1697
diff changeset
22 local block_users = module:get_option_set("block_registrations_users", {
368bf9b06484 mod_block_registrations: Expand default list of blocked usernames (RFC 2142)
Matthew Wild <mwild1@gmail.com>
parents: 1697
diff changeset
23 "abuse", "admin", "administrator", "hostmaster", "info", "news",
368bf9b06484 mod_block_registrations: Expand default list of blocked usernames (RFC 2142)
Matthew Wild <mwild1@gmail.com>
parents: 1697
diff changeset
24 "noc", "operator", "owner", "postmaster", "register", "registration",
368bf9b06484 mod_block_registrations: Expand default list of blocked usernames (RFC 2142)
Matthew Wild <mwild1@gmail.com>
parents: 1697
diff changeset
25 "root", "security", "service", "signup", "support", "sysadmin",
368bf9b06484 mod_block_registrations: Expand default list of blocked usernames (RFC 2142)
Matthew Wild <mwild1@gmail.com>
parents: 1697
diff changeset
26 "sysop", "system", "test", "trouble", "webmaster", "www", "xmpp",
6322
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
27 }) / normal;
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local block_patterns = module:get_option_set("block_registrations_matching", {});
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local require_pattern = module:get_option_string("block_registrations_require");
6404
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
30 local length_min = module:get_option_number("block_registrations_length_minimum", 1);
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
31 local length_max = module:get_option_number("block_registrations_length_maximum", 255);
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 function is_blocked(username)
6321
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
34 -- Check if the username is simply blocked
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
35 if block_users:contains(username) then return true; end
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
6404
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
37 -- Check how long the username is (Unicode characters)
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
38 local length = it.count(username:gmatch("[\000-\127\194-\253][\128-\191]*"));
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
39 if length < length_min or length > length_max then return true; end
a1df76ba4f68 mod_block_registrations: Add username length requirements
Kim Alvefur <zash@zash.se>
parents: 6322
diff changeset
40
6322
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
41 local normalized_username = normal(username);
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
42 if block_users:contains(normalized_username) then return true; end
f110bce35630 mod_block_registrations: Support blocking confusable usernames (UTS 39)
Kim Alvefur <zash@zash.se>
parents: 6321
diff changeset
43
6321
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
44 for pattern in block_patterns do
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
45 if username:find(pattern) then
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
46 return true;
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
47 end
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
48 end
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
49 -- Not blocked, but check that username matches allowed pattern
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
50 if require_pattern and not username:match(require_pattern) then
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
51 return true;
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
52 end
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
1331
dbaa67babeb4 mod_block_registrations: Switch to using the 'user-registering' event
Kim Alvefur <zash@zash.se>
parents: 1053
diff changeset
55 module:hook("user-registering", function(event)
6321
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
56 local username = event.username;
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
57 if is_blocked(username) then
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
58 event.allowed = false;
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
59 return true;
2f01497b04c9 mod_block_registrations: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5065
diff changeset
60 end
1053
cabbcc1997d9 mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end, 10);