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): replay voice-agent events of rooms that are gone
Jaya Allamsettyaecf45938312
diff --git a/resources/prosody-plugins/mod_voice_agent_component.lua b/resources/prosody-plugins/mod_voice_agent_component.luaindex fbcd397e55..8fd433c318 100644--- a/resources/prosody-plugins/mod_voice_agent_component.lua+++ b/resources/prosody-plugins/mod_voice_agent_component.lua@@ -64,8 +64,9 @@-- 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; a named room that exists is checked like them, and a stream--- carries only the events of rooms the token may act on (verify_room per room). A replay naming a room that--- is gone, which can no longer be checked, falls back to a snapshot+-- carries only the events of rooms the token may act on (verify_room per room). A replayed event of a room+-- that is gone uses what verify_room answered for the same token while the room existed, and is skipped+-- when that token was never checked against it---- 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. It also needs@@ -86,6 +87,7 @@-- Copyright (C) 2026-present 8x8, Inc.local async = require 'util.async';+local cache_new = require 'util.cache'.new;local hashes = require 'util.hashes';local random = require 'util.random';local json = require 'cjson.safe';@@ -650,9 +652,13 @@ shared.seq = shared.seq or 0;-- The last EVENT_BUFFER_SIZE events as { seq, conference, room_jid, frame }, replayed to a reconnecting stream. The-- room is kept by JID, not by object, so the buffer never holds a destroyed room alive.shared.buffer = shared.buffer or {};+-- What verify_room answered for a token and a room ('<token hash> <room jid>' → boolean), kept after the room is+-- gone so a replay can still use it.+shared.decisions = shared.decisions or cache_new(4096);local rooms_with_agents = shared.rooms;local streams = shared.streams;local event_buffer = shared.buffer;+local decisions = shared.decisions;local EVENT_BUFFER_SIZE = 256;local EVENT_STREAM_KEEPALIVE = 15;-- Whether this instance owns the one keepalive timer (released on unload, see module.unload).@@ -718,8 +724,10 @@ local function stream_allows(stream, room)if not allowed and reason == 'room-does-not-exist' thenreturn nil;end- stream.allowed[room] = allowed and true or false;- return stream.allowed[room];+ allowed = allowed and true or false;+ stream.allowed[room] = allowed;+ decisions:set(stream.token_key .. ' ' .. room.jid, allowed);+ return allowed;end-- Records a lifecycle event (body: its encoded payload) and writes it to every stream in scope.@@ -753,11 +761,15 @@ local function missed_frames(last_event_id, stream)local frames = {};for _, entry in ipairs(event_buffer) doif entry.seq > seq and in_scope(stream, entry.conference) then- local room = get_room_from_jid(entry.room_jid);- local allowed = room and stream_allows(stream, room);- if allowed == nil then- -- A room that is gone can no longer be checked: a snapshot instead of a partial replay.- return nil;+ local allowed = stream.session == nil;+ if not allowed then+ local room = get_room_from_jid(entry.room_jid);+ allowed = room and stream_allows(stream, room);+ if allowed == nil then+ -- The room is gone: what verify_room answered for this token while it existed, if it was asked.+ -- A room this token was never checked against is one it never knew, so it is skipped.+ allowed = decisions:get(stream.token_key .. ' ' .. entry.room_jid);+ endendif allowed thentable.insert(frames, entry.frame);@@ -1198,6 +1210,7 @@ local function open_event_stream(request, response, session)response:write_headers();local stream = { response = response; conference = conference; opened = os.time(); session = session;+ token_key = session and hashes.sha256(session.auth_token, true);allowed = setmetatable({}, { __mode = 'k' }); };streams[stream] = true;shared.count = shared.count + 1;diff --git a/tests/prosody/lua/mod_voice_agent_component_spec.lua b/tests/prosody/lua/mod_voice_agent_component_spec.luaindex cf3aa23adb..4709341909 100644--- a/tests/prosody/lua/mod_voice_agent_component_spec.lua+++ b/tests/prosody/lua/mod_voice_agent_component_spec.lua@@ -15,10 +15,23 @@package.preload['util.hashes'] = function()return {- sha256 = function() return 'deadbeefcafef00d' end,+ -- Generated agent ids hash the random bytes; anything else (a token) hashes to its own key.+ sha256 = function(data)+ if data == 'xxxxxxxx' then return 'deadbeefcafef00d'; end+ return (tostring(data):gsub('.', function(c) return string.format('%02x', c:byte()); end));+ end,hmac_sha256 = function(_key, data, _hex) return 'h:' .. data end};end+-- util.cache: a plain map per cache, every one registered so each test starts empty.+local caches = {};+package.preload['util.cache'] = function()+ return { new = function()+ local entries = {};+ table.insert(caches, entries);+ return { get = function(_, k) return entries[k]; end, set = function(_, k, v) entries[k] = v; end };+ end };+endpackage.preload['util.random'] = function()return { bytes = function() return 'xxxxxxxx' end };end@@ -82,6 +95,8 @@ local verify_room_calls = {};local require_room_claim_calls = {};-- A breakout room the room lookups return for its JID, when a test sets one.local breakout_room = nil;+-- Room JIDs the lookups no longer find, as after a room is destroyed.+local gone_rooms = {};local mock_room;local timers = {};local host_hooks = {};@@ -110,7 +125,7 @@ local util_stub = {return mock_room;end,get_room_from_jid = function(jid)- if jid == 'missing' then return nil; end+ if jid == 'missing' or gone_rooms[jid] then return nil; endif breakout_room and jid == breakout_room.jid then return breakout_room; endreturn mock_room;end,@@ -465,6 +480,10 @@ describe('mod_voice_agent_component', function()verify_room_ok = true;verify_room_calls = {};breakout_room = nil;+ gone_rooms = {};+ for _, entries in ipairs(caches) do+ for k in pairs(entries) do entries[k] = nil; end+ endmock_room = fresh_room();http_requests = {};metadata_events = 0;@@ -1470,19 +1489,44 @@ describe('mod_voice_agent_component', function()assert.is_true((events('conference=acme/missing')));end)- it('replays to a scoped token, or sends a snapshot when a missed event names a room that is gone', function()+ it('replays a missed event of a room that is gone when the token was checked against it before', function()+ token_room = 'r';+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });+ local _, first = events('conference=r');+ local snapshot_id = stream_events(first)[1].id;+ first:disconnect();+ dismiss({ conference = 'r'; agentId = 'agent-support' });+ gone_rooms[mock_room.jid] = true;+ local _, second = events(nil, { lastEventId = snapshot_id });+ local received = stream_events(second);+ assert.are.equal(1, #received);+ assert.are.equal('agent.ended', received[1].event);+ end)++ it('skips a missed event of a gone room a token was never checked against, without a snapshot', function()+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });+ local _, first = events('conference=r');+ local snapshot_id = stream_events(first)[1].id;+ first:disconnect();+ dismiss({ conference = 'r'; agentId = 'agent-support' });+ gone_rooms[mock_room.jid] = true;+ local _, other = events(nil, { lastEventId = snapshot_id; token = 'Bearer another' });+ assert.are.equal(0, #stream_events(other), 'neither the event nor a snapshot');+ end)++ it('replays a missed event of a room that still exists after checking it', function()token_room = 'r';invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' });local _, first = events('conference=r');local snapshot_id = stream_events(first)[1].id;- status({ conference = 'r'; agentId = 'agent-support'; state = 'active' });first:disconnect();- local _, replayed = events('conference=r', { lastEventId = snapshot_id });- assert.are.equal('agent.connected', stream_events(replayed)[1].event);- replayed:disconnect();- verify_room_ok = function() return false, 'room-does-not-exist'; end;- local _, fallback = events(nil, { lastEventId = snapshot_id });- assert.are.equal('snapshot', stream_events(fallback)[1].event);+ status({ conference = 'r'; agentId = 'agent-support'; state = 'active' });+ verify_room_ok = false;+ local _, denied = events(nil, { lastEventId = snapshot_id; token = 'Bearer another' });+ assert.are.equal(0, #stream_events(denied));+ verify_room_ok = true;+ local _, allowed = events(nil, { lastEventId = snapshot_id; token = 'Bearer third' });+ assert.are.equal('agent.connected', stream_events(allowed)[1].event);end)end)@@ -1557,6 +1601,18 @@ describe('mod_voice_agent_component', function()assert.is_true(said_why);end)+ it('a dev rig without auth replays every missed event, rooms gone or not', function()+ reload_with({ prosody_password_public_key_repo_url = ''; voice_agent_insecure_skip_auth = true });+ invite({ conference = 'r'; displayName = 'Bot'; agentId = 'support' }, { omitAuth = true });+ local _, first = events('conference=r', { omitAuth = true });+ local snapshot_id = stream_events(first)[1].id;+ first:disconnect();+ post('POST voice-agent/dismiss', { conference = 'r'; agentId = 'agent-support' }, { omitAuth = true });+ gone_rooms[mock_room.jid] = true;+ local _, second = events(nil, { omitAuth = true; lastEventId = snapshot_id });+ assert.are.equal('agent.ended', stream_events(second)[1].event);+ end)+it('loads without a key repository only under the insecure dev flag', function()reload_with({ prosody_password_public_key_repo_url = ''; voice_agent_insecure_skip_auth = true });assert.is_function(routes['POST voice-agent/invite']);