Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
fix(auth-token): guard c2s-session-updated cross-host handler against foreign VirtualHosts
damencho2a6a746567f3
diff --git a/resources/prosody-plugins/mod_auth_token.lua b/resources/prosody-plugins/mod_auth_token.luaindex 4a584803ee..b47e9aa589 100644--- a/resources/prosody-plugins/mod_auth_token.lua+++ b/resources/prosody-plugins/mod_auth_token.lua@@ -182,7 +182,11 @@ module:hook_global('c2s-session-updated', function (event)end-- we care to handle sessions from other hosts (anonymous hosts)- if module.host ~= event.from_session.host then+ -- Skip if from_session was already authenticated by its own token-auth module+ -- (indicated by _jitsi_auth_done=true), to avoid a second module instance+ -- on a different VirtualHost (e.g. hs256.localhost) incorrectly re-verifying+ -- a token signed for the original host and then closing the session.+ if module.host ~= event.from_session.host and not from_session._jitsi_auth_done then-- Handle session updates (e.g., when a session is resumed on some anonymous host with a token we need to do all the checks here)session.auth_token = event.from_session.auth_token;diff --git a/tests/prosody/docker/cfg/components.cfg.lua b/tests/prosody/docker/cfg/components.cfg.luaindex 8ab6d12d09..a9f40248bd 100644--- a/tests/prosody/docker/cfg/components.cfg.lua+++ b/tests/prosody/docker/cfg/components.cfg.lua@@ -9,9 +9,10 @@ Component "lobby.conference.localhost" "muc"muc_room_default_public_jids = true-- Breakout MUC component for mod_muc_breakout_rooms tests.--- Only Prosody admins ([email protected]) may create rooms here; the module--- creates them programmatically. Joining an unregistered UUID room triggers--- on_breakout_room_pre_create which destroys it and returns an error.+-- Only Prosody admins ([email protected]) may create rooms here, matching+-- real-life behaviour where Jicofo creates breakout rooms on behalf of the+-- moderator. The on_breakout_room_pre_create hook then enforces that the UUID+-- is registered in the main room before admitting non-admin occupants.Component "breakout.conference.localhost" "muc"storage = "memory"restrict_room_creation = truediff --git a/tests/prosody/helpers/xmpp_client.js b/tests/prosody/helpers/xmpp_client.jsindex d1e6f94ba6..defc2b58f0 100644--- a/tests/prosody/helpers/xmpp_client.js+++ b/tests/prosody/helpers/xmpp_client.js@@ -1108,15 +1108,103 @@ export async function createXmppClient({ host = 'localhost', domain, params, use},/**- * Abruptly closes the underlying WebSocket without sending a stream- * close. Prosody will put the session into smacks hibernation, keeping- * it in full_sessions so mod_auth_jitsi-anonymous can find it by- * resumption_token on the next connect.+ * Abruptly terminates the underlying WebSocket without a close+ * handshake. Prosody sees an unclean disconnect and hibernates the+ * SMACKS session. On reconnect the library sends <resume>, which+ * triggers Prosody's c2s-session-updated hook (where the+ * breakout-rooms bug manifested).+ *+ * Jitsi's SMACKS resume flow (mod_auth_token.lua + mod_smacks.lua):+ * 1. Client reconnects with ?previd=<smacks-id> in the WebSocket URL.+ * 2. mod_jitsi_session.lua sets session.previd from the URL param.+ * 3. SASL runs; mod_auth_token.lua's get_username_from_token finds+ * the hibernating session whose resumption_token == session.previd+ * and copies its username, preserving the original UUID.+ * 4. Post-auth features include <sm>; @xmpp/stream-management sends+ * <resume previd='...'>. mod_smacks looks up the session with+ * registry key "uuid@host/smacks_id" — which now matches because+ * step 3 preserved the UUID — and resumes successfully.+ * 5. sessionmanager.update_session fires c2s-session-updated.+ *+ * Without ?previd in the URL, step 3 assigns a fresh UUID, the key+ * doesn't match, mod_smacks falls back to a new session, and the+ * regression is never exercised.+ *+ * IMPORTANT: must use ws.terminate() (TCP RST, no close frame), NOT+ * ws.close() / Socket.end(). A clean WebSocket close (code 1000)+ * causes Prosody to destroy the SMACKS session rather than hibernate it.+ *+ * xmpp.socket – @xmpp/websocket Socket wrapper+ * xmpp.socket.socket – underlying ws.WebSocket instance with terminate()*/dropConnection() {+ // Patch the reconnect URL to carry ?previd=<smacks-id> so that+ // mod_jitsi_session.lua sets session.previd and mod_auth_token.lua+ // can preserve session.username across the SASL exchange, allowing+ // mod_smacks.lua's registry lookup to succeed.+ const smId = xmpp.streamManagement?.id;++ if (smId) {+ try {+ const serviceUrl = new URL(xmpp.options.service);++ serviceUrl.searchParams.set('previd', smId);+ xmpp.options.service = serviceUrl.toString();+ } catch { /* ignore */ }+ }try {- xmpp.websocket?.socket?.end();+ const ws = xmpp.socket?.socket;++ if (ws?.terminate) {+ ws.terminate();+ } else {+ xmpp.socket?.end();+ }} catch { /* ignore */ }+ },++ /**+ * Resolves when the SMACKS (XEP-0198) session resume is fully complete+ * after a dropConnection(). Set up this listener BEFORE calling+ * dropConnection() to avoid a race with a very fast reconnect.+ *+ * The @xmpp/reconnect 'reconnected' event fires after entity.open()+ * resolves, which happens on the stream-open header — BEFORE stream+ * features are processed and therefore BEFORE SMACKS sends <resume>+ * and receives <resumed>. The SMACKS handler then sets entity.status+ * = 'online' directly (no event emitted). We poll that field after+ * 'reconnected' to detect the moment the resume is complete, so that+ * any stanza sent afterwards is processed on the fully-resumed session.+ *+ * @param {number} [timeout=10000]+ */+ waitForReconnect(timeout = 10000) {+ return new Promise((resolve, reject) => {+ let done = false;+ const timer = setTimeout(() => {+ if (!done) {+ done = true;+ reject(new Error('Timeout waiting for session to reconnect'));+ }+ }, timeout);++ xmpp.reconnect.once('reconnected', () => {+ const checkOnline = () => {+ if (done) {+ return;+ }+ if (xmpp.status === 'online') {+ done = true;+ clearTimeout(timer);+ resolve();+ } else {+ setTimeout(checkOnline, 50);+ }+ };++ checkOnline();+ });+ });}};}diff --git a/tests/prosody/mod_muc_breakout_rooms_spec.js b/tests/prosody/mod_muc_breakout_rooms_spec.jsindex e5f73bc30a..32e04bfa13 100644--- a/tests/prosody/mod_muc_breakout_rooms_spec.js+++ b/tests/prosody/mod_muc_breakout_rooms_spec.js@@ -251,6 +251,74 @@ describe('mod_muc_breakout_rooms', () => {});+ // -------------------------------------------------------------------------+ // smacks regression: eb43cf601+ // -------------------------------------------------------------------------+ describe('smacks session resume', () => {++ it('participant can join breakout room after smacks session resume', async () => {+ // Regression test for eb43cf601:+ // mod_auth_token.lua's c2s-session-updated handler used to copy+ // jitsi_breakout_main_jid from the fresh TCP session (which never+ // joined a room, so the field is nil) onto the old hibernating+ // session, silently overwriting the valid value set when the user+ // originally joined the main room. Without the fix, the subsequent+ // on_occupant_pre_join_or_change check+ // origin.jitsi_breakout_main_jid ~= main_room.jid+ // fails with nil ~= main_room_jid → not-allowed error.+ const name = nextRoomName();+ const { focus, moderator } = await createRoom(name);++ clients.push(focus, moderator);++ // Register a breakout room via the OP_ADD message flow and capture its JID.+ await moderator.sendBreakoutRoomsMessage(BREAKOUT_MUC, OP_ADD, { subject: 'Breakout 1' });+ const createPayload = await waitForBreakoutUpdate(moderator);+ const breakoutJid = Object.values(createPayload.rooms).find(r => !r.isMainRoom)?.jid;++ assert.ok(breakoutJid, 'breakout room JID must be in the create broadcast');++ // Focus (jicofo) joins the breakout room first to physically create it,+ // matching the real-life flow where Jicofo creates the room on the+ // breakout component (which has restrict_room_creation=true, so only+ // Prosody admins like [email protected] can create rooms here).+ const focusBreakoutPresence = await focus.joinRoom(breakoutJid, 'focus');++ assert.notEqual(+ focusBreakoutPresence.attrs.type,+ 'error',+ 'focus must be able to join (create) the breakout room'+ );++ // Arm the reconnect listener BEFORE dropping so we cannot miss the+ // 'reconnected' event even if the reconnect is very fast.+ const reconnected = moderator.waitForReconnect();++ // Snap the WebSocket. @xmpp/client schedules a reconnect after ~1 s;+ // on reconnect it sends <resume> which triggers Prosody's+ // c2s-session-updated — the event where the bug cleared jitsi_breakout_main_jid.+ moderator.dropConnection();++ // Wait until SMACKS resume is fully complete (entity.status === 'online').+ // waitForReconnect() polls entity.status after the @xmpp/reconnect+ // 'reconnected' event, because that event fires on stream-open which+ // is before stream features (including SMACKS) are negotiated.+ await reconnected;++ // The moderator now tries to join the breakout room.+ // With the bug: jitsi_breakout_main_jid is nil → not-allowed error.+ // With the fix: jitsi_breakout_main_jid is preserved → join succeeds.+ const presence = await moderator.joinRoom(breakoutJid);++ assert.notEqual(+ presence.attrs.type,+ 'error',+ 'participant must be able to join breakout room after smacks session resume'+ );+ });++ });+// -------------------------------------------------------------------------// access control// -------------------------------------------------------------------------@@ -268,7 +336,7 @@ describe('mod_muc_breakout_rooms', () => {clients.push(user);const fakeBreakoutJid = `00000000-0000-0000-0000-000000000000@${BREAKOUT_MUC}`;- const presence = await user.joinRoom(fakeBreakoutJid);+ const presence = await user.joinRoom(fakeBreakoutJid, undefined, { timeout: 15000 });assert.equal(presence.attrs.type,