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(prosody): scope voice-agent tokens to their room and tenant claims
Jaya Allamsetty84f36535e7b1
diff --git a/resources/prosody-plugins/mod_voice_agent_component.lua b/resources/prosody-plugins/mod_voice_agent_component.luaindex eb91219bb4..20c1fde9ab 100644--- a/resources/prosody-plugins/mod_voice_agent_component.lua+++ b/resources/prosody-plugins/mod_voice_agent_component.lua@@ -26,8 +26,10 @@---- ── Endpoints (contract frozen at v1.0 — see AGENT_REST_API.md) ───────────────-- Authentication: Authorization: Bearer <system ASAP token>, verified against--- prosody_password_public_key_repo_url (NOT login tokens), exactly like--- mod_muc_jigasi_invite.+-- prosody_password_public_key_repo_url (NOT login tokens), like mod_muc_jigasi_invite. Unlike it, the token's+-- room and sub claims scope the caller, as a meeting token's do: token/util's verify_room checks them against+-- the resolved room ('*' allows every room, a '*' sub every tenant), as mod_muc_size does for its routes, and+-- a request for another room is 403.---- `conference` names the room as customers know it, '<tenant>/<room>' or '<room>' (plain or percent-encoded, any-- case), or by its JID (what jicofo and the media relay send, and the only way to name a breakout room). Every@@ -61,10 +63,12 @@-- state } ] }), unless its Last-Event-ID is still in the replay buffer, in which case it gets the events it-- missed instead. A keepalive comment every 15 s; closed after voice_agent_event_stream_max_age seconds-- (the client reconnects with Last-Event-ID); at most voice_agent_max_event_streams open at once (503)--- auth: ASAP, like the provisioning routes+-- auth: ASAP, like the provisioning routes; a stream for every room, or for a room that does not exist yet,+-- needs the wildcard claims (there is no room to check the token against)---- Provisioning auth needs prosody_password_public_key_repo_url: without it the module refuses to load (a meeting--- token would otherwise pass), unless voice_agent_insecure_skip_auth is set for a dev rig. Lifecycle webhooks need+-- token would otherwise pass), unless voice_agent_insecure_skip_auth is set for a dev rig. It also needs+-- muc_mapper_domain_base, which verify_room resolves rooms with, as the token modules do. Lifecycle webhooks need-- voice_agent_webhook_secret: without it an invite with a callbackUrl is refused rather than delivered unsigned.-- voice_agent_callback_allowed_hosts (exact host or .suffix) limits where callbacks may go; empty allows any host,-- so an OSS deployment that accepts invites from several customers should set it (prosody POSTs there).@@ -189,6 +193,13 @@ elseendtoken_util = module:require 'token/util'.new(module);token_util:set_asap_key_server(ASAP_KEY_SERVER);+ local muc_domain_base = module:get_option_string('muc_mapper_domain_base');+ if muc_domain_base == nil or muc_domain_base == '' then+ -- verify_room resolves the room under this domain; without it every token check would fail closed as a 500.+ module:log('error', 'No muc_mapper_domain_base set: token room claims cannot be verified. '+ .. 'Disabling /voice-agent.');+ return;+ endendlocal main_muc_module;@@ -197,23 +208,20 @@ process_host_module(muc_component_host, function(host_module)main_muc_module = host_module;end);-local function is_token_valid(token)- if token == nil then- module:log('warn', 'no token provided');- return false;- end-+-- Verifies a token; returns the session token/util filled with its claims (room, sub), or nil.+local function verified_session(token)local session = {};session.auth_token = token;local verified, reason, msg = token_util:process_and_verify_token(session);if not verified thenmodule:log('warn', 'not a valid token %s %s', tostring(reason), tostring(msg));- return false;+ return nil;end- return true;+ return session;end--- Verifies the Authorization header; returns nil when authorized, or the error response.+-- Verifies the Authorization header; returns nil and the token's session when authorized (no session for a dev+-- rig without auth), or the error response.local function check_authorization(request)if INSECURE_SKIP_AUTH thenreturn nil;@@ -229,10 +237,11 @@ local function check_authorization(request)module:log('warn', 'Authorization header is invalid');return { status_code = 401 };end- if not is_token_valid(token) then+ local session = verified_session(token);+ if not session thenreturn { status_code = 401 };end- return nil;+ return nil, session;end-- Constant-time secret comparison: HMACs under a per-load random key have a fixed length and leak nothing about@@ -278,6 +287,44 @@ local function error_response(status_code, message)return { status_code = status_code, body = json.encode({ error = message }) };end+-- Checks the token's room and sub claims against the room with token/util's verify_room, as mod_muc_size does for+-- its routes: '*' allows every room, a '*' sub every tenant. Shared-secret callers (jicofo, the media relay) and a+-- dev rig without auth have no session and are not scoped. Returns nil when allowed, or the error response.+local function check_room_authorization(session, room)+ if session == nil then+ return nil;+ end+ local allowed, reason = token_util:verify_room(session, room.jid);+ if not allowed then+ -- verify_room answers a bare false when the claims do not name this room, with no reason to show.+ module:log('warn', 'Token not authorized for room %s%s: token room %s, sub %s', room.jid,+ reason and (' (' .. reason .. ')') or '', tostring(session.jitsi_meet_room),+ tostring(session.jitsi_meet_domain));+ return error_response(403, 'Token not authorized for this room');+ end+ return nil;+end++-- An event stream for every room, or for a room that does not exist yet, has no room for verify_room to check:+-- the token must then be allowed every room (a '*' room claim, or none while the deployment does not require+-- one), within its tenant when its sub names one and domain verification is on.+local function check_stream_authorization(session, conference)+ if session == nil then+ return nil;+ end+ local room_claim, sub = session.jitsi_meet_room, session.jitsi_meet_domain;+ local every_room = room_claim == '*' or (room_claim == nil and not token_util.requireRoomClaim);+ local tenant = conference and conference:match('^([^/]+)/');+ local every_tenant = not token_util.enableDomainVerification or sub == '*'+ or (tenant ~= nil and type(sub) == 'string' and sub:lower() == tenant);+ if every_room and every_tenant then+ return nil;+ end+ module:log('warn', 'Token not authorized for an event stream on %s: token room %s, sub %s',+ conference or 'every room', tostring(room_claim), tostring(sub));+ return error_response(403, 'Token not authorized for this stream');+end+-- Reads a JSON request body; returns the payload, or nil plus the error response.local function parse_json_request(request)if request.headers.content_type ~= JSON_CONTENT_TYPE or (not request.body or #request.body == 0) then@@ -830,7 +877,7 @@ local function handle_invite(event)invite_count();local request = event.request;- local auth_error = check_authorization(request);+ local auth_error, session = check_authorization(request);if auth_error thenreturn auth_error;end@@ -844,6 +891,10 @@ local function handle_invite(event)module:log('warn', 'No room found for %s', tostring(payload.conference));return error_response(404, 'Room not found');end+ local room_error = check_room_authorization(session, room);+ if room_error then+ return room_error;+ endif not is_valid_display_name(payload.displayName) thenreturn error_response(400, 'Missing or invalid displayName');@@ -968,7 +1019,7 @@ local function handle_dismiss(event)dismiss_count();local request = event.request;- local auth_error = check_authorization(request);+ local auth_error, session = check_authorization(request);if auth_error thenreturn auth_error;end@@ -981,6 +1032,10 @@ local function handle_dismiss(event)if not room thenreturn error_response(404, 'Room not found');end+ local room_error = check_room_authorization(session, room);+ if room_error then+ return room_error;+ endlocal agent_id = normalize_agent_id(payload.agentId);if not agent_id or not remove_agent(room, agent_id, 'dismissed') then@@ -1001,7 +1056,7 @@ endlocal function handle_list(event)local request = event.request;- local auth_error = check_authorization(request);+ local auth_error, session = check_authorization(request);if auth_error thenreturn auth_error;end@@ -1010,6 +1065,10 @@ local function handle_list(event)if not room thenreturn error_response(404, 'Room not found');end+ local room_error = check_room_authorization(session, room);+ if room_error then+ return room_error;+ endlocal agents = (room.jitsiMetadata and room.jitsiMetadata.agents) or {};return { status_code = 200, body = json.encode({ agents = agents }) };@@ -1018,7 +1077,7 @@ endlocal function handle_get(event)local request = event.request;- local auth_error = check_authorization(request);+ local auth_error, session = check_authorization(request);if auth_error thenreturn auth_error;end@@ -1028,6 +1087,10 @@ local function handle_get(event)if not room thenreturn error_response(404, 'Room not found');end+ local room_error = check_room_authorization(session, room);+ if room_error then+ return room_error;+ endlocal entry, _, agent_id = get_agent(room, params.agentId);if not entry thenreturn error_response(404, 'Agent not found');@@ -1045,7 +1108,7 @@ local function stream_conference(conference)endlocal room = find_room(conference);if room then- return public_room_name(room);+ return public_room_name(room), room;endif conference:find('@', 1, true) thenreturn nil;@@ -1062,15 +1125,20 @@ end-- Opens a lifecycle event stream on the response once the request is authorized; returns an error response instead-- when it cannot be opened.-local function open_event_stream(request, response)+local function open_event_stream(request, response, session)local params = query_params(request);- local conference;+ local conference, room;if params.conference ~= nil then- conference = stream_conference(params.conference);+ conference, room = stream_conference(params.conference);if not conference thenreturn error_response(404, 'Room not found');endend+ local scope_error = room and check_room_authorization(session, room)+ or check_stream_authorization(session, conference);+ if scope_error then+ return scope_error;+ endif shared.count >= MAX_EVENT_STREAMS thenreturn error_response(503, 'Too many event streams');end@@ -1110,7 +1178,8 @@ endlocal function handle_events(event)-- The runner only calls its function for input it is given, so the event goes in run().async.runner(function(e)- local refused = check_authorization(e.request) or open_event_stream(e.request, e.response);+ local refused, session = check_authorization(e.request);+ refused = refused or open_event_stream(e.request, e.response, session);if refused thene.response.status_code = refused.status_code;e.response:send(refused.body);@@ -1123,7 +1192,7 @@ endlocal function handle_status(event)local request = event.request;- local auth_error = check_status_authorization(request);+ local auth_error, session = check_status_authorization(request);if auth_error thenreturn auth_error;end@@ -1136,6 +1205,10 @@ local function handle_status(event)if not room thenreturn error_response(404, 'Room not found');end+ local room_error = check_room_authorization(session, room);+ if room_error then+ return room_error;+ endif not STATUS_TRANSITIONS[payload.state] thenreturn error_response(400, 'Invalid state');end@@ -1170,7 +1243,7 @@ endlocal function handle_dial(event)local request = event.request;- local auth_error = check_dial_authorization(request);+ local auth_error, session = check_dial_authorization(request);if auth_error thenreturn auth_error;end@@ -1180,6 +1253,10 @@ local function handle_dial(event)if not room thenreturn error_response(404, 'Room not found');end+ local room_error = check_room_authorization(session, room);+ if room_error then+ return room_error;+ endlocal entry, jicofo_entry, agent_id = get_agent(room, params.agentId);if not entry or not jicofo_entry.endpoint thenreturn error_response(404, 'Agent not found');diff --git a/tests/prosody/lua/mod_voice_agent_component_spec.lua b/tests/prosody/lua/mod_voice_agent_component_spec.luaindex 88b5e94e82..ae84de0305 100644--- a/tests/prosody/lua/mod_voice_agent_component_spec.lua+++ b/tests/prosody/lua/mod_voice_agent_component_spec.lua@@ -71,6 +71,10 @@ end-- ---------------------------------------------------------------------------local token_valid = true;+-- The claims a verified token carries, as token/util puts them on the session, and what verify_room answers.+local token_room, token_sub = '*', '*';+local verify_room_ok = true;+local verify_room_calls = {};local mock_room;local timers = {};local host_hooks = {};@@ -154,12 +158,21 @@ local key_cached = true;local token_util_stub = {new = function()return {+ enableDomainVerification = true,+ requireRoomClaim = false,set_asap_key_server = function() end,- process_and_verify_token = function()+ process_and_verify_token = function(_, session)if not key_cached thenrequire('util.async').waiter();end+ session.jitsi_meet_room = token_room;+ session.jitsi_meet_domain = token_sub;return token_valid, 'reason', 'msg';+ end,+ verify_room = function(_, session, room_address)+ table.insert(verify_room_calls, { room = room_address; token_room = session.jitsi_meet_room });+ if verify_room_ok then return true; end+ return false, 'room-mismatch', 'Room does not match the room from token';end};end@@ -178,6 +191,7 @@ local function option(key, default)if key == 'voice_agent_status_secret' then return 'jicofo-secret'; endif key == 'voice_agent_dial_secret' then return 'relay-secret'; endif key == 'prosody_password_public_key_repo_url' then return 'https://keys.example.com/'; end+ if key == 'muc_mapper_domain_base' then return 'localhost'; endif key == 'voice_agent_callback_allowed_hosts' then return { 'app.example.com' }; endreturn default;end@@ -286,12 +300,19 @@ local function invite(payload, opts)endreturn post('POST voice-agent/invite', payload, opts);end-local function dismiss(payload) return post('POST voice-agent/dismiss', payload); end+local function dismiss(payload, opts) return post('POST voice-agent/dismiss', payload, opts); endlocal function status(payload, opts) return post('POST voice-agent/status', payload, opts); end-local function get(query)+local function get(query, opts)+ opts = opts or {};return routes['GET voice-agent/get']({- request = { headers = { authorization = 'Bearer t' }; url = { query = query } } });+ request = { headers = { authorization = opts.token or 'Bearer t' }; url = { query = query } } });+end++local function list(query, opts)+ opts = opts or {};+ return routes['GET voice-agent/list']({+ request = { headers = { authorization = opts.token or 'Bearer t' }; url = { query = query } } });endlocal function dial(query, opts)@@ -413,6 +434,9 @@ describe('mod_voice_agent_component', function()if timer.delay == 15 then pcall(timer.fn); endendtoken_valid = true;+ token_room, token_sub = '*', '*';+ verify_room_ok = true;+ verify_room_calls = {};mock_room = fresh_room();http_requests = {};metadata_events = 0;@@ -1320,6 +1344,67 @@ describe('mod_voice_agent_component', function()end)end)+ describe('token room scope', function()+ -- token/util's verify_room gets the token's session and the resolved room; the stub answers as the test says.+ it('checks the token against the resolved room JID on the provisioning routes', function()+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });+ assert.are.equal(mock_room.jid, verify_room_calls[1].room);+ assert.are.equal('*', verify_room_calls[1].token_room);+ get('conference=r&agentId=agent-support');+ assert.are.equal(2, #verify_room_calls);+ end)++ it('refuses a token for another room with 403 on every route, changing nothing', function()+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });+ verify_room_ok = false;+ assert.are.equal(403, invite({ conference = 'r'; displayName = 'Bot'; agentId = 'other' }).status_code);+ assert.are.equal(403, get('conference=r&agentId=agent-support').status_code);+ assert.are.equal(403, list('conference=r').status_code);+ assert.are.equal(403, dismiss({ conference = 'r'; agentId = 'agent-support' }).status_code);+ assert.are.equal(403, status({ conference = 'r'; agentId = 'agent-support'; state = 'active' }).status_code);+ assert.are.equal(403, dial('conference=r&agentId=agent-support').status_code);+ assert.are.same({ 'agent-support' }, agent_ids(mock_room));+ assert.are.equal('provisioning', mock_room.jitsiMetadata.agents['agent-support'].state);+ end)++ it('does not scope the shared-secret callers: jicofo and the relay act on any room', function()+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });+ verify_room_ok = false;+ assert.are.equal(200, dial('conference=r&agentId=agent-support', { token = 'Bearer relay-secret' }).status_code);+ assert.are.equal(200, status({ conference = 'r'; agentId = 'agent-support'; state = 'active' },+ { token = 'Bearer jicofo-secret' }).status_code);+ end)++ it('event stream: a room that exists is checked like the routes', function()+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });+ verify_room_ok = false;+ local result, response = events('conference=r');+ assert.are.equal(403, result.status_code);+ assert.are.equal(0, #response.writes);+ end)++ it('event stream for every room needs a token allowed every room and every tenant', function()+ token_room = 'r';+ assert.are.equal(403, events(nil).status_code);+ token_room = '*';+ token_sub = 'acme';+ assert.are.equal(403, events(nil).status_code);+ token_sub = '*';+ assert.is_true((events(nil)));+ end)++ it('event stream for a room that does not exist yet: every room, within the token tenant', function()+ token_room = '*';+ token_sub = 'acme';+ assert.is_true((events('conference=acme/missing')));+ token_sub = 'other';+ assert.are.equal(403, events('conference=acme/missing').status_code);+ token_room = 'missing';+ token_sub = 'acme';+ assert.are.equal(403, events('conference=acme/missing').status_code);+ end)+ end)+describe('alternate configurations (module reloaded)', function()-- Reloads the module with option overrides into the same `routes` table, so the request helpers above-- drive the reloaded module; the defaults are restored afterwards.@@ -1369,6 +1454,16 @@ describe('mod_voice_agent_component', function()assert.are.equal('0\r\n\r\n', response.writes[#response.writes]);end)+ it('refuses to load without muc_mapper_domain_base, which the token room check resolves rooms with', function()+ reload_with({ muc_mapper_domain_base = '' });+ assert.is_nil(routes['POST voice-agent/invite']);+ local logged = false;+ for _, entry in ipairs(logs) do+ if entry.level == 'error' and entry.text:find('muc_mapper_domain_base', 1, true) then logged = true; end+ end+ assert.is_true(logged, 'the missing option must be logged');+ end)+it('refuses to load without an ASAP key repository, so a meeting token can never provision agents', function()reload_with({ prosody_password_public_key_repo_url = '' });assert.is_nil(next(routes), 'no route may be registered');