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
test(prosody): integration tests for mod_jibri_session, handle non-object app_data (#17976)
Aaron van Meerten74ccabd98681
diff --git a/resources/prosody-plugins/mod_jibri_session.lua b/resources/prosody-plugins/mod_jibri_session.luaindex 0a1e00ea7b..271ee87844 100644--- a/resources/prosody-plugins/mod_jibri_session.lua+++ b/resources/prosody-plugins/mod_jibri_session.lua@@ -11,7 +11,10 @@-- meetingId of the room the iq is addressed to.---- 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+-- the room has no meetingId. An app_data that is not valid JSON is left+-- unchanged. An app_data or file_recording_metadata that is valid JSON but+-- not an object (null, a number, a string or a boolean) is replaced with an+-- empty object first. 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.@@ -58,13 +61,27 @@ local function attachJibriSessionId(event)module:log('warn', 'Failed to decode jibri app_data from %s: %s', stanza.attr.from, err);return;end+ -- valid JSON that does not decode to a table (null, which decodes to cjson.null, a number, a string or+ -- a boolean) carries nothing we can keep, so start from an empty object as if no app_data had been sent+ if type(app_data) ~= 'table' then+ module:log('warn', 'Replacing jibri app_data of type %s from %s to %s',+ type(app_data), stanza.attr.from, stanza.attr.to);+ app_data = {};+ endelseapp_data = {};end- if app_data.file_recording_metadata == nil then- app_data.file_recording_metadata = {};- end+ -- the same for a file_recording_metadata that is not a table, which we would fail to index belowlocal metadata = app_data.file_recording_metadata;+ if metadata ~= nil and type(metadata) ~= 'table' then+ module:log('warn', 'Replacing jibri file_recording_metadata of type %s from %s to %s',+ type(metadata), stanza.attr.from, stanza.attr.to);+ metadata = nil;+ end+ if metadata == nil then+ metadata = {};+ app_data.file_recording_metadata = metadata;+ endlocal meeting_id = room._data.meetingId;if meeting_id then@@ -92,7 +109,19 @@ local function attachJibriSessionId(event)metadata.initiator = nil;end- jibri.attr.app_data = json.encode(app_data);+ local encoded, encode_err = json.encode(app_data);+ if not encoded then+ -- cjson decodes NaN, Infinity and 1e999 but cannot encode them. Keep only the metadata set here, never+ -- the original, which could carry a client's own initiator and session_id+ module:log('error',+ 'Failed to encode jibri app_data from %s to %s, keeping only conference_details and initiator: %s',+ stanza.attr.from, stanza.attr.to, encode_err);+ encoded = json.encode({ file_recording_metadata = {+ conference_details = metadata.conference_details;+ initiator = metadata.initiator;+ } });+ end+ jibri.attr.app_data = encoded;endmodule:hook('pre-iq/full', attachJibriSessionId);diff --git a/resources/prosody-plugins/mod_test_observer.lua b/resources/prosody-plugins/mod_test_observer.luaindex 2ad7281fa7..663dcdc05d 100644--- a/resources/prosody-plugins/mod_test_observer.lua+++ b/resources/prosody-plugins/mod_test_observer.lua@@ -64,6 +64,7 @@ module:hook("iq/full", function(event)to = stanza.attr.to;action = jibri.attr.action;recording_mode = jibri.attr.recording_mode;+ app_data = jibri.attr.app_data;});endlocal dial = stanza:get_child('dial', 'urn:xmpp:rayo:1');diff --git a/resources/prosody-plugins/mod_test_observer_http.lua b/resources/prosody-plugins/mod_test_observer_http.luaindex dd14990069..963bb002e3 100644--- a/resources/prosody-plugins/mod_test_observer_http.lua+++ b/resources/prosody-plugins/mod_test_observer_http.lua@@ -628,7 +628,7 @@ module:provides("http", {end;-- GET /test-observer/[email protected]- -- Returns: { jid, hidden, occupant_count }+ -- Returns: { jid, hidden, occupant_count, meeting_id }["GET /rooms"] = function(event)local params = parse_query(event.request.url.query);local room_jid = params["jid"];@@ -649,6 +649,7 @@ module:provides("http", {jid = room.jid;hidden = room:get_hidden();occupant_count = count;+ meeting_id = room._data.meetingId;});};end;diff --git a/tests/prosody/README.md b/tests/prosody/README.mdindex 39a020ce60..07ba20b7cd 100644--- a/tests/prosody/README.md+++ b/tests/prosody/README.md@@ -62,7 +62,7 @@ shared state over plain HTTP on port 5280.|---|---|| `GET /test-observer/events` | Return all MUC events fired since the last clear || `DELETE /test-observer/events` | Clear the event log (call in `beforeEach`) |-| `GET /test-observer/rooms?jid=<room-jid>` | Return live room state: `{ jid, hidden, occupant_count }` |+| `GET /test-observer/rooms?jid=<room-jid>` | Return live room state: `{ jid, hidden, occupant_count, meeting_id }` |```jsimport { clearEvents, getEvents, getRoomState } from './helpers/test_observer.js';diff --git a/tests/prosody/docker/cfg/virtualhosts.cfg.lua b/tests/prosody/docker/cfg/virtualhosts.cfg.luaindex c271b06014..fc97709841 100644--- a/tests/prosody/docker/cfg/virtualhosts.cfg.lua+++ b/tests/prosody/docker/cfg/virtualhosts.cfg.lua@@ -23,6 +23,9 @@ VirtualHost "localhost""conference_duration";"filter_iq_rayo"; -- priority 1 on pre-iq/full; listed before filter_iq_jibri (matches prod)"filter_iq_jibri";+ -- Adds the initiator and the room's meetingId (session_id) to the+ -- app_data of a client's jibri start iq.+ "jibri_session";"muc_lobby_rooms";"jiconop";"muc_kick_participant";@@ -78,6 +81,10 @@ VirtualHost "localhost"-- Clients on whitelist.localhost bypass the lobby.muc_lobby_whitelist = { "whitelist.localhost" }+ -- mod_filter_iq_jibri: every test client connects from the same IP, and+ -- the jibri specs send more start/stop iqs than the default 9 per minute.+ max_number_ip_attempts_per_minute = 100+-- VirtualHost for the focus (jicofo) test helper.-- Clients authenticate with username "focus" and password "focussecret".-- The account is pre-created in the Docker image (see Dockerfile).diff --git a/tests/prosody/helpers/test_observer.js b/tests/prosody/helpers/test_observer.jsindex 9e2458e2ef..689bb735c3 100644--- a/tests/prosody/helpers/test_observer.js+++ b/tests/prosody/helpers/test_observer.js@@ -339,7 +339,7 @@ export async function clearOtlpTraces() {/*** Returns room state from Prosody's internal MUC state.* @param {string} roomJid e.g. '[email protected]'- * @returns {Promise<{jid: string, hidden: boolean, occupant_count: number}|null>}+ * @returns {Promise<{jid: string, hidden: boolean, occupant_count: number, meeting_id?: string}|null>}*/export async function getRoomState(roomJid) {const res = await fetch(`${BASE}/rooms?jid=${encodeURIComponent(roomJid)}`);@@ -494,3 +494,33 @@ export async function endMeeting(roomJid, token, { silentReconnect = false, omitreturn { status: res.status,body: await res.text() };}++/**+ * Returns the Jibri IQs that reached the MUC component (i.e. passed+ * mod_filter_iq_jibri), as recorded by mod_test_observer. app_data is the+ * value after the sender's `pre-iq/full` hooks (e.g. mod_jibri_session) ran.+ *+ * @returns {Promise<Array<{from: string, to: string, action?: string, recording_mode?: string, app_data?: string}>>}+ */+export async function getJibriIqs() {+ const res = await fetch(`${BASE}/jibri-iqs`);++ if (!res.ok) {+ throw new Error(`GET /jibri-iqs failed: ${res.status}`);+ }++ return res.json();+}++/**+ * Clears the Jibri IQs recorded by mod_test_observer.+ *+ * @returns {Promise<void>}+ */+export async function clearJibriIqs() {+ const res = await fetch(`${BASE}/jibri-iqs`, { method: 'DELETE' });++ if (!res.ok) {+ throw new Error(`DELETE /jibri-iqs failed: ${res.status}`);+ }+}diff --git a/tests/prosody/helpers/xmpp_client.js b/tests/prosody/helpers/xmpp_client.jsindex 28feda84ac..b3be6079bc 100644--- a/tests/prosody/helpers/xmpp_client.js+++ b/tests/prosody/helpers/xmpp_client.js@@ -301,19 +301,42 @@ export async function createXmppClient({ host = 'localhost', domain, params, use* @param {string} roomJid e.g. '[email protected]'* @param {string} action 'start' | 'stop' | 'status' etc.* @param {string} recordingMode 'file' (recording) | 'stream' (livestreaming)+ * @param {object} [opts]+ * @param {string} [opts.appData] Raw app_data attribute value, sent as is.*/- sendJibriIq(roomJid, action, recordingMode) {- // Directed to focus's occupant full JID so `pre-iq/full` fires on the- // main VirtualHost where mod_filter_iq_jibri is loaded.+ sendJibriIq(roomJid, action, recordingMode, { appData } = {}) {+ // `pre-iq/full` (mod_filter_iq_jibri, mod_jibri_session) fires on this+ // client's own VirtualHost. Directed to focus's occupant full JID so+ // the MUC records it and forwards it to the focus.+ return this.sendJibriIqTo(`${roomJid}/focus`, {+ action,+ // eslint-disable-next-line camelcase+ recording_mode: recordingMode,+ // eslint-disable-next-line camelcase+ ...appData === undefined ? {} : { app_data: appData }+ });+ },++ /**+ * Sends a Jibri IQ (http://jitsi.org/protocol/jibri) with the given+ * <jibri/> attributes to any full JID, e.g. the start iq jicofo sends+ * to a jibri, which names the room in its `room` attribute.+ * Fire-and-forget; assert on the recipient's stanza queue (waitForIq).+ * The /jibri-iqs HTTP endpoint only records iqs addressed to an+ * occupant on conference.localhost, so it never sees an iq sent to a+ * jibri's own JID.+ *+ * @param {string} to Destination full JID.+ * @param {object} jibriAttrs Attributes of the <jibri/> element.+ */+ sendJibriIqTo(to, jibriAttrs) {return xmpp.send(xml('iq', { type: 'set',- to: `${roomJid}/focus`,+ to,id: `jibri-${++_counter}` },xml('jibri', {xmlns: 'http://jitsi.org/protocol/jibri',- action,- // eslint-disable-next-line camelcase- recording_mode: recordingMode+ ...jibriAttrs})));diff --git a/tests/prosody/lua/mod_jibri_session_spec.lua b/tests/prosody/lua/mod_jibri_session_spec.luaindex 8cc01f6cba..3402f01342 100644--- a/tests/prosody/lua/mod_jibri_session_spec.lua+++ b/tests/prosody/lua/mod_jibri_session_spec.lua@@ -367,6 +367,76 @@ describe("mod_jibri_session", function()assert.equal(raw, jibri.attr.app_data) -- unchanged: module returned earlyend)+ for _, raw in ipairs({ 'null', '123', '"text"', 'true' }) do+ it("replaces app_data that is valid JSON but not an object (" .. raw .. ")", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ 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, { to = ROOM_FOCUS }), session))+ end)+ 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++ it("leaves non-object app_data unchanged when the iq is not addressed to a room", function()+ mock.room_from_jid = nil+ local jibri = make_jibri_elem({ action = 'start', app_data = 'null' })+ assert.has_no.errors(function()+ hooked_fn(make_event(make_iq(jibri, { to = ROOM_FOCUS }), { jitsi_meet_context_user = { id = 'user-1' } }))+ end)+ assert.equal('null', jibri.attr.app_data)+ end)++ for _, raw in ipairs({ 'null', '123', '"text"', 'true' }) do+ local app_data = '{"other":1,"file_recording_metadata":' .. raw .. '}'++ it("replaces file_recording_metadata that is not an object (" .. raw .. ")", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local jibri = make_jibri_elem({ action = 'start', app_data = app_data })+ local session = { jitsi_meet_context_user = { id = 'user-1' } }+ assert.has_no.errors(function()+ hooked_fn(make_event(make_iq(jibri, { to = ROOM_FOCUS }), session))+ end)+ local data = decoded_app_data(jibri)+ assert.equal(1, data.other)+ assert.equal('meeting-abc', data.file_recording_metadata.conference_details.session_id)+ assert.equal('user-1', data.file_recording_metadata.initiator.id)+ end)++ it("replaces file_recording_metadata that is not an object when the room has no meetingId ("+ .. raw .. ")", function()+ mock.room_from_jid = { _data = {} }+ local jibri = make_jibri_elem({ action = 'start', app_data = app_data })+ local session = { jitsi_meet_context_user = { id = 'user-1' } }+ assert.has_no.errors(function()+ hooked_fn(make_event(make_iq(jibri, { to = ROOM_FOCUS }), session))+ end)+ assert.same({ other = 1, file_recording_metadata = { initiator = { id = 'user-1' } } },+ decoded_app_data(jibri))+ end)+ end++ for _, number in ipairs({ 'NaN', '1e999' }) do+ it("keeps only the metadata it sets when app_data decodes but cannot be encoded again ("+ .. number .. ")", function()+ mock.room_from_jid = { _data = { meetingId = 'meeting-abc' } }+ local raw = '{"x":' .. number .. ',"file_recording_metadata":{"share":true,'+ .. '"initiator":{"id":"other"},"conference_details":{"session_id":"other"}}}'+ 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, { to = ROOM_FOCUS }), session))+ end)+ assert.same({ file_recording_metadata = {+ conference_details = { session_id = 'meeting-abc' },+ initiator = { id = 'user-1' },+ } }, decoded_app_data(jibri))+ end)+ end+end)end) -- mod_jibri_sessiondiff --git a/tests/prosody/mod_jibri_session_spec.js b/tests/prosody/mod_jibri_session_spec.jsnew file mode 100644index 0000000000..f6cc9bc307--- /dev/null+++ b/tests/prosody/mod_jibri_session_spec.js@@ -0,0 +1,238 @@+import assert from 'assert';++import { mintAsapToken } from './helpers/jwt.js';+import { clearJibriIqs, getJibriIqs, getRoomState } from './helpers/test_observer.js';+import { createXmppClient, joinWithFocus } from './helpers/xmpp_client.js';++// mod_jibri_session is loaded on the main VirtualHost. On a client's jibri+// start iq to the focus occupant it writes the room's meetingId to+// app_data.file_recording_metadata.conference_details.session_id and the+// initiator's identity to app_data.file_recording_metadata.initiator. Jicofo+// copies app_data into the iq it sends to jibri.++const CONFERENCE = 'conference.localhost';+const CONFERENCE_SUB = 'conference.subdomain.localhost';++let _roomCounter = 0;+const nextRoom = () => `jibri-session-${++_roomCounter}@${CONFERENCE}`;+const nextSubRoom = () => `jibri-session-sub-${++_roomCounter}@${CONFERENCE_SUB}`;++/**+ * Converts a subdomain room JID ([email protected]) to the+ * internal form Prosody keys rooms by ([sub][email protected]).+ *+ * @param {string} roomJid+ * @returns {string}+ */+function internalRoomJid(roomJid) {+ const [ node, host ] = roomJid.split('@');+ const sub = host.match(/^conference\.([^.]+)\.localhost$/);++ return sub ? `[${sub[1]}]${node}@${CONFERENCE}` : roomJid;+}++/**+ * Polls the test observer until a Jibri IQ has reached the MUC component.+ *+ * @param {number} [timeout=2000]+ * @returns {Promise<object>} the first recorded IQ.+ */+async function waitForJibriIq(timeout = 2000) {+ const deadline = Date.now() + timeout;++ for (;;) {+ const iqs = await getJibriIqs();++ if (iqs.length > 0) {+ return iqs[0];+ }+ if (Date.now() >= deadline) {+ throw new Error('Timeout waiting for the jibri IQ to reach the MUC');+ }+ await new Promise(r => setTimeout(r, 50));+ }+}++describe('mod_jibri_session', () => {++ const clients = [];++ beforeEach(() => clearJibriIqs());++ afterEach(async () => {+ await Promise.all(clients.map(c => c.disconnect()));+ clients.length = 0;+ });++ /**+ * Joins a fresh room as focus, then as a client whose token allows+ * recording, and returns the client, the focus and the room's meetingId.+ *+ * @param {string} room Room JID to join.+ * @returns {Promise<{client: object, focus: object, meetingId: string}>}+ */+ async function setup(room) {+ const focus = await joinWithFocus(room);++ clients.push(focus);++ const token = mintAsapToken({+ room: '*',+ context: {+ user: { id: 'user-1' },+ group: 'group-A',+ features: { recording: true }+ }+ });+ const client = await createXmppClient({ params: { token } });++ clients.push(client);+ await client.joinRoom(room);++ const state = await getRoomState(internalRoomJid(room));++ assert.ok(state?.meeting_id, `room ${room} must have a meetingId`);++ return { client,+ focus,+ meetingId: state.meeting_id };+ }++ // ─── client → jicofo ─────────────────────────────────────────────────────++ describe('client-to-jicofo pass (iq to the focus occupant)', () => {++ it('adds the room meetingId as session_id and the token identity as initiator', async () => {+ const room = nextRoom();+ const { client, meetingId } = await setup(room);++ await client.sendJibriIq(room, 'start', 'file');+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ // eslint-disable-next-line camelcase+ assert.deepStrictEqual(metadata.conference_details, { session_id: meetingId });+ assert.deepStrictEqual(metadata.initiator, { id: 'user-1',+ group: 'group-A' });+ });++ it('keeps the other app_data the client sent', async () => {+ const room = nextRoom();+ const { client, meetingId } = await setup(room);++ await client.sendJibriIq(room, 'start', 'file', {+ appData: JSON.stringify({+ // eslint-disable-next-line camelcase+ file_recording_metadata: { share: true }+ })+ });+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ assert.strictEqual(metadata.share, true);+ assert.strictEqual(metadata.conference_details.session_id, meetingId);+ });++ it('replaces conference_details sent by the client', async () => {+ const room = nextRoom();+ const { client, meetingId } = await setup(room);++ await client.sendJibriIq(room, 'start', 'file', {+ appData: JSON.stringify({+ // eslint-disable-next-line camelcase+ file_recording_metadata: { conference_details: { session_id: 'spoofed' } }+ })+ });+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ // eslint-disable-next-line camelcase+ assert.deepStrictEqual(metadata.conference_details, { session_id: meetingId });+ });++ it('replaces app_data that is valid JSON but not an object', async () => {+ const room = nextRoom();+ const { client, meetingId } = await setup(room);++ // Before the fix the hook threw, and prosody dropped the iq, so+ // waitForJibriIq timed out.+ await client.sendJibriIq(room, 'start', 'file', { appData: 'null' });+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ assert.strictEqual(metadata.conference_details.session_id, meetingId);+ });++ it('replaces file_recording_metadata that is not an object', async () => {+ const room = nextRoom();+ const { client, meetingId } = await setup(room);++ await client.sendJibriIq(room, 'start', 'file', {+ appData: JSON.stringify({+ // eslint-disable-next-line camelcase+ file_recording_metadata: null,+ other: 1+ })+ });+ const appData = JSON.parse((await waitForJibriIq()).app_data);++ assert.strictEqual(appData.other, 1);+ assert.strictEqual(appData.file_recording_metadata.conference_details.session_id, meetingId);+ assert.strictEqual(appData.file_recording_metadata.initiator.id, 'user-1');+ });++ it('uses the meetingId of a subdomain room', async () => {+ const room = nextSubRoom();+ const { client, meetingId } = await setup(room);++ await client.sendJibriIq(room, 'start', 'file');+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ assert.strictEqual(metadata.conference_details.session_id, meetingId);+ });++ it('replaces the initiator sent by the client', async () => {+ const room = nextRoom();+ const { client } = await setup(room);++ await client.sendJibriIq(room, 'start', 'file', {+ appData: JSON.stringify({+ // eslint-disable-next-line camelcase+ file_recording_metadata: { initiator: { id: 'other' } }+ })+ });+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ assert.deepStrictEqual(metadata.initiator, { id: 'user-1',+ group: 'group-A' });+ });++ it('takes the room from the iq address and ignores a room attribute', async () => {+ const otherRoom = nextRoom();++ clients.push(await joinWithFocus(otherRoom));++ const otherState = await getRoomState(otherRoom);+ const room = nextRoom();+ const { client, meetingId } = await setup(room);++ assert.notStrictEqual(otherState?.meeting_id, meetingId);++ await client.sendJibriIqTo(`${room}/focus`, {+ action: 'start',+ // eslint-disable-next-line camelcase+ recording_mode: 'file',+ room: otherRoom+ });+ const metadata = JSON.parse((await waitForJibriIq()).app_data).file_recording_metadata;++ assert.strictEqual(metadata.conference_details.session_id, meetingId);+ assert.strictEqual(metadata.initiator.id, 'user-1');+ });++ it('does not touch app_data of a stop iq', async () => {+ const room = nextRoom();+ const { client } = await setup(room);++ await client.sendJibriIq(room, 'stop', 'file');++ assert.strictEqual((await waitForJibriIq()).app_data, undefined);+ });+ });+});