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): set the recording session_id from the client's jibri iq (#17975)
Aaron van Meertenb8b0302528f2
diff --git a/resources/prosody-plugins/mod_jibri_session.lua b/resources/prosody-plugins/mod_jibri_session.luaindex 16431c81bd..0a1e00ea7b 100644--- a/resources/prosody-plugins/mod_jibri_session.lua+++ b/resources/prosody-plugins/mod_jibri_session.lua@@ -1,91 +1,98 @@-- mod_jibri_session----- Enriches jibri `start` IQ stanzas with recording metadata before they reach--- jibri. It handles two passes:+-- Enriches the jibri `start` iq that a client sends to the focus occupant of+-- its room (room@muc/focus) with recording metadata, before the iq reaches the+-- MUC:----- 1. Client → jicofo (no `room` attr): reads the initiating user's identity--- from the XMPP session and writes it to--- app_data.file_recording_metadata.initiator {id, group}.+-- app_data.file_recording_metadata.initiator {id, group}: the sender's+-- identity, read from its XMPP session.----- 2. Jicofo → jibri (`room` attr present): looks up the MUC room, reads its--- meetingId, and writes it to--- app_data.file_recording_metadata.conference_details.session_id.+-- app_data.file_recording_metadata.conference_details.session_id: the+-- meetingId of the room the iq is addressed to.----- Must be loaded on both the main virtual host and the jicofo virtual host.+-- Both replace anything the client sent. conference_details is removed when+-- the room has no meetingId. The room is taken from the iq's `to`, which is+-- also what the MUC routes the iq by; the `room` attribute of the jibri element+-- is ignored. Iqs that are not addressed to a room on this prosody are left+-- untouched.+--+-- Jicofo copies app_data into the start iq it sends to jibri, so the metadata+-- reaches jibri whichever prosody jicofo uses to talk to jibri.+--+-- Must be loaded on every virtual host that clients who can start a recording+-- connect to (e.g. the main and the guest virtual hosts). It is not needed on+-- the jicofo virtual host. A start iq sent by a server-side module (e.g. from+-- the MUC component) does not pass through this module, so that module must+-- set these values itself.local json = require 'cjson.safe';+local jid_bare = require 'util.jid'.bare;local util = module:require 'util';local room_jid_match_rewrite = util.room_jid_match_rewrite;local get_room_from_jid = util.get_room_from_jid;--- This needs to be attached to the main virtual host and the virtual host where jicofo is connected and authenticated.--- The first pass is the iq coming from the client where we get the creator and attach it to the app_data.--- The second pass is jicofo approving that and inviting jibri where we attach the session_id information to app_datalocal function attachJibriSessionId(event)-local stanza = event.stanza;- if stanza.name == "iq" then- local jibri = stanza:get_child('jibri', 'http://jitsi.org/protocol/jibri');- if jibri then- if jibri.attr.action and jibri.attr.action:lower() == 'start' then-- local update_app_data = false;- local app_data = jibri.attr.app_data;- if app_data then- local err;- app_data, err = json.decode(app_data);- if err then- module:log('warn', 'Failed to decode jibri app_data from %s: %s', stanza.attr.from, err);- return;- end- else- app_data = {};- end- if app_data.file_recording_metadata == nil then- app_data.file_recording_metadata = {};- end+ local stanza = event.stanza;+ if stanza.name ~= 'iq' then+ return;+ end- if jibri.attr.room then- local jibri_room = jibri.attr.room;- jibri_room = room_jid_match_rewrite(jibri_room)- local room = get_room_from_jid(jibri_room);- if room then- local conference_details = {};- conference_details["session_id"] = room._data.meetingId;- app_data.file_recording_metadata.conference_details = conference_details;- update_app_data = true;- end- else- -- no room is because the iq received by the initiator in the room- local session = event.origin;- -- if a token is provided, add data to app_data- if session ~= nil then- local initiator = {};+ local jibri = stanza:get_child('jibri', 'http://jitsi.org/protocol/jibri');+ if not jibri or not jibri.attr.action or jibri.attr.action:lower() ~= 'start' then+ return;+ end- if session.jitsi_meet_context_user ~= nil then- initiator.id = session.jitsi_meet_context_user.id;- else- initiator.id = session.granted_jitsi_meet_context_user_id;- end+ -- the client's iq is addressed to the focus occupant of the room (room@muc/focus)+ local room = stanza.attr.to and get_room_from_jid(room_jid_match_rewrite(jid_bare(stanza.attr.to)));+ if not room then+ -- not addressed to a room here, so the MUC will not route it to jicofo+ return;+ end- initiator.group- = session.jitsi_meet_context_group or session.granted_jitsi_meet_context_group_id;+ local app_data = jibri.attr.app_data;+ if app_data then+ local err;+ app_data, err = json.decode(app_data);+ if err then+ module:log('warn', 'Failed to decode jibri app_data from %s: %s', stanza.attr.from, err);+ return;+ end+ else+ app_data = {};+ end+ if app_data.file_recording_metadata == nil then+ app_data.file_recording_metadata = {};+ end+ local metadata = app_data.file_recording_metadata;- app_data.file_recording_metadata.initiator = initiator- update_app_data = true;- end+ local meeting_id = room._data.meetingId;+ if meeting_id then+ metadata.conference_details = { session_id = meeting_id };+ else+ module:log('warn', 'No meetingId for room %s, recording started by %s has no session_id',+ room.jid, stanza.attr.from);+ metadata.conference_details = nil;+ end- end+ local session = event.origin;+ if session ~= nil then+ local initiator = {};- if update_app_data then- app_data = json.encode(app_data);- jibri.attr.app_data = app_data;- jibri:up()- stanza:up()- end- end+ if session.jitsi_meet_context_user ~= nil then+ initiator.id = session.jitsi_meet_context_user.id;+ else+ initiator.id = session.granted_jitsi_meet_context_user_id;end++ initiator.group = session.jitsi_meet_context_group or session.granted_jitsi_meet_context_group_id;++ metadata.initiator = initiator;+ else+ metadata.initiator = nil;end++ jibri.attr.app_data = json.encode(app_data);endmodule:hook('pre-iq/full', attachJibriSessionId);diff --git a/tests/prosody/lua/mod_jibri_session_spec.lua b/tests/prosody/lua/mod_jibri_session_spec.luaindex 8698e14c00..8cc01f6cba 100644--- a/tests/prosody/lua/mod_jibri_session_spec.lua+++ b/tests/prosody/lua/mod_jibri_session_spec.lua@@ -52,6 +52,14 @@ function mod_stub:log() end -- luacheck: ignore 212_G.module = mod_stub+package.preload['util.jid'] = function()+ return {+ bare = function(jid)+ return jid and jid:match('^([^/]+)')+ end,+ }+end+-- ----------------------------------------------------------------------------- Load the module under test-- ---------------------------------------------------------------------------@@ -80,8 +88,8 @@ local function make_jibri_elem(attrs)return eend-local function make_iq(jibri_elem)- local s = { name = 'iq', attr = {} }+local function make_iq(jibri_elem, attrs)+ local s = { name = 'iq', attr = attrs or {} }function s:get_child(tag, ns)if tag == 'jibri' and ns == JIBRI_NS then return jibri_elem endreturn nil@@ -104,12 +112,22 @@ enddescribe("mod_jibri_session", function()+ local ROOM_FOCUS = '[email protected]/focus'+before_each(function()mock.room_from_jid = nilmock.rewrite_jid = nilmock.last_room_jid = nilend)+ -- A start iq addressed to the focus occupant of a room that exists.+ local function start_iq_to_room(jibri_attrs)+ local attrs = { action = 'start' }+ for k, v in pairs(jibri_attrs or {}) do attrs[k] = v end+ local jibri = make_jibri_elem(attrs)+ return jibri, make_iq(jibri, { to = ROOM_FOCUS, from = '[email protected]/res' })+ end+-- -----------------------------------------------------------------------describe("non-matching stanzas pass through unchanged", function()@@ -126,145 +144,226 @@ describe("mod_jibri_session", function()end)it("ignores a jibri IQ with action 'stop'", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }local jibri = make_jibri_elem({ action = 'stop' })- hooked_fn(make_event(make_iq(jibri)))+ hooked_fn(make_event(make_iq(jibri, { to = ROOM_FOCUS }), {}))assert.is_nil(jibri.attr.app_data)end)it("ignores a jibri IQ with no action attribute", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }local jibri = make_jibri_elem({})- hooked_fn(make_event(make_iq(jibri)))+ hooked_fn(make_event(make_iq(jibri, { to = ROOM_FOCUS }), {}))assert.is_nil(jibri.attr.app_data)end)+ it("ignores a start IQ with no 'to'", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local jibri = make_jibri_elem({ action = 'start' })+ hooked_fn(make_event(make_iq(jibri), { jitsi_meet_context_user = { id = 'user-1' } }))+ assert.is_nil(mock.last_room_jid)+ assert.is_nil(jibri.attr.app_data)+ end)++ it("ignores a start IQ that is not addressed to a room", function()+ mock.room_from_jid = nil+ local existing = json.encode({ file_recording_metadata = { initiator = { id = 'x' } } })+ local jibri = make_jibri_elem({ action = 'start', app_data = existing })+ hooked_fn(make_event(make_iq(jibri, { to = ROOM_FOCUS }), { jitsi_meet_context_user = { id = 'user-1' } }))+ assert.equal(existing, jibri.attr.app_data)+ end)++ it("leaves jicofo's start IQ to jibri untouched", function()+ -- jicofo addresses jibri, not a room, and sets the room attribute+ mock.room_from_jid = nil+ local existing = json.encode({+ file_recording_metadata = {+ conference_details = { session_id = 'meeting-abc' },+ initiator = { id = 'user-1' },+ }+ })+ local jibri = make_jibri_elem({+ action = 'start',+ room = '[email protected]',+ app_data = existing,+ })+ hooked_fn(make_event(make_iq(jibri, { to = '[email protected]/res' }), {}))+ assert.equal('[email protected]', mock.last_room_jid)+ assert.equal(existing, jibri.attr.app_data)+ end)+end)-- ------------------------------------------------------------------------ -- Client → jicofo pass: no `room` attribute on the jibri element.- -- The module reads initiator identity from the XMPP session (event.origin).+ -- The initiator is read from the XMPP session (event.origin).-- ------------------------------------------------------------------------ describe("client-to-jicofo pass (no room attr on jibri element)", function()+ describe("initiator", function()++ before_each(function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ end)it("sets initiator.id from jitsi_meet_context_user.id", function()- local jibri = make_jibri_elem({ action = 'start' })- local session = { jitsi_meet_context_user = { id = 'user-abc' } }- hooked_fn(make_event(make_iq(jibri), session))+ local jibri, iq = start_iq_to_room()+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-abc' } }))assert.equal('user-abc', decoded_app_data(jibri).file_recording_metadata.initiator.id)end)it("falls back to granted_jitsi_meet_context_user_id when context_user is nil", function()- local jibri = make_jibri_elem({ action = 'start' })- local session = { granted_jitsi_meet_context_user_id = 'granted-id-xyz' }- hooked_fn(make_event(make_iq(jibri), session))+ local jibri, iq = start_iq_to_room()+ hooked_fn(make_event(iq, { granted_jitsi_meet_context_user_id = 'granted-id-xyz' }))assert.equal('granted-id-xyz', decoded_app_data(jibri).file_recording_metadata.initiator.id)end)it("sets initiator.group from jitsi_meet_context_group", function()- local jibri = make_jibri_elem({ action = 'start' })- local session = {+ local jibri, iq = start_iq_to_room()+ hooked_fn(make_event(iq, {jitsi_meet_context_user = { id = 'user-1' },jitsi_meet_context_group = 'group-A',- }- hooked_fn(make_event(make_iq(jibri), session))+ }))assert.equal('group-A', decoded_app_data(jibri).file_recording_metadata.initiator.group)end)it("falls back to granted_jitsi_meet_context_group_id when context_group is nil", function()- local jibri = make_jibri_elem({ action = 'start' })- local session = {+ local jibri, iq = start_iq_to_room()+ hooked_fn(make_event(iq, {jitsi_meet_context_user = { id = 'user-1' },granted_jitsi_meet_context_group_id = 'granted-group-B',- }- hooked_fn(make_event(make_iq(jibri), session))+ }))assert.equal('granted-group-B', decoded_app_data(jibri).file_recording_metadata.initiator.group)end)- it("does not modify app_data when origin (session) is nil", function()- local jibri = make_jibri_elem({ action = 'start' })- hooked_fn(make_event(make_iq(jibri), nil))- assert.is_nil(jibri.attr.app_data)+ it("replaces the initiator supplied by the client", function()+ local existing = json.encode({ file_recording_metadata = { initiator = { id = 'other', group = 'g' } } })+ local jibri, iq = start_iq_to_room({ app_data = existing })+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))+ local initiator = decoded_app_data(jibri).file_recording_metadata.initiator+ assert.equal('user-1', initiator.id)+ assert.is_nil(initiator.group)end)- it("preserves pre-existing top-level app_data keys", function()- local existing = json.encode({ custom_key = 'custom_value' })- local jibri = make_jibri_elem({ action = 'start', app_data = existing })- local session = { jitsi_meet_context_user = { id = 'user-1' } }- hooked_fn(make_event(make_iq(jibri), session))+ it("removes the initiator supplied by the client when origin (session) is nil", function()+ local existing = json.encode({ file_recording_metadata = { initiator = { id = 'other' } } })+ local jibri, iq = start_iq_to_room({ app_data = existing })+ hooked_fn(make_event(iq, nil))local data = decoded_app_data(jibri)- assert.equal('custom_value', data.custom_key)- assert.equal('user-1', data.file_recording_metadata.initiator.id)+ assert.is_nil(data.file_recording_metadata.initiator)+ assert.equal('meeting-abc', data.file_recording_metadata.conference_details.session_id)end)- it("preserves pre-existing file_recording_metadata fields", function()- local existing = json.encode({- file_recording_metadata = { existing_field = 'existing_value' }- })- local jibri = make_jibri_elem({ action = 'start', app_data = existing })- local session = { jitsi_meet_context_user = { id = 'user-1' } }- hooked_fn(make_event(make_iq(jibri), session))+ end)++ -- -----------------------------------------------------------------------+ -- The session_id is the meetingId of the room the iq is addressed to.+ -- -----------------------------------------------------------------------+ describe("conference details", function()++ it("sets conference_details.session_id from the room the iq is addressed to", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local jibri, iq = start_iq_to_room()+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))local data = decoded_app_data(jibri)- assert.equal('existing_value', data.file_recording_metadata.existing_field)+ assert.equal('meeting-abc', data.file_recording_metadata.conference_details.session_id)assert.equal('user-1', data.file_recording_metadata.initiator.id)end)- end)+ it("looks up the bare room JID, without the focus resource", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local _, iq = start_iq_to_room()+ hooked_fn(make_event(iq, {}))+ assert.equal('[email protected]', mock.last_room_jid)+ end)- -- ------------------------------------------------------------------------ -- Jicofo → jibri pass: `room` attribute present on the jibri element.- -- The module looks up the room and attaches its meetingId as session_id.- -- ------------------------------------------------------------------------ describe("jicofo-to-jibri pass (room attr present on jibri element)", function()-- it("sets conference_details.session_id from room meetingId", function()- mock.room_from_jid = { _data = { meetingId = 'meeting-123' } }- local jibri = make_jibri_elem({ action = 'start', room = '[email protected]' })- hooked_fn(make_event(make_iq(jibri)))- assert.equal('meeting-123',- decoded_app_data(jibri).file_recording_metadata.conference_details.session_id)+ it("passes the room JID through room_jid_match_rewrite before lookup", function()+ mock.rewrite_jid = '[sub][email protected]'+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local jibri = make_jibri_elem({ action = 'start' })+ hooked_fn(make_event(make_iq(jibri, { to = '[email protected]/focus' }), {}))+ assert.equal('[sub][email protected]', mock.last_room_jid)end)- it("does not modify app_data when room is not found", function()- mock.room_from_jid = nil- local jibri = make_jibri_elem({ action = 'start', room = '[email protected]' })- hooked_fn(make_event(make_iq(jibri)))- assert.is_nil(jibri.attr.app_data)+ it("replaces conference_details supplied by the client", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-real' } }+ local existing = json.encode({+ file_recording_metadata = { conference_details = { session_id = 'other', extra = 'x' } }+ })+ local jibri, iq = start_iq_to_room({ app_data = existing })+ hooked_fn(make_event(iq, {}))+ local details = decoded_app_data(jibri).file_recording_metadata.conference_details+ assert.equal('meeting-real', details.session_id)+ assert.is_nil(details.extra)end)- it("preserves pre-existing top-level app_data keys", function()- mock.room_from_jid = { _data = { meetingId = 'meeting-456' } }- local existing = json.encode({ custom_key = 'custom_value' })- local jibri = make_jibri_elem({- action = 'start',- room = '[email protected]',- app_data = existing,+ it("leaves conference_details unset when the room has no meetingId", function()+ mock.room_from_jid = { _data = {} }+ local jibri, iq = start_iq_to_room()+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))+ local data = decoded_app_data(jibri)+ assert.is_nil(data.file_recording_metadata.conference_details)+ assert.equal('user-1', data.file_recording_metadata.initiator.id)+ end)++ it("removes conference_details supplied by the client when the room has no meetingId", function()+ mock.room_from_jid = { _data = {} }+ local existing = json.encode({+ custom_key = 'custom_value',+ file_recording_metadata = { conference_details = { session_id = 'other' } }})- hooked_fn(make_event(make_iq(jibri)))+ local jibri, iq = start_iq_to_room({ app_data = existing })+ hooked_fn(make_event(iq, {}))local data = decoded_app_data(jibri)+ assert.is_nil(data.file_recording_metadata.conference_details)assert.equal('custom_value', data.custom_key)- assert.equal('meeting-456', data.file_recording_metadata.conference_details.session_id)end)- it("passes the room JID through room_jid_match_rewrite before lookup", function()- mock.rewrite_jid = '[email protected]'- mock.room_from_jid = { _data = { meetingId = 'meeting-789' } }- local jibri = make_jibri_elem({- action = 'start',- room = '[email protected]',+ it("uses the room from 'to' and ignores the room attribute set by the client", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local existing = json.encode({+ file_recording_metadata = {+ conference_details = { session_id = 'other' },+ initiator = { id = 'other' },+ }})- hooked_fn(make_event(make_iq(jibri)))- assert.equal('[email protected]', mock.last_room_jid)+ local jibri, iq = start_iq_to_room({ room = '[email protected]', app_data = existing })+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))+ assert.equal('[email protected]', mock.last_room_jid)+ local data = decoded_app_data(jibri)+ assert.equal('meeting-abc', data.file_recording_metadata.conference_details.session_id)+ assert.equal('user-1', data.file_recording_metadata.initiator.id)end)end)-- ------------------------------------------------------------------------ describe("malformed app_data", function()+ describe("existing app_data", function()++ before_each(function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ end)++ it("preserves pre-existing top-level app_data keys", function()+ local jibri, iq = start_iq_to_room({ app_data = json.encode({ custom_key = 'custom_value' }) })+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))+ local data = decoded_app_data(jibri)+ assert.equal('custom_value', data.custom_key)+ assert.equal('user-1', data.file_recording_metadata.initiator.id)+ end)++ it("preserves pre-existing file_recording_metadata fields", function()+ local existing = json.encode({ file_recording_metadata = { share = true } })+ local jibri, iq = start_iq_to_room({ app_data = existing })+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))+ local data = decoded_app_data(jibri)+ assert.is_true(data.file_recording_metadata.share)+ assert.equal('meeting-abc', data.file_recording_metadata.conference_details.session_id)+ end)it("silently ignores invalid JSON in app_data (cjson.safe: returns nil, no throw)", function()- local raw = '{not valid json{{'- local jibri = make_jibri_elem({ action = 'start', app_data = raw })- local session = { jitsi_meet_context_user = { id = 'user-1' } }- assert.has_no.errors(function() hooked_fn(make_event(make_iq(jibri), session)) end)+ local raw = '{not valid json{{'+ local jibri, iq = start_iq_to_room({ app_data = raw })+ assert.has_no.errors(function()+ hooked_fn(make_event(iq, { jitsi_meet_context_user = { id = 'user-1' } }))+ end)assert.equal(raw, jibri.attr.app_data) -- unchanged: module returned earlyend)