97 lines
6.9 KiB
JavaScript
97 lines
6.9 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { validateRequest, RENDERER_COMMANDS } = require('../app/main/ipc-validate');
|
|
|
|
test('renderer allowlist excludes main-owned commands', () => {
|
|
for (const cmd of ['shutdown', 'audio_frame', 'audio_end', 'eval', '__proto__', 'constructor']) {
|
|
assert.throws(() => validateRequest(cmd, {}), (err) => err.code === 'unknown_command', cmd);
|
|
}
|
|
assert.ok(RENDERER_COMMANDS.includes('stream_signal'));
|
|
});
|
|
|
|
test('unknown argument keys and wrong types are rejected', () => {
|
|
assert.throws(() => validateRequest('connect', { address: 'h', path: '/etc/passwd' }));
|
|
assert.throws(() => validateRequest('connect', { address: 5 }));
|
|
assert.throws(() => validateRequest('connect', { address: 'a\nb' }));
|
|
for (const nickname of ['', null, 5, 'a\nb', 'x'.repeat(65)]) {
|
|
assert.throws(() => validateRequest('connect', { address: 'h', nickname }), error => error.code === 'bad_request');
|
|
}
|
|
assert.throws(() => validateRequest('join_channel', { channel_id: -1 }));
|
|
assert.throws(() => validateRequest('set_audio', { input_muted: 'yes' }));
|
|
assert.deepEqual(validateRequest('connect', { address: '10.10.10.160:9987' }), { address: '10.10.10.160:9987' });
|
|
});
|
|
|
|
test('send_text requires client_id and captured UID exactly for private messages', () => {
|
|
assert.throws(() => validateRequest('send_text', { target: 'client', message: 'hi' }));
|
|
assert.throws(() => validateRequest('send_text', { target: 'channel', client_id: 3, message: 'hi' }));
|
|
assert.deepEqual(validateRequest('send_text', { target: 'client', client_id: 3, expected_client_uid: 'fixture=', message: 'hi\nthere' }), { target: 'client', client_id: 3, expected_client_uid: 'fixture=', message: 'hi\nthere' });
|
|
assert.throws(() => validateRequest('send_text', { target: 'client', client_id: 3, message: 'hi' }));
|
|
assert.throws(() => validateRequest('send_text', { target: 'channel', expected_client_uid: 'fixture=', message: 'hi' }));
|
|
assert.throws(() => validateRequest('send_text', { target: 'server', message: 'x'.repeat(8193) }));
|
|
});
|
|
|
|
test('stream_signal carries viewer answers, ICE and publisher reconnect offers only', () => {
|
|
const base = { stream_id: 'c4', client_id: 16 };
|
|
assert.ok(validateRequest('stream_signal', { ...base, json: { cmd: 'answer', args: { answer: 'v=0\r\n' } } }));
|
|
assert.ok(validateRequest('stream_signal', { ...base, json: { cmd: 'iceCandidate', args: { mLine: 0, mid: '0', sdp: 'candidate:1' } } }));
|
|
assert.ok(validateRequest('stream_signal', { ...base, json: { cmd: 'reconnectOffer', args: { offer: 'v=0\r\n' } } }));
|
|
assert.throws(() => validateRequest('stream_signal', { ...base, json: { cmd: 'offer', args: { offer: 'x' } } }));
|
|
assert.throws(() => validateRequest('stream_signal', { ...base, json: { cmd: 'answer', args: { answer: 'x', extra: 1 } } }));
|
|
assert.throws(() => validateRequest('stream_signal', { ...base, json: { cmd: 'answer', args: { answer: 'x'.repeat(61 * 1024) } } }));
|
|
});
|
|
|
|
test('publisher bridge restricts metadata, SDP decisions and fixed eviction reason', () => {
|
|
const metadata = { name: 'QA | stream', type: 3, accessibility: 3, mode: 1, bitrate: 4500, viewer_limit: 0, audio: false };
|
|
assert.deepEqual(validateRequest('stream_setup', metadata), metadata);
|
|
assert.deepEqual(validateRequest('stream_update', { stream_id: 's1', ...metadata }), { stream_id: 's1', ...metadata });
|
|
for (const invalid of [{ ...metadata, mode: 2 }, { ...metadata, viewer_limit: -1 }, { ...metadata, bitrate: 99 }, { ...metadata, stream_id: 's1' }]) {
|
|
assert.throws(() => validateRequest('stream_setup', invalid));
|
|
}
|
|
assert.throws(() => validateRequest('stream_update', metadata));
|
|
const response = { stream_id: 's1', client_id: 5, decision: true, offer: 'v=0\r\n', message: '' };
|
|
assert.deepEqual(validateRequest('stream_respond', response), response);
|
|
assert.throws(() => validateRequest('stream_respond', { ...response, offer: '' }));
|
|
assert.throws(() => validateRequest('stream_respond', { ...response, decision: false }));
|
|
assert.throws(() => validateRequest('stream_remove', { stream_id: 's1', client_id: 5, reason: 5 }));
|
|
assert.throws(() => validateRequest('stream_remove', { stream_id: 's1', client_id: 0 }));
|
|
});
|
|
|
|
test('client menus bind identity, generation and action-specific argument shapes', () => {
|
|
const target = { client_id: 42, uid: 'controlled-fixture-uid', generation: 7 };
|
|
assert.deepEqual(validateRequest('client_context', { client_id: 42, uid: target.uid }), { client_id: 42, uid: target.uid });
|
|
const valid = [
|
|
{ action: 'poke', message: 'a b|c' }, { action: 'connection_info' }, { action: 'commander', enabled: true },
|
|
{ action: 'server_group', group_id: 6, enabled: false }, { action: 'channel_group', group_id: 7, channel_id: 1 },
|
|
{ action: 'client_permissions' }, { action: 'channel_permissions', channel_id: 1 },
|
|
{ action: 'set_client_permission', permission_id: 42, value: null, skip: false },
|
|
{ action: 'set_channel_permission', channel_id: 1, permission_id: 42, value: -1 },
|
|
{ action: 'kick_channel', reason: '' }, { action: 'kick_server', reason: 'fixture' },
|
|
{ action: 'ban', reason: 'fixture', duration: 0 }, { action: 'volume', db: -8, muted: true },
|
|
];
|
|
for (const action of valid) assert.deepEqual(validateRequest('client_action', { ...target, ...action }), { ...target, ...action });
|
|
for (const args of [
|
|
{ ...target, action: 'connection_info', path: '/etc/passwd' },
|
|
{ ...target, action: '__proto__' }, { ...target, action: 'volume', db: NaN, muted: false },
|
|
{ ...target, action: 'volume', db: 10.5, muted: false }, { ...target, action: 'volume', db: 1 },
|
|
{ ...target, action: 'volume', db: -200.5, muted: false }, { ...target, action: 'volume', db: 0.1, muted: false },
|
|
{ ...target, action: 'volume', value: 1, muted: false },
|
|
{ ...target, action: 'set_client_permission', permission_id: 42, skip: false },
|
|
{ ...target, action: 'poke', message: '\n' }, { ...target, action: 'ban', reason: '', duration: -1 },
|
|
{ ...target, action: 'connection_info', client_id: 65536 }, { ...target, action: 'connection_info', generation: 0 },
|
|
]) assert.throws(() => validateRequest('client_action', args));
|
|
});
|
|
|
|
test('avatar IPC accepts only bounded base64 PNG/JPEG bytes, never paths', () => {
|
|
const target = { client_id: 42, uid: 'fixture', generation: 1, action: 'avatar' };
|
|
const png = Buffer.from([137,80,78,71,13,10,26,10]).toString('base64');
|
|
assert.equal(validateRequest('client_action', { ...target, data: png }).data, png);
|
|
for (const data of ['not base64!', Buffer.from('<svg/>').toString('base64'), png + 'AAAA']) {
|
|
assert.throws(() => validateRequest('client_action', { ...target, data }));
|
|
}
|
|
const huge = Buffer.alloc(128 * 1024 + 1); Buffer.from([255,216,255]).copy(huge);
|
|
assert.throws(() => validateRequest('client_action', { ...target, data: huge.toString('base64') }));
|
|
assert.throws(() => validateRequest('client_action', { ...target, path: 'avatar.png' }));
|
|
});
|