Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix(prosody): limit room claim patterns to one ".*" (#18168)

bgrozev3692aa537ca5
diff --git a/resources/prosody-plugins/token/room_pattern.lib.lua b/resources/prosody-plugins/token/room_pattern.lib.lua
new file mode 100644
index 0000000000..352afa6886
--- /dev/null
+++ b/resources/prosody-plugins/token/room_pattern.lib.lua
@@ -0,0 +1,47 @@
+-- Room claim patterns
+-- Copyright (C) 2026-present 8x8, Inc.
+--
+-- A token can flag its room claim as a pattern (context.room.regex). This
+-- library matches such a claim against a room name. It has no dependencies,
+-- so that busted can load it with dofile (tests/prosody/lua).
+
+-- matches any character that is special in a Lua pattern
+local LUA_PATTERN_SPECIAL_CHARS = '[%^%$%(%)%%%.%[%]%*%+%-%?]';
+
+--- Matches a room name against a room claim pattern.
+-- The only special sequence in the pattern is one '.*', which matches any run
+-- of characters, including an empty one. Every other character matches
+-- literally, and the pattern must match the whole room name. Unlike a Lua
+-- pattern, this does not backtrack, so the time it takes is linear in the
+-- length of the pattern and the room name.
+-- @param pattern the room claim
+-- @param room the room name
+-- @return true or false, or nil and a reason when the pattern is not supported:
+-- it is not a string, it has more than one '.*', or it has another Lua
+-- pattern special character
+local function match(pattern, room)
+ if type(pattern) ~= 'string' then
+ return nil, 'not a string';
+ end
+
+ local wildcard_start, wildcard_end = string.find(pattern, '.*', 1, true);
+ local prefix = wildcard_start and pattern:sub(1, wildcard_start - 1) or pattern;
+ local suffix = wildcard_start and pattern:sub(wildcard_end + 1) or '';
+
+ -- this also catches a second '.*'
+ if string.find(prefix, LUA_PATTERN_SPECIAL_CHARS) or string.find(suffix, LUA_PATTERN_SPECIAL_CHARS) then
+ return nil, 'more than one .* or another special character';
+ end
+
+ if not wildcard_start then
+ return room == prefix;
+ end
+
+ return #room >= #prefix + #suffix
+ and room:sub(1, #prefix) == prefix
+ and room:sub(#room - #suffix + 1) == suffix;
+end
+
+return {
+ match = match;
+};
diff --git a/resources/prosody-plugins/token/util.lib.lua b/resources/prosody-plugins/token/util.lib.lua
index 83678c61cd..1e0b22039c 100644
--- a/resources/prosody-plugins/token/util.lib.lua
+++ b/resources/prosody-plugins/token/util.lib.lua
@@ -62,39 +62,8 @@ local function is_regex_room_claim(room_context)
return room_context['regex'] == true or room_context['regex'] == 'true';
end
---- Counts the quantifiers ('*', '+', '-' and '?') in a Lua pattern.
--- A quantifier is only special outside a character class, so the contents of
--- a class ('[a-z]') are skipped, and so is the character after a '%' escape.
--- @param pattern the Lua pattern to inspect
--- @return the number of quantifiers in the pattern
-local function count_pattern_quantifiers(pattern)
- local count = 0;
- local index = 1;
- local length = #pattern;
- local in_class = false;
-
- while index <= length do
- local char = pattern:sub(index, index);
-
- if char == '%' then
- -- '%' escapes the next character, so skip both
- index = index + 2;
- else
- if in_class then
- if char == ']' then
- in_class = false;
- end
- elseif char == '[' then
- in_class = true;
- elseif char == '*' or char == '+' or char == '-' or char == '?' then
- count = count + 1;
- end
- index = index + 1;
- end
- end
-
- return count;
-end
+-- matches a room name against a room claim that is flagged as a pattern
+local match_room_pattern = module:require "token/room_pattern".match;
local Util = {}
Util.__index = Util
@@ -154,16 +123,16 @@ function Util.new(module)
self.enableDomainVerification = module:get_option_boolean('enable_domain_verification', true);
--[[
- A room claim that is flagged with context.room.regex is used as a Lua
- pattern. Lua patterns backtrack, so the cost of a match grows as
- (room name length) ^ (number of quantifiers). Both are limited to keep
- the cost of one match bounded, because a match blocks Prosody.
- Measured worst case with the defaults below is about 70ms; lowering
- the quantifier limit to 2 brings that down to about 2ms.
+ A room claim that is flagged with context.room.regex is a pattern with
+ at most one '.*' (see token/room_pattern.lib.lua). It is not a Lua
+ pattern, so a match does not backtrack and its cost is linear. The
+ length limit is not needed for the match time. It stays as a bound on
+ the work per join and on the size of the claim in the log: a room name
+ longer than the limit is not matched, and a pattern longer than the
+ limit plus the two characters of '.*' cannot match and is refused.
--]]
self.regexEnabled = module:get_option_boolean('token_regex_enabled', true);
self.regexRoomMaxLength = module:get_option_number('token_regex_room_max_length', 128);
- self.regexRoomMaxQuantifiers = module:get_option_number('token_regex_max_quantifiers', 3);
if self.allowEmptyToken == true then
module:log("warn", "WARNING - empty tokens allowed");
@@ -510,12 +479,9 @@ function Util:verify_room(session, room_address)
local auth_room = session.jitsi_meet_room;
if auth_room then
if type(auth_room) == 'string' then
- -- Room names are lowercase. Lowercase a literal room claim so that
- -- a token with uppercase letters still matches. Keep a pattern as
- -- written: pattern classes are case-sensitive (%d vs %D).
- if not auth_room_is_pattern then
- auth_room = string.lower(auth_room);
- end
+ -- Room names are lowercase. Lowercase the room claim so that a
+ -- token with uppercase letters still matches.
+ auth_room = string.lower(auth_room);
else
module:log('warn', 'session.jitsi_meet_room not string: %s', serialize(auth_room));
end
@@ -567,18 +533,25 @@ function Util:verify_room(session, room_address)
return false, 'invalid-regex', 'Room name is too long to match against the room claim';
end
- local quantifiers = count_pattern_quantifiers(auth_room);
- if quantifiers > self.regexRoomMaxQuantifiers then
- module:log('warn', 'Room claim has %s quantifiers, more than the limit of %s: %s',
- quantifiers, self.regexRoomMaxQuantifiers, auth_room);
- return false, 'invalid-regex', 'Room claim pattern is too complex';
+ if type(auth_room) == 'string' and #auth_room > self.regexRoomMaxLength + 2 then
+ module:log('warn', 'Room claim pattern is longer than %s, refusing it: %s...',
+ self.regexRoomMaxLength + 2, auth_room:sub(1, 32));
+ return false, 'invalid-regex', 'Room claim pattern is too long';
+ end
+
+ local matches, reason = match_room_pattern(auth_room, match_target);
+ if matches == nil then
+ module:log('warn', 'Room claim pattern is not supported (%s): %s', reason, tostring(auth_room));
+ return false, 'invalid-regex', 'Room claim pattern is not supported';
end
- local ok, result = pcall(string.match, match_target, auth_room);
- if not ok then
- return false, 'invalid-regex', 'Room claim is not a valid Lua pattern';
+ if not matches then
+ -- The claim is present and does not match. This is not a
+ -- missing claim, so it is refused also when the room claim is
+ -- optional (asap_require_room_claim = false).
+ return false, 'room-name-does-not-match', 'Room name does not match the room claim pattern.';
end
- room_to_check = result;
+ room_to_check = match_target;
else
-- not a regex
room_to_check = auth_room;
diff --git a/tests/prosody/docker/cfg/components.cfg.lua b/tests/prosody/docker/cfg/components.cfg.lua
index 300b73cf05..3927e25fab 100644
--- a/tests/prosody/docker/cfg/components.cfg.lua
+++ b/tests/prosody/docker/cfg/components.cfg.lua
@@ -134,3 +134,13 @@ Component "conference.noregex.test" "muc"
}
muc_mapper_domain_base = "noregex.test"
muc_mapper_domain_prefix = "conference"
+
+-- MUC component paired with VirtualHost "noclaim.test", where the room claim
+-- is optional (asap_require_room_claim = false).
+Component "conference.noclaim.test" "muc"
+ storage = "memory"
+ modules_enabled = {
+ "token_verification";
+ }
+ muc_mapper_domain_base = "noclaim.test"
+ muc_mapper_domain_prefix = "conference"
diff --git a/tests/prosody/docker/cfg/virtualhosts.cfg.lua b/tests/prosody/docker/cfg/virtualhosts.cfg.lua
index 6b74549452..726a7bed5f 100644
--- a/tests/prosody/docker/cfg/virtualhosts.cfg.lua
+++ b/tests/prosody/docker/cfg/virtualhosts.cfg.lua
@@ -151,6 +151,19 @@ VirtualHost "noregex.test"
token_regex_enabled = false
muc_mapper_domain_base = "noregex.test"
+-- VirtualHost with the room claim optional (asap_require_room_claim), as on
+-- meet.jit.si. A token without a room claim connects and joins any room. A
+-- room claim that is present must still match, also when it is a pattern.
+-- Same layout and .test suffix as noregex.test above.
+VirtualHost "noclaim.test"
+ authentication = "token"
+ app_id = "jitsi"
+ asap_key_server = "http://localhost:5280/test-observer/asap-keys"
+ signature_algorithm = "RS256"
+ allow_empty_token = true
+ asap_require_room_claim = false
+ muc_mapper_domain_base = "noclaim.test"
+
-- VirtualHost used by mod_auth_jitsi-anonymous tests.
VirtualHost "jitsi-anonymous.localhost"
authentication = "jitsi-anonymous"
diff --git a/tests/prosody/lua/token_room_pattern_spec.lua b/tests/prosody/lua/token_room_pattern_spec.lua
new file mode 100644
index 0000000000..d6a826dfd4
--- /dev/null
+++ b/tests/prosody/lua/token_room_pattern_spec.lua
@@ -0,0 +1,130 @@
+-- Unit tests for token/room_pattern.lib.lua
+-- Run with busted from resources/prosody-plugins/:
+-- busted ../../tests/prosody/lua/
+--
+-- The library has no Prosody dependencies. dofile resolves relative to CWD,
+-- so run from resources/prosody-plugins/.
+--
+-- The integration tests in mod_auth_token_asap_spec.js cover the use of the
+-- matcher in verify_room(). These tests cover the matcher itself: which
+-- patterns it accepts, what they match, and that its time stays linear.
+
+local M = dofile("token/room_pattern.lib.lua")
+local match = M.match
+
+describe("token/room_pattern", function()
+
+ describe("accepted patterns", function()
+ -- { pattern, room, expected }
+ local cases = {
+ -- one '.*' at the end: a prefix match
+ { "team_42.*", "team_42", true },
+ { "team_42.*", "team_42_standup", true },
+ { "team_42.*", "team_420", true },
+ { "team_42.*", "myteam_42", false },
+ { "team_42.*", "team_4", false },
+
+ -- one '.*' at the start: a suffix match
+ { ".*_standup", "team_42_standup", true },
+ { ".*_standup", "team_42_standup_2", false },
+
+ -- one '.*' in the middle: a prefix and a suffix
+ { "team_.*_standup", "team_42_standup", true },
+ { "team_.*_standup", "team__standup", true },
+ { "team_.*_standup", "team_standup", false },
+ { "ab.*b", "ab", false },
+ { "ab.*b", "abb", true },
+
+ -- only '.*': any room
+ { ".*", "", true },
+ { ".*", "anything", true },
+
+ -- no '.*': a literal match
+ { "abc", "abc", true },
+ { "abc", "abcd", false },
+ { "abc", "ab", false },
+ { "", "", true },
+ { "", "x", false },
+
+ -- bytes are compared as they are
+ { "a\0b", "a\0b", true },
+ { "a.*b", "a\0\0b", true },
+ { "caf\195\169.*", "caf\195\169x", true },
+ { "CAF.*", "cafx", false },
+ }
+
+ for _, c in ipairs(cases) do
+ local pattern, room, expected = c[1], c[2], c[3]
+ it(string.format("%q against %q is %s", pattern, room, tostring(expected)), function()
+ local result, reason = match(pattern, room)
+ assert.is_nil(reason)
+ assert.equal(expected, result)
+ end)
+ end
+ end)
+
+ describe("refused patterns", function()
+ local patterns = {
+ "a.*.*", "a.*b.*", ".*.*", "..*", ".**", "*.*",
+ "a*", "*", "a.b", "a%d", "a-b", "(x", "x)", "[a]", "a+", "a?", "^a", "a$",
+ }
+
+ for _, pattern in ipairs(patterns) do
+ it(string.format("%q is refused", pattern), function()
+ local result, reason = match(pattern, "a")
+ assert.is_nil(result)
+ assert.equal("more than one .* or another special character", reason)
+ end)
+ end
+
+ it("refuses a pattern that is not a string", function()
+ for _, pattern in ipairs({ 123, true, {} }) do
+ local result, reason = match(pattern, "a")
+ assert.is_nil(result)
+ assert.equal("not a string", reason)
+ end
+ local result, reason = match(nil, "a")
+ assert.is_nil(result)
+ assert.equal("not a string", reason)
+ end)
+ end)
+
+ describe("time", function()
+ -- A Lua pattern such as 'a*a*a*a*%d' over a long run of 'a's takes
+ -- seconds. The matcher must stay linear, so one megabyte on each side
+ -- must take well under one second.
+ local big = string.rep("a", 1000000)
+
+ local function under_one_second(f)
+ local start = os.clock()
+ local result = f()
+ local elapsed = os.clock() - start
+ assert.is_true(elapsed < 1, string.format("took %.2fs", elapsed))
+ return result
+ end
+
+ it("matches a 1MB room against a 1MB prefix and suffix", function()
+ assert.is_true(under_one_second(function()
+ return match(big .. ".*" .. big, big .. "zzz" .. big)
+ end))
+ end)
+
+ it("matches a 1MB literal", function()
+ assert.is_true(under_one_second(function()
+ return match(big, big)
+ end))
+ end)
+
+ it("refuses 1MB of special characters", function()
+ assert.is_nil(under_one_second(function()
+ return match(string.rep("%", 1000000), big)
+ end))
+ end)
+
+ it("refuses 1MB of dots", function()
+ assert.is_nil(under_one_second(function()
+ return match(string.rep(".", 1000000), big)
+ end))
+ end)
+ end)
+end)
diff --git a/tests/prosody/mod_auth_token_asap_spec.js b/tests/prosody/mod_auth_token_asap_spec.js
index 21d8390091..a703a15c3b 100644
--- a/tests/prosody/mod_auth_token_asap_spec.js
+++ b/tests/prosody/mod_auth_token_asap_spec.js
@@ -183,170 +183,184 @@ describe('mod_auth_token (ASAP / RS256)', () => {
assert.ok(c.jid, 'client should have a JID');
});
- // ── Room regex claim: invalid Lua pattern ─────────────────────────────────
+ // ── Room regex claim: supported syntax ────────────────────────────────────
//
- // When context.room.regex == true, util.lib.lua uses the JWT room claim as
- // a Lua pattern via string:match() inside verify_room() at MUC join time.
- // An invalid Lua pattern (e.g. unbalanced parenthesis) must be rejected
- // cleanly rather than crashing Prosody with a Lua error.
-
- it('does not crash on connect with invalid Lua regex room claim', async () => {
- // process_and_verify_token only stores the claim; it does not evaluate
- // the pattern. The crash path is verify_room(), called at MUC join.
+ // When context.room.regex == true, verify_room() matches the JWT room claim
+ // against the room name at MUC join time. The only special sequence is one
+ // '.*', which matches any run of characters. Every other character must
+ // match literally, and the claim must match the whole room name. A claim
+ // with more than one '.*', or with any other Lua pattern character, is
+ // refused with <invalid-regex/>, which distinguishes it from a claim that
+ // was evaluated and did not match (<room-name-does-not-match/>).
+
+ /**
+ * Joins roomJid with a token whose room claim is the given pattern.
+ *
+ * @param {string} roomJid - The room to join.
+ * @param {string|number|undefined} pattern - The room claim, flagged as a pattern.
+ * @param {object} [opts]
+ * @param {string} [opts.domain] - The VirtualHost to connect to (default: localhost).
+ * @param {string} [opts.sub] - The sub claim (default: '*').
+ * @returns {Promise<object>} The presence the join returns.
+ */
+ async function joinWithPattern(roomJid, pattern, { domain, sub } = {}) {
const token = mintAsapToken({
- room: '(invalid',
- context: { room: { regex: true } }
+ room: pattern,
+ context: { room: { regex: true } },
+ ...sub === undefined ? {} : { sub }
});
- const c = await asapClient({ token });
- clients.push(c);
- assert.ok(c.jid, 'connection must succeed: pattern not evaluated at auth time');
- });
+ const focus = await joinWithFocus(roomJid);
+ const c = await createXmppClient({ domain,
+ params: { token } });
- it('rejects MUC join with invalid Lua regex room claim', async () => {
- const CONFERENCE = 'conference.localhost';
- const roomJid = `regex-invalid-${Date.now()}@${CONFERENCE}`;
+ clients.push(focus, c);
+
+ return c.joinRoom(roomJid);
+ }
+
+ /**
+ * Asserts that a join was refused with the given error condition.
+ *
+ * @param {object} presence - The presence the join returned.
+ * @param {string} condition - The expected error condition.
+ * @returns {void}
+ */
+ function assertJoinError(presence, condition) {
+ assert.equal(presence.attrs.type, 'error',
+ `join must be rejected: ${presence.toString()}`);
+ assert.ok(
+ presence.getChild('error')?.getChild(condition),
+ `error must contain <${condition}/>: ${presence.toString()}`
+ );
+ }
+
+ /**
+ * Asserts that a join succeeded.
+ *
+ * @param {object} presence - The presence the join returned.
+ * @returns {void}
+ */
+ function assertJoinOk(presence) {
+ assert.notEqual(presence.attrs.type, 'error',
+ `join must succeed: ${presence.toString()}`);
+ }
- // '(invalid' is an unbalanced Lua pattern capture — string:match() would
- // raise "unfinished capture" without the pcall guard in verify_room().
+ it('does not crash on connect with an unsupported regex room claim', async () => {
+ // process_and_verify_token only stores the claim; it does not evaluate
+ // the pattern. The pattern is checked in verify_room(), at MUC join.
const token = mintAsapToken({
room: '(invalid',
context: { room: { regex: true } }
});
-
- const focus = await joinWithFocus(roomJid);
const c = await asapClient({ token });
- clients.push(focus, c);
-
- const presence = await c.joinRoom(roomJid);
-
- assert.equal(presence.attrs.type, 'error', 'join must be rejected');
- assert.ok(
- presence.getChild('error')?.getChild('invalid-regex'),
- 'error must contain <invalid-regex/>'
- );
+ clients.push(c);
+ assert.ok(c.jid, 'connection must succeed: pattern not evaluated at auth time');
});
- // ── Room regex claim: pattern is used as written ──────────────────────────
- //
- // Lua pattern classes are case-sensitive: %d is a digit, %D is anything
- // but a digit. verify_room() must therefore not change the case of a room
- // claim that is flagged as a pattern.
+ it('rejects MUC join with an unbalanced capture in the regex room claim', async () => {
+ const presence = await joinWithPattern(
+ `regex-invalid-${Date.now()}@conference.localhost`, '(invalid');
- // Letters only, so that the room name matches %D+ ("not a digit").
- const lettersOnly = () => Date.now()
- .toString(36)
- .replace(/[0-9]/g, 'x');
+ assertJoinError(presence, 'invalid-regex');
+ });
- it('accepts MUC join when the regex room claim uses an uppercase class', async () => {
- const CONFERENCE = 'conference.localhost';
- const roomJid = `rxcase${lettersOnly()}@${CONFERENCE}`;
+ it('accepts MUC join when the room name starts with the prefix before .*', async () => {
+ const id = Date.now();
- const token = mintAsapToken({
- room: 'rxcase%D+',
- context: { room: { regex: true } }
- });
+ assertJoinOk(await joinWithPattern(`rxpre_${id}[email protected]`, `rxpre_${id}.*`));
+ });
- const focus = await joinWithFocus(roomJid);
- const c = await asapClient({ token });
+ it('accepts MUC join when the room name is exactly the prefix before .*', async () => {
+ const id = Date.now();
- clients.push(focus, c);
+ assertJoinOk(await joinWithPattern(`rxexact_${id}@conference.localhost`, `rxexact_${id}.*`));
+ });
- const presence = await c.joinRoom(roomJid);
+ it('accepts MUC join when the room name has the prefix and suffix around .*', async () => {
+ const id = Date.now();
- assert.notEqual(presence.attrs.type, 'error',
- `join must succeed: ${presence.toString()}`);
+ assertJoinOk(await joinWithPattern(`rxmid_${id}[email protected]`,
+ `rxmid_${id}.*_tail`));
});
- it('rejects MUC join when the room does not match the regex room claim', async () => {
- const CONFERENCE = 'conference.localhost';
- const roomJid = `rxcase${Date.now()}@${CONFERENCE}`;
+ it('accepts MUC join with a regex room claim that has no .*', async () => {
+ const id = Date.now();
- // 'rxcase%D+' does not match a room name that ends in digits.
- const token = mintAsapToken({
- room: 'rxcase%D+',
- context: { room: { regex: true } }
- });
+ assertJoinOk(await joinWithPattern(`rxlit_${id}@conference.localhost`, `rxlit_${id}`));
+ });
- const focus = await joinWithFocus(roomJid);
- const c = await asapClient({ token });
+ it('lowercases a regex room claim before it is matched', async () => {
+ // Room names are lowercase. The claim must be lowercased, the same as a
+ // claim that is not a pattern.
+ const id = Date.now();
- clients.push(focus, c);
+ assertJoinOk(await joinWithPattern(`rxcase_${id}[email protected]`, `RxCase_${id}.*`));
+ });
- const presence = await c.joinRoom(roomJid);
+ it('rejects MUC join when the prefix is not at the start of the room name', async () => {
+ const id = Date.now();
- assert.equal(presence.attrs.type, 'error', 'join must be rejected');
- assert.ok(
- presence.getChild('error')?.getChild('room-name-does-not-match'),
- 'error must contain <room-name-does-not-match/>'
- );
+ assertJoinError(await joinWithPattern(`x_rxstart_${id}@conference.localhost`, `rxstart_${id}.*`),
+ 'room-name-does-not-match');
});
- // ── Room regex claim: matching limits ─────────────────────────────────────
- //
- // Lua patterns backtrack, and one match blocks Prosody. verify_room()
- // therefore refuses to match a room name that is longer than
- // token_regex_room_max_length (128), or a pattern that has more than
- // token_regex_max_quantifiers (3) quantifiers. Both refusals report
- // <invalid-regex/>, which distinguishes them from a pattern that was
- // evaluated and did not match (<room-name-does-not-match/>).
-
- it('accepts MUC join when the regex room claim is at the quantifier limit', async () => {
- const CONFERENCE = 'conference.localhost';
- const roomJid = `rxlimit${lettersOnly()}@${CONFERENCE}`;
+ it('rejects MUC join when the room name does not end with the suffix', async () => {
+ const id = Date.now();
- // Three quantifiers is the default limit, and must still be allowed.
- const token = mintAsapToken({
- room: 'rxlimit%D*%D*%D+',
- context: { room: { regex: true } }
- });
+ assertJoinError(await joinWithPattern(`rxend_${id}[email protected]`, `rxend_${id}.*_tail`),
+ 'room-name-does-not-match');
+ });
- const focus = await joinWithFocus(roomJid);
- const c = await asapClient({ token });
+ it('rejects MUC join when the prefix and suffix overlap in the room name', async () => {
+ // The room name has the prefix 'rxover_<id>_ab' and the suffix 'b',
+ // but only because they share the last 'b'. The room name is too
+ // short to hold both.
+ const id = Date.now();
- clients.push(focus, c);
+ assertJoinError(await joinWithPattern(`rxover_${id}[email protected]`, `rxover_${id}_ab.*b`),
+ 'room-name-does-not-match');
+ });
- const presence = await c.joinRoom(roomJid);
+ it('rejects MUC join when the regex room claim has two .*', async () => {
+ const id = Date.now();
- assert.notEqual(presence.attrs.type, 'error',
- `join must succeed: ${presence.toString()}`);
+ assertJoinError(await joinWithPattern(`rxtwo_${id}[email protected]`, `rxtwo_${id}.*_.*`),
+ 'invalid-regex');
});
- it('rejects MUC join when the regex room claim has too many quantifiers', async () => {
- const CONFERENCE = 'conference.localhost';
-
- // Under the 128-char room-name limit, so the quantifier limit is what
- // must reject this join.
- const roomJid = `${'a'.repeat(90)}${lettersOnly()}@${CONFERENCE}`;
+ it('rejects MUC join when the regex room claim uses a Lua pattern class', async () => {
+ const id = Date.now();
- // The pathological case: four quantifiers over a long run of 'a's, and
- // a trailing '%d' that a letters-only room name can never satisfy, so
- // every combination is tried before the match fails.
- const token = mintAsapToken({
- room: 'a*a*a*a*%d',
- context: { room: { regex: true } }
- });
+ assertJoinError(await joinWithPattern(`rxclass_${id}@conference.localhost`, 'rxclass_%d+'),
+ 'invalid-regex');
+ });
- const focus = await joinWithFocus(roomJid);
- const c = await asapClient({ token });
+ it('rejects MUC join when the regex room claim has a . without *', async () => {
+ const id = Date.now();
- clients.push(focus, c);
+ assertJoinError(await joinWithPattern(`rxdot_${id}@conference.localhost`, `rxdot.${id}`),
+ 'invalid-regex');
+ });
- const presence = await c.joinRoom(roomJid);
+ it('rejects MUC join when the regex room claim has a - quantifier', async () => {
+ const id = Date.now();
- assert.equal(presence.attrs.type, 'error', 'join must be rejected');
- assert.ok(
- presence.getChild('error')?.getChild('invalid-regex'),
- `pattern must be refused unevaluated: ${presence.toString()}`
- );
+ assertJoinError(await joinWithPattern(`rxdash-${id}@conference.localhost`, `rxdash-${id}`),
+ 'invalid-regex');
});
+ // ── Room regex claim: room name length limit ──────────────────────────────
+ //
+ // verify_room() refuses to match a room name that is longer than
+ // token_regex_room_max_length (128), and reports <invalid-regex/>.
+
it('rejects MUC join when the room name is too long to match a regex claim', async () => {
const CONFERENCE = 'conference.localhost';
// Over the 128-char room-name limit.
- const roomJid = `${'x'.repeat(150)}${lettersOnly()}@${CONFERENCE}`;
+ const roomJid = `${'x'.repeat(150)}${Date.now()}@${CONFERENCE}`;
// '.*' matches any room name, so without the length limit this join
// would be allowed.
@@ -377,12 +391,12 @@ describe('mod_auth_token (ASAP / RS256)', () => {
// conference.localhost, where the option keeps its default.
it('rejects MUC join with a regex room claim when patterns are disabled', async () => {
- const roomName = `rxoff${lettersOnly()}`;
+ const roomName = `rxoff${Date.now()}`;
const roomJid = `${roomName}@conference.noregex.test`;
// Matches the room name, so only the disabled option can reject it.
const token = mintAsapToken({
- room: 'rxoff%D+',
+ room: 'rxoff.*',
context: { room: { regex: true } }
});
@@ -402,11 +416,11 @@ describe('mod_auth_token (ASAP / RS256)', () => {
});
it('accepts the same regex room claim on a host where patterns are enabled', async () => {
- const roomName = `rxoff${lettersOnly()}`;
+ const roomName = `rxoff${Date.now()}`;
const roomJid = `${roomName}@conference.localhost`;
const token = mintAsapToken({
- room: 'rxoff%D+',
+ room: 'rxoff.*',
context: { room: { regex: true } }
});
@@ -420,4 +434,176 @@ describe('mod_auth_token (ASAP / RS256)', () => {
assert.notEqual(presence.attrs.type, 'error',
`join must succeed: ${presence.toString()}`);
});
+
+ it('rejects a non-matching regex room claim as unsupported when patterns are disabled', async () => {
+ // The disabled-patterns check runs before the match, so the error is
+ // <invalid-regex/> and not <room-name-does-not-match/>.
+ const presence = await joinWithPattern(`rxoffmiss${Date.now()}@conference.noregex.test`,
+ 'other.*', { domain: 'noregex.test' });
+
+ assertJoinError(presence, 'invalid-regex');
+ });
+
+ // ── Room regex claim: a claim that does not match is not a missing claim ──
+ //
+ // On conference.noclaim.test the room claim is optional. A token without a
+ // room claim can join any room. A pattern that is present but does not
+ // match must still be refused: it must not be treated as a missing claim.
+
+ it('rejects MUC join when the regex room claim does not match and the room claim is optional', async () => {
+ const presence = await joinWithPattern(`rxopt${Date.now()}@conference.noclaim.test`,
+ 'other.*', { domain: 'noclaim.test' });
+
+ assertJoinError(presence, 'room-name-does-not-match');
+ });
+
+ it('accepts MUC join when the regex room claim matches and the room claim is optional', async () => {
+ const id = Date.now();
+
+ assertJoinOk(await joinWithPattern(`rxopt_${id}[email protected]`,
+ `rxopt_${id}.*`, { domain: 'noclaim.test' }));
+ });
+
+ it('rejects MUC join when a literal room claim does not match and the room claim is optional', async () => {
+ const roomJid = `rxoptlit${Date.now()}@conference.noclaim.test`;
+ const token = mintAsapToken({ room: 'other' });
+
+ const focus = await joinWithFocus(roomJid);
+ const c = await createXmppClient({ domain: 'noclaim.test',
+ params: { token } });
+
+ clients.push(focus, c);
+
+ assertJoinError(await c.joinRoom(roomJid), 'not-allowed');
+ });
+
+ it('rejects MUC join with a regex flag but no room claim when the room claim is optional', async () => {
+ // The token connects, because the room claim is optional. The pattern
+ // flag without a claim is refused at join.
+ const presence = await joinWithPattern(`rxoptnone${Date.now()}@conference.noclaim.test`,
+ undefined, { domain: 'noclaim.test' });
+
+ assertJoinError(presence, 'invalid-regex');
+ });
+
+ // ── Room regex claim: tenant boundary ─────────────────────────────────────
+ //
+ // The pattern is matched against the room name without its tenant. The
+ // tenant of the room must still equal the sub claim of the token.
+ // mod_muc_domain_mapper maps room@conference.<tenant>.localhost to
+ // [<tenant>][email protected].
+
+ it('rejects MUC join when the regex room claim matches but the tenant differs', async () => {
+ const presence = await joinWithPattern(`rxtenant${Date.now()}@conference.tenantb.localhost`,
+ '.*', { sub: 'tenanta' });
+
+ assertJoinError(presence, 'not-allowed');
+ });
+
+ it('accepts MUC join when the regex room claim matches in the tenant of the token', async () => {
+ assertJoinOk(await joinWithPattern(`rxtenant${Date.now()}@conference.tenanta.localhost`,
+ '.*', { sub: 'tenanta' }));
+ });
+
+ // ── Room regex claim: edge cases of the matcher ───────────────────────────
+
+ it('accepts MUC join with a regex room claim of only .*', async () => {
+ assertJoinOk(await joinWithPattern(`rxany${Date.now()}@conference.localhost`, '.*'));
+ });
+
+ it('accepts MUC join when the regex room claim is only a suffix after .*', async () => {
+ const id = Date.now();
+
+ assertJoinOk(await joinWithPattern(`rxsuf_${id}[email protected]`, `.*_${id}_tail`));
+ });
+
+ it('rejects MUC join when the regex room claim is an empty string', async () => {
+ // An empty pattern has no '.*' and matches only an empty room name,
+ // which does not exist.
+ assertJoinError(await joinWithPattern(`rxempty${Date.now()}@conference.localhost`, ''),
+ 'room-name-does-not-match');
+ });
+
+ it('accepts MUC join with a regex room claim of * as the wildcard', async () => {
+ // The wildcard check runs before the pattern check, so '*' keeps its
+ // meaning also when the claim is flagged as a pattern.
+ assertJoinOk(await joinWithPattern(`rxstar${Date.now()}@conference.localhost`, '*'));
+ });
+
+ for (const pattern of [ '.*.*', '..*', '.**', 'a*.b' ]) {
+ it(`rejects MUC join when the regex room claim is ${pattern}`, async () => {
+ assertJoinError(await joinWithPattern(`rxbad${Date.now()}@conference.localhost`, pattern),
+ 'invalid-regex');
+ });
+ }
+
+ it('accepts MUC join when the room name is exactly the prefix and the suffix', async () => {
+ const id = Date.now();
+
+ assertJoinOk(await joinWithPattern(`rxgap_${id}[email protected]`, `rxgap_${id}.*_tail`));
+ });
+
+ it('rejects MUC join when the room name is shorter than the prefix and the suffix', async () => {
+ const id = Date.now();
+
+ // 'rxgap_<id>_tai' ends with '_tai', not '_tail', and is one character
+ // too short to hold both parts.
+ assertJoinError(await joinWithPattern(`rxgap_${id}[email protected]`, `rxgap_${id}.*_tail`),
+ 'room-name-does-not-match');
+ });
+
+ it('rejects MUC join when the room name is shorter than the prefix', async () => {
+ const tag = String(Date.now());
+
+ assertJoinError(await joinWithPattern(`rxs${tag}@conference.localhost`, `rxs${tag}longerprefix.*`),
+ 'room-name-does-not-match');
+ });
+
+ it('accepts MUC join when the regex room claim prefix is a prefix of a longer id', async () => {
+ // 'demo_4.*' also matches 'demo_40'. This is how prefixes work, and the
+ // issuer must take it into account.
+ const id = Date.now();
+
+ assertJoinOk(await joinWithPattern(`rxid_${id}[email protected]`, `rxid_${id}.*`));
+ });
+
+ it('rejects MUC join when the regex room claim is a number, and stays up', async () => {
+ assertJoinError(await joinWithPattern(`rxnum${Date.now()}@conference.localhost`, 123),
+ 'invalid-regex');
+
+ // Prosody must still serve the next join.
+ assertJoinOk(await joinWithPattern(`rxnumok${Date.now()}@conference.localhost`, '.*'));
+ });
+
+ it('accepts MUC join when the room name is exactly token_regex_room_max_length', async () => {
+ const tag = String(Date.now());
+ const roomName = `${'r'.repeat(128 - tag.length)}${tag}`;
+
+ assertJoinOk(await joinWithPattern(`${roomName}@conference.localhost`, '.*'));
+ });
+
+ it('rejects MUC join when the room name is one longer than token_regex_room_max_length', async () => {
+ const tag = String(Date.now());
+ const roomName = `${'r'.repeat(129 - tag.length)}${tag}`;
+
+ assertJoinError(await joinWithPattern(`${roomName}@conference.localhost`, '.*'), 'invalid-regex');
+ });
+
+ // ── Room regex claim: pattern length ──────────────────────────────────────
+ //
+ // A pattern longer than token_regex_room_max_length plus the two characters
+ // of '.*' can never match a room name that passes the length check. It is
+ // refused without a match, and the log line holds a truncated pattern.
+
+ it('rejects MUC join when the regex room claim is longer than the room name limit', async () => {
+ assertJoinError(await joinWithPattern(`rxlong${Date.now()}@conference.localhost`,
+ `${'a'.repeat(131)}.*`), 'invalid-regex');
+ });
+
+ it('rejects MUC join with a very long regex room claim, and stays up', async () => {
+ assertJoinError(await joinWithPattern(`rxhuge${Date.now()}@conference.localhost`,
+ `${'a'.repeat(4000)}.*`), 'invalid-regex');
+
+ assertJoinOk(await joinWithPattern(`rxhugeok${Date.now()}@conference.localhost`, '.*'));
+ });
});
diff --git a/tests/prosody/mod_auth_token_spec.js b/tests/prosody/mod_auth_token_spec.js
index 74f48396a7..f84f43df07 100644
--- a/tests/prosody/mod_auth_token_spec.js
+++ b/tests/prosody/mod_auth_token_spec.js
@@ -335,7 +335,9 @@ describe('mod_auth_token (claims on XEP-0198 resumption)', () => {
const roomB = `resumeb${n}`;
const jidA = `${roomA}@${CONFERENCE}`;
const jidB = `${roomB}@${CONFERENCE}`;
- const pattern = `^resume[ab]${n}$`;
+
+ // one '.*' is the only pattern syntax a room claim supports
+ const pattern = `resume.*${n}`;
const focusA = await joinWithFocus(jidA);
const focusB = await joinWithFocus(jidB);
const participant = await tokenClient(roomA, mintAsapToken({