168 lines
11 KiB
JavaScript
168 lines
11 KiB
JavaScript
'use strict';
|
|
/** Renderer allowlist for core/src/admin.rs and admin_extra.rs; server permissions remain authoritative. */
|
|
function createAdminValidators({shape,bool,boundedInt,optional,oneOf,isPlainObject,ValidationError}) {
|
|
const bad = message => { throw new ValidationError(message); };
|
|
const text = (min,max) => (v,k) => {
|
|
if (typeof v !== 'string' || [...v].length < min || [...v].length > max || (max===8192 ? /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/ : /[\u0000-\u001f\u007f-\u009f]/).test(v)) bad(k+': invalid text');
|
|
return v;
|
|
};
|
|
const float=(v,k)=>{if(typeof v!=='number'||!Number.isFinite(v)||Math.abs(v)>3.4028234663852886e38)bad(k+': expected finite f32');return v;};
|
|
const positive=boundedInt(1,Number.MAX_SAFE_INTEGER), zero=boundedInt(0,Number.MAX_SAFE_INTEGER), generation=positive;
|
|
const properties=(spec,requiredName=false)=>(v)=>{
|
|
if(!isPlainObject(v)||Object.keys(v).length===0)bad('properties: nonempty object required');
|
|
const out=shape(Object.fromEntries(Object.entries(spec).map(([k,check])=>[k,optional(check)])))(v);
|
|
if(requiredName&&!out.channel_name)bad('channel_name: required');
|
|
if(out.channel_flag_permanent&&out.channel_flag_semi_permanent)bad('channel cannot be permanent and semi-permanent');
|
|
return out;
|
|
};
|
|
const CHANNEL_CREATE = {
|
|
"channel_name": text(1,40),
|
|
"channel_topic": text(0,255),
|
|
"channel_description": text(0,8192),
|
|
"channel_password": text(0,1024),
|
|
"channel_codec": boundedInt(0,5),
|
|
"channel_codec_quality": boundedInt(0,10),
|
|
"channel_maxclients": boundedInt(-1,2147483647),
|
|
"channel_maxfamilyclients": boundedInt(-1,2147483647),
|
|
"channel_order": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"channel_codec_is_unencrypted": bool,
|
|
"channel_delete_delay": boundedInt(0,4294967295),
|
|
"channel_flag_maxclients_unlimited": bool,
|
|
"channel_flag_maxfamilyclients_unlimited": bool,
|
|
"channel_flag_maxfamilyclients_inherited": bool,
|
|
"channel_name_phonetic": text(0,255),
|
|
"channel_flag_permanent": bool,
|
|
"channel_flag_semi_permanent": bool,
|
|
"channel_flag_default": bool,
|
|
};
|
|
const CHANNEL_EDIT = {
|
|
"channel_order": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"channel_name": text(1,40),
|
|
"channel_topic": text(0,255),
|
|
"channel_flag_default": bool,
|
|
"channel_password": text(0,1024),
|
|
"channel_flag_permanent": bool,
|
|
"channel_flag_semi_permanent": bool,
|
|
"channel_codec": boundedInt(0,5),
|
|
"channel_codec_quality": boundedInt(0,10),
|
|
"channel_needed_talk_power": boundedInt(-2147483648,2147483647),
|
|
"channel_maxclients": boundedInt(-1,2147483647),
|
|
"channel_maxfamilyclients": boundedInt(-1,2147483647),
|
|
"channel_codec_latency_factor": boundedInt(1,10),
|
|
"channel_codec_is_unencrypted": bool,
|
|
"channel_delete_delay": boundedInt(0,4294967295),
|
|
"channel_flag_maxclients_unlimited": bool,
|
|
"channel_flag_maxfamilyclients_unlimited": bool,
|
|
"channel_flag_maxfamilyclients_inherited": bool,
|
|
"channel_name_phonetic": text(0,255),
|
|
"channel_description": text(0,8192),
|
|
"channel_banner_gfx_url": text(0,1024),
|
|
"channel_banner_mode": boundedInt(0,2),
|
|
};
|
|
const SERVER_EDIT = {
|
|
"virtualserver_name": text(1,64),
|
|
"virtualserver_sfu_endpoint": text(0,1024),
|
|
"virtualserver_storage_quota": boundedInt(-1,4294967295),
|
|
"virtualserver_welcomemessage": text(0,8192),
|
|
"virtualserver_maxclients": boundedInt(0,65535),
|
|
"virtualserver_password": text(0,1024),
|
|
"virtualserver_hostmessage": text(0,8192),
|
|
"virtualserver_hostmessage_mode": boundedInt(0,3),
|
|
"virtualserver_hostbanner_url": text(0,1024),
|
|
"virtualserver_hostbanner_gfx_url": text(0,1024),
|
|
"virtualserver_hostbanner_gfx_interval": boundedInt(0,4294967295),
|
|
"virtualserver_hostbutton_tooltip": text(0,1024),
|
|
"virtualserver_hostbutton_url": text(0,1024),
|
|
"virtualserver_hostbutton_gfx_url": text(0,1024),
|
|
"virtualserver_icon_id": boundedInt(0,4294967295),
|
|
"virtualserver_reserved_slots": boundedInt(0,65535),
|
|
"virtualserver_hostbanner_mode": boundedInt(0,2),
|
|
"virtualserver_nickname": text(0,1024),
|
|
"virtualserver_max_download_total_bandwidth": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_max_upload_total_bandwidth": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_download_quota": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_upload_quota": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_antiflood_points_tick_reduce": boundedInt(0,4294967295),
|
|
"virtualserver_antiflood_points_needed_command_block": boundedInt(0,4294967295),
|
|
"virtualserver_antiflood_points_needed_ip_block": boundedInt(0,4294967295),
|
|
"virtualserver_codec_encryption_mode": boundedInt(0,2),
|
|
"virtualserver_needed_identity_security_level": boundedInt(0,255),
|
|
"virtualserver_default_server_group": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_default_channel_group": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_default_channel_admin_group": boundedInt(0,Number.MAX_SAFE_INTEGER),
|
|
"virtualserver_complain_autoban_count": boundedInt(0,4294967295),
|
|
"virtualserver_complain_autoban_time": boundedInt(0,4294967295),
|
|
"virtualserver_complain_remove_time": boundedInt(0,4294967295),
|
|
"virtualserver_min_clients_in_channel_before_forced_silence": boundedInt(0,4294967295),
|
|
"virtualserver_priority_speaker_dimm_modificator": float,
|
|
"virtualserver_name_phonetic": text(0,1024),
|
|
"virtualserver_channel_temp_delete_delay_default": boundedInt(0,4294967295),
|
|
"virtualserver_weblist_enabled": bool,
|
|
"virtualserver_log_client": bool,
|
|
"virtualserver_log_query": bool,
|
|
"virtualserver_log_channel": bool,
|
|
"virtualserver_log_permissions": bool,
|
|
"virtualserver_log_server": bool,
|
|
"virtualserver_log_filetransfer": bool,
|
|
};
|
|
|
|
const confirm=text(0,1024), uid=text(1,128), groupKind=oneOf('server','channel');
|
|
const permission=(server)=>(value)=>{
|
|
if(!Array.isArray(value)||value.length<1||value.length>512)bad('permissions: expected 1..512 rows');
|
|
return value.map(v=>{
|
|
const p=shape({id:text(1,128),value:boundedInt(-2147483648,2147483647),negate:optional(bool),skip:optional(bool)})(v);
|
|
if(!/^[A-Za-z0-9_]+$/.test(p.id)||(/^\d+$/.test(p.id)&&(!Number.isSafeInteger(Number(p.id))||Number(p.id)<1||Number(p.id)>4294967295)))bad('permission: invalid identifier');
|
|
if(!server&&(p.negate||p.skip))bad('permission: negate/skip require server group');return p;
|
|
});
|
|
};
|
|
// Explicit-permission removal (servergroupdelperm / channelgroupdelperm / channeldelperm): identifiers only.
|
|
const permissionIds=(value)=>{
|
|
if(!Array.isArray(value)||value.length<1||value.length>512)bad('permissions: expected 1..512 identifiers');
|
|
return value.map(v=>{
|
|
const id=text(1,128)(v,'permission');
|
|
if(!/^[A-Za-z0-9_]+$/.test(id)||(/^\d+$/.test(id)&&(!Number.isSafeInteger(Number(id))||Number(id)<1||Number(id)>4294967295)))bad('permission: invalid identifier');
|
|
return id;
|
|
});
|
|
};
|
|
const commands={
|
|
admin_context:shape({}),
|
|
channel_create:shape({generation,parent_id:zero,properties:properties(CHANNEL_CREATE,true)}),
|
|
channel_edit:shape({generation,channel_id:positive,properties:properties(CHANNEL_EDIT)}),
|
|
channel_move:(a)=>{const o=shape({generation,channel_id:positive,parent_id:zero,order:optional(zero)})(a);if(o.channel_id===o.parent_id)bad('channel cannot parent itself');return o;},
|
|
channel_delete:shape({generation,channel_id:positive,force:bool,confirm}),
|
|
channel_subscribe:shape({channel_id:positive,subscribe:bool}), channel_subscribe_all:shape({subscribe:bool}),
|
|
channel_description:shape({channel_id:positive}),channel_info:shape({channel_id:positive}),channel_list:shape({}),
|
|
client_info:shape({client_id:boundedInt(1,65535),uid}),
|
|
set_away:(a)=>{const o=shape({away:bool,message:optional(text(0,80))})(a);if(!o.away&&o.message!==undefined)bad('message requires away');return o;},
|
|
talk_request:(a)=>{const o=shape({request:bool,message:optional(text(0,50))})(a);if(!o.request&&o.message!==undefined)bad('message requires request');return o;},
|
|
talk_power:shape({generation,client_id:boundedInt(1,65535),uid,grant:bool}),
|
|
ban_list:shape({}),ban_add:(a)=>{const o=shape({generation,ip:optional(text(0,255)),name:optional(text(0,255)),uid:optional(text(0,255)),duration_s:boundedInt(0,4294967295),reason:optional(text(0,512))})(a);if(!o.ip&&!o.name&&!o.uid)bad('ban needs ip, name or uid');return o;},
|
|
ban_delete:shape({generation,ban_id:positive,confirm}),ban_delete_all:shape({generation,confirm}),
|
|
privilege_key_list:shape({}),privilege_key_add:(a)=>{
|
|
const o=shape({generation,kind:oneOf('server_group','channel_group'),group_id:positive,channel_id:optional(positive),description:optional(text(0,255))})(a);
|
|
if((o.kind==='channel_group')!==(o.channel_id!==undefined))bad('channel_id required only for channel_group');return o;
|
|
},
|
|
privilege_key_delete:shape({generation,token:text(1,255),confirm}),privilege_key_use:shape({generation,token:text(1,255)}),
|
|
temp_password_list:shape({}),temp_password_add:(a)=>{const o=shape({generation,password:text(1,255),description:optional(text(0,255)),duration_s:boundedInt(1,4294967295),channel_id:optional(zero),channel_password:optional(text(0,255))})(a);if(!o.channel_id&&o.channel_password)bad('channel_password needs channel_id');return o;},
|
|
temp_password_delete:shape({generation,password:text(1,255),confirm}),
|
|
server_info:shape({}),server_edit:shape({generation,confirm,properties:properties(SERVER_EDIT)}),
|
|
server_group_perm_list:shape({group_id:positive}),channel_group_perm_list:shape({group_id:positive}),channel_perm_list:shape({channel_id:positive}),server_group_client_list:shape({group_id:positive}),
|
|
server_group_list:shape({}),channel_group_list:shape({}),
|
|
server_log:shape({lines:boundedInt(1,1000),reverse:bool,instance:bool,begin_pos:zero}),
|
|
client_database_list:shape({start:boundedInt(0,4294967295),duration:boundedInt(1,200)}),
|
|
client_database_find:shape({pattern:text(1,255),uid:bool}),client_database_id:shape({uid}),
|
|
client_database_delete:shape({generation,client_database_id:positive,confirm}),
|
|
group_add:shape({generation,kind:groupKind,name:text(1,128),type:boundedInt(0,2)}),
|
|
group_rename:shape({generation,kind:groupKind,group_id:positive,name:text(1,128)}),
|
|
group_set_permissions:(a)=>shape({generation,kind:groupKind,group_id:positive,permissions:permission(a?.kind==='server')})(a),
|
|
channel_set_permissions:shape({generation,channel_id:positive,permissions:permission(false)}),
|
|
group_delete:(a)=>{const o=shape({generation,kind:groupKind,group_id:positive,force:bool,confirm})(a);if(o.confirm!==String(o.group_id))bad('confirm must be the group ID');return o;},
|
|
group_delete_permissions:shape({generation,kind:groupKind,group_id:positive,permissions:permissionIds}),
|
|
channel_delete_permissions:shape({generation,channel_id:positive,permissions:permissionIds}),
|
|
server_group_add_client:shape({generation,group_id:positive,client_database_id:positive}),
|
|
server_group_member_add_uid:shape({generation,group_id:positive,uid}),
|
|
};
|
|
return commands;
|
|
}
|
|
module.exports={createAdminValidators};
|