148 lines
5.5 KiB
JavaScript
148 lines
5.5 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Allowlist + argument-shape validation for renderer → core requests.
|
|
* The renderer may only call these bridge commands; `shutdown`, `audio_frame` and `audio_end`
|
|
* are main-owned (audio has its own binary IPC channel). Unknown keys are rejected like the core does.
|
|
*/
|
|
|
|
class ValidationError extends Error {
|
|
constructor(message) {
|
|
super(message);
|
|
this.code = 'bad_request';
|
|
}
|
|
}
|
|
|
|
const CONTROL_CHARS = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/;
|
|
|
|
const str = (min, max, { multiline = false, maxBytes } = {}) => (value, key) => {
|
|
if (typeof value !== 'string') throw new ValidationError(`${key}: expected a string`);
|
|
if (value.length < min || value.length > max) throw new ValidationError(`${key}: invalid length`);
|
|
if (maxBytes !== undefined && Buffer.byteLength(value, 'utf8') > maxBytes) throw new ValidationError(`${key}: too long`);
|
|
if (multiline ? CONTROL_CHARS.test(value) : /[\u0000-\u001f\u007f]/.test(value)) {
|
|
throw new ValidationError(`${key}: control characters are not allowed`);
|
|
}
|
|
return value;
|
|
};
|
|
|
|
const posInt = (value, key) => {
|
|
if (!Number.isSafeInteger(value) || value < 0) throw new ValidationError(`${key}: expected a non-negative integer`);
|
|
return value;
|
|
};
|
|
|
|
const bool = (value, key) => {
|
|
if (typeof value !== 'boolean') throw new ValidationError(`${key}: expected true/false`);
|
|
return value;
|
|
};
|
|
|
|
const oneOf = (...allowed) => (value, key) => {
|
|
if (!allowed.includes(value)) throw new ValidationError(`${key}: value not allowed`);
|
|
return value;
|
|
};
|
|
|
|
const optional = (check) => {
|
|
const wrapped = (value, key) => check(value, key);
|
|
wrapped.optional = true;
|
|
return wrapped;
|
|
};
|
|
|
|
function isPlainObject(value) {
|
|
return value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype;
|
|
}
|
|
|
|
function shape(spec) {
|
|
return (args) => {
|
|
if (args === undefined || args === null) args = {};
|
|
if (!isPlainObject(args)) throw new ValidationError('args: expected an object');
|
|
for (const key of Object.keys(args)) {
|
|
if (!Object.prototype.hasOwnProperty.call(spec, key)) throw new ValidationError(`${key}: unknown argument`);
|
|
}
|
|
const out = {};
|
|
for (const [key, check] of Object.entries(spec)) {
|
|
const value = args[key];
|
|
if (value === undefined) {
|
|
if (!check.optional) throw new ValidationError(`${key}: required argument`);
|
|
continue;
|
|
}
|
|
out[key] = check(value, key);
|
|
}
|
|
return out;
|
|
};
|
|
}
|
|
|
|
const streamId = str(1, 256);
|
|
const SIGNAL_COMMANDS = new Set(['answer', 'iceCandidate']);
|
|
|
|
function signalJson(value) {
|
|
if (!isPlainObject(value)) throw new ValidationError('json: expected an object');
|
|
const keys = Object.keys(value);
|
|
if (keys.length !== 2 || !keys.includes('cmd') || !keys.includes('args')) throw new ValidationError('json: expected exactly the fields cmd and args');
|
|
if (!SIGNAL_COMMANDS.has(value.cmd)) throw new ValidationError('json.cmd: signalling command not allowed');
|
|
const args = value.args;
|
|
if (!isPlainObject(args)) throw new ValidationError('json.args: expected an object');
|
|
if (value.cmd === 'answer') {
|
|
shape({ answer: str(1, 60000, { multiline: true }) })(args);
|
|
} else {
|
|
shape({
|
|
mLine: posInt,
|
|
mid: str(0, 64),
|
|
sdp: str(0, 2048),
|
|
})(args);
|
|
}
|
|
const serialized = JSON.stringify(value);
|
|
if (Buffer.byteLength(serialized, 'utf8') > 60 * 1024) throw new ValidationError('json: larger than 60 KiB');
|
|
return JSON.parse(serialized);
|
|
}
|
|
|
|
const COMMANDS = {
|
|
profile_get: shape({}),
|
|
profile_create: shape({ nickname: str(1, 64) }),
|
|
rename: shape({ nickname: str(1, 64) }),
|
|
connect: shape({
|
|
address: str(1, 255),
|
|
server_password: optional(str(0, 255)),
|
|
channel: optional(str(1, 512)),
|
|
channel_password: optional(str(0, 255)),
|
|
}),
|
|
disconnect: shape({ message: optional(str(0, 80)) }),
|
|
join_channel: shape({ channel_id: posInt, password: optional(str(0, 255)) }),
|
|
send_text: (args) => {
|
|
const out = shape({
|
|
target: oneOf('channel', 'server', 'client'),
|
|
client_id: optional(posInt),
|
|
message: str(1, 8192, { multiline: true, maxBytes: 8192 }),
|
|
})(args);
|
|
if ((out.target === 'client') !== (out.client_id !== undefined)) {
|
|
throw new ValidationError('client_id: required for private messages only');
|
|
}
|
|
return out;
|
|
},
|
|
set_audio: shape({ input_muted: optional(bool), output_muted: optional(bool) }),
|
|
set_transmit: shape({ mode: oneOf('direct', 'vad'), sensitivity: optional(oneOf('high', 'normal', 'low')) }),
|
|
stream_list: shape({ client_id: optional(posInt) }),
|
|
stream_join: shape({ stream_id: streamId, client_id: posInt, message: optional(str(0, 200)) }),
|
|
stream_signal: shape({ stream_id: streamId, client_id: posInt, json: signalJson }),
|
|
stream_leave: shape({ stream_id: streamId, client_id: posInt }),
|
|
fetch_icon: shape({
|
|
icon_id: (v, k) => {
|
|
if (!Number.isSafeInteger(v) || v <= 0 || v > 0xffffffff) throw new ValidationError(`${k}: expected an icon id`);
|
|
return v;
|
|
},
|
|
}),
|
|
fetch_avatar: shape({ client_id: posInt }),
|
|
fetch_banner: shape({ channel_id: posInt }),
|
|
fetch_server_banner: shape({}),
|
|
};
|
|
|
|
/** Returns sanitized args or throws ValidationError. */
|
|
function validateRequest(cmd, args) {
|
|
if (typeof cmd !== 'string' || !Object.prototype.hasOwnProperty.call(COMMANDS, cmd)) {
|
|
const err = new ValidationError('Command not allowed');
|
|
err.code = 'unknown_command';
|
|
throw err;
|
|
}
|
|
return COMMANDS[cmd](args);
|
|
}
|
|
|
|
module.exports = { validateRequest, ValidationError, RENDERER_COMMANDS: Object.keys(COMMANDS) };
|