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): clear the token of anonymous sessions that bind a resource (#18122)

Дамян Минков1f7fcbeffedc
diff --git a/resources/prosody-plugins/mod_auth_jitsi-anonymous.lua b/resources/prosody-plugins/mod_auth_jitsi-anonymous.lua
index 998bb72139..fd94439992 100644
--- a/resources/prosody-plugins/mod_auth_jitsi-anonymous.lua
+++ b/resources/prosody-plugins/mod_auth_jitsi-anonymous.lua
@@ -86,6 +86,13 @@ end
sasl.registerMechanism("ANONYMOUS", {"anonymous"}, anonymous);
+-- The token kept above for a matching previd is meant for mod_auth_token, which verifies it
+-- when the stream is resumed (c2s-session-updated). A resumed stream never binds a resource,
+-- so a session reaching bind is a new one and its token was never verified: drop it.
+module:hook("resource-bind", function (event)
+ event.session.auth_token = nil;
+end, 100);
+
module:hook("pre-resource-unbind", function (e)
local error, session = e.error, e.session;
diff --git a/resources/prosody-plugins/mod_muc_wait_for_host.lua b/resources/prosody-plugins/mod_muc_wait_for_host.lua
index 81112c11a0..644a2c8c71 100644
--- a/resources/prosody-plugins/mod_muc_wait_for_host.lua
+++ b/resources/prosody-plugins/mod_muc_wait_for_host.lua
@@ -1,7 +1,7 @@
-- Loaded on the main MUC component.
-- Holds unauthenticated guests in a persistent lobby until an authenticated host joins.
--- A "host" is any session with a JWT auth_token or with PLAIN credentials whose JID
--- domain matches muc_mapper_domain_base. Prosody admins (e.g. jicofo) bypass the check
+-- A "host" is any session with a JWT auth_token verified by mod_auth_token or with PLAIN
+-- credentials whose JID domain matches muc_mapper_domain_base. Prosody admins (e.g. jicofo) bypass the check
-- and are not counted as hosts.
-- When the first guest arrives before any host, a persistent lobby room is created via
-- mod_persistent_lobby + mod_muc_lobby_rooms, and guests are held there. When an
@@ -35,6 +35,11 @@ local audit_logger = logger.make_logger('mod_'..module.name, 'audit');
local lobby_muc_component_config = 'lobby.' .. muc_domain_base;
local lobby_host;
+-- mod_auth_token marks the sessions whose token it verified, on login or on stream resumption
+local function has_verified_token(session)
+ return session and session.auth_token and session._jitsi_auth_done;
+end
+
if not disable_auto_owners then
module:hook('muc-occupant-joined', function (event)
local room, occupant, session = event.room, event.occupant, event.origin;
@@ -43,7 +48,7 @@ if not disable_auto_owners then
-- for jwt authenticated and username and password authenticated
-- only if it is not a moderated room
if not is_moderated_room and
- (session.auth_token or (session.username and jid.host(occupant.bare_jid) == muc_domain_base)) then
+ (has_verified_token(session) or (session.username and jid.host(occupant.bare_jid) == muc_domain_base)) then
room:set_affiliation(true, occupant.bare_jid, 'owner');
end
end, 2);
@@ -65,7 +70,7 @@ module:hook('muc-occupant-pre-join', function (event)
-- the main virtual host that requires tokens
if jid.host(o.bare_jid) == muc_domain_base
-- or this is anonymous that upgraded by passing token which we validated
- or prosody.full_sessions[o.jid].auth_token then
+ or has_verified_token(prosody.full_sessions[o.jid]) then
room.has_host = true;
end
end
@@ -104,7 +109,7 @@ end);
module:hook('room_has_host', function(event)
local room, occupant, session = event.room, event.occupant, event.session;
- if session.auth_token
+ if has_verified_token(session)
or (session.username and jid.host(occupant.bare_jid) == muc_domain_base) then
return true;
end
diff --git a/resources/prosody-plugins/mod_test_observer_http.lua b/resources/prosody-plugins/mod_test_observer_http.lua
index 963bb002e3..baa93cf80f 100644
--- a/resources/prosody-plugins/mod_test_observer_http.lua
+++ b/resources/prosody-plugins/mod_test_observer_http.lua
@@ -506,6 +506,7 @@ module:provides("http", {
headers = { ["Content-Type"] = "application/json" };
body = json.encode({
auth_token = session.auth_token,
+ jitsi_auth_done = session._jitsi_auth_done,
jitsi_web_query_room = session.jitsi_web_query_room,
jitsi_web_query_prefix = session.jitsi_web_query_prefix,
jitsi_meet_room = session.jitsi_meet_room,
diff --git a/tests/prosody/helpers/test_observer.js b/tests/prosody/helpers/test_observer.js
index 689bb735c3..fd718495a6 100644
--- a/tests/prosody/helpers/test_observer.js
+++ b/tests/prosody/helpers/test_observer.js
@@ -148,6 +148,23 @@ export async function getSessionFeatures(fullJid) {
return (await res.json()).features;
}
+/**
+ * Returns the current token fields (auth_token and the JWT-derived claims) of a
+ * live c2s session.
+ *
+ * @param {string} fullJid e.g. 'abc123@localhost/res1'
+ * @returns {Promise<object>}
+ */
+export async function getSessionLive(fullJid) {
+ const res = await fetch(`${BASE}/session-live?jid=${encodeURIComponent(fullJid)}`);
+
+ if (!res.ok) {
+ throw new Error(`getSessionLive failed: ${res.status} ${await res.text()}`);
+ }
+
+ return res.json();
+}
+
/**
* Sets jitsi_meet_context_user and jitsi_meet_context_features on an active c2s
* session identified by full JID. Allows tests to simulate JWT token context
diff --git a/tests/prosody/mod_auth_jitsi_anonymous_spec.js b/tests/prosody/mod_auth_jitsi_anonymous_spec.js
index 13c851e5bd..0b5506ee89 100644
--- a/tests/prosody/mod_auth_jitsi_anonymous_spec.js
+++ b/tests/prosody/mod_auth_jitsi_anonymous_spec.js
@@ -1,5 +1,6 @@
import assert from 'assert';
+import { getSessionLive } from './helpers/test_observer.js';
import { createXmppClient } from './helpers/xmpp_client.js';
describe('mod_auth_jitsi-anonymous', () => {
@@ -68,6 +69,24 @@ describe('mod_auth_jitsi-anonymous', () => {
'reconnecting with previd must yield the same username');
});
+ it('drops the token of a new session that reuses the username via previd', async () => {
+ const c1 = await connect();
+ const username = c1.jid.split('@')[0];
+ const smId = c1.smId;
+
+ c1.dropConnection();
+
+ // Binds a resource instead of resuming the hibernating session.
+ const c2 = await connect({ params: { previd: smId,
+ token: 'not-a-jwt' } });
+
+ assert.equal(c2.jid.split('@')[0], username, 'previd must have matched the hibernating session');
+
+ const session = await getSessionLive(c2.jid);
+
+ assert.equal(session.auth_token, undefined, 'a session that was not resumed must not keep the token');
+ });
+
it('assigns a new username when previd does not match any session', async () => {
const c = await connect({ params: { previd: 'nonexistent-token' } });
diff --git a/tests/prosody/mod_muc_wait_for_host_spec.js b/tests/prosody/mod_muc_wait_for_host_spec.js
index 25177eee5d..a6013d077d 100644
--- a/tests/prosody/mod_muc_wait_for_host_spec.js
+++ b/tests/prosody/mod_muc_wait_for_host_spec.js
@@ -1,6 +1,8 @@
import assert from 'assert';
import { mintAsapToken } from './helpers/jwt.js';
+import { prosodyShell } from './helpers/prosody_shell.js';
+import { getSessionLive } from './helpers/test_observer.js';
import { createXmppClient, joinWithFocus } from './helpers/xmpp_client.js';
import { isAvailablePresence } from './helpers/xmpp_utils.js';
@@ -112,6 +114,115 @@ describe('mod_muc_wait_for_host', () => {
});
});
+ // -------------------------------------------------------------------------
+ // Guests on the jitsi-anonymous host (no token_verification on this MUC)
+ // -------------------------------------------------------------------------
+ describe('guest on the jitsi-anonymous host', () => {
+
+ const ANON_DOMAIN = 'jitsi-anonymous.localhost';
+
+ /**
+ * Connects an anonymous client, drops its connection so the session
+ * hibernates and returns its stream management id and username.
+ *
+ * @returns {Promise<{smId: string, username: string}>}
+ */
+ async function hibernatedSession() {
+ const c = await connect({ domain: ANON_DOMAIN });
+ const smId = c.smId;
+ const username = c.jid.split('@')[0];
+
+ c.dropConnection();
+
+ return { smId,
+ username };
+ }
+
+ /**
+ * Asserts the join was held by the lobby.
+ *
+ * @param {Element} presence - The join response.
+ * @returns {void}
+ */
+ function assertBlocked(presence) {
+ assert.equal(presence.attrs.type, 'error', 'join must be rejected');
+ assert.ok(
+ presence.getChild('error')?.getChild('registration-required'),
+ 'error condition must be registration-required'
+ );
+ }
+
+ it('without previd and token is blocked', async () => {
+ const r = room();
+
+ await focusJoin(r);
+
+ const guest = await connect({ domain: ANON_DOMAIN });
+
+ assertBlocked(await guest.joinRoom(r));
+ });
+
+ it('with a matching previd and no token is blocked', async () => {
+ const r = room();
+
+ await focusJoin(r);
+
+ const { smId, username } = await hibernatedSession();
+ const guest = await connect({ domain: ANON_DOMAIN,
+ params: { previd: smId } });
+
+ assert.equal(guest.jid.split('@')[0], username, 'previd must have matched the hibernating session');
+ assertBlocked(await guest.joinRoom(r));
+ });
+
+ it('with a token and no previd is blocked', async () => {
+ const r = room();
+
+ await focusJoin(r);
+
+ const guest = await connect({ domain: ANON_DOMAIN,
+ params: { token: 'not-a-jwt' } });
+
+ assertBlocked(await guest.joinRoom(r));
+ });
+
+ it('with a matching previd and a token, binding instead of resuming, is blocked', async () => {
+ const r = room();
+
+ await focusJoin(r);
+
+ const { smId, username } = await hibernatedSession();
+ const guest = await connect({ domain: ANON_DOMAIN,
+ params: { previd: smId,
+ token: 'not-a-jwt' } });
+
+ assert.equal(guest.jid.split('@')[0], username, 'previd must have matched the hibernating session');
+ assertBlocked(await guest.joinRoom(r));
+ });
+
+ it('resuming with a valid token leaves a verified token on the session', async () => {
+ // The HS256 host would verify the RS256 token against its own secret and
+ // close the session; production has a single token VirtualHost.
+ await prosodyShell('module:unload("auth_token", "hs256.localhost")');
+
+ try {
+ const guest = await connect({ domain: ANON_DOMAIN });
+ const reconnected = guest.waitForReconnect();
+
+ guest.dropConnection({ token: mintAsapToken({ room: room().split('@')[0] }) });
+ await reconnected;
+
+ const session = await getSessionLive(guest.jid);
+
+ // What muc_wait_for_host checks before counting the session as a host.
+ assert.ok(session.auth_token, 'the token presented on resume must be kept');
+ assert.equal(session.jitsi_auth_done, true, 'the token must be marked as verified');
+ } finally {
+ await prosodyShell('module:load("auth_token", "hs256.localhost")');
+ }
+ });
+ });
+
// -------------------------------------------------------------------------
// Room does not exist yet — creator bypass
// -------------------------------------------------------------------------