Enforce safe integer bounds on administrative writes and targets

This commit is contained in:
omar committed 2026-11-27 18:53:43 +03:00
1 parent 1213468924
commit d1b611a911
6 files changed
+82 -16

No files matched your search

+25 -13
View File
@@ -24,6 +24,8 @@ pub mod extra;
/// Upper bound for rows collected for one list request.
pub const MAX_ROWS: usize = 10_000;
/// Inclusive limit for integer arguments shared with the reference command interface.
pub const MAX_SAFE_INTEGER: u64 = (1 << 53) - 1;
const MAX_TEXT: usize = 8192;
const MAX_SHORT: usize = 1024;
@@ -89,7 +91,7 @@ const CHANNEL_CREATE: &[(&str, Kind)] = &[
("channel_codec_quality", Kind::Uint(10)),
("channel_maxclients", Kind::Int(-1, i32::MAX as i64)),
("channel_maxfamilyclients", Kind::Int(-1, i32::MAX as i64)),
("channel_order", Kind::Uint(u64::MAX)),
("channel_order", Kind::Uint(MAX_SAFE_INTEGER)),
("channel_codec_is_unencrypted", Kind::Bool),
("channel_delete_delay", Kind::Uint(u32::MAX as u64)),
("channel_flag_maxclients_unlimited", Kind::Bool),
@@ -102,7 +104,7 @@ const CHANNEL_CREATE: &[(&str, Kind)] = &[
];
/// Fields of `channeledit` (`ChannelEdit` declaration, minus `cid`).
const CHANNEL_EDIT: &[(&str, Kind)] = &[
("channel_order", Kind::Uint(u64::MAX)),
("channel_order", Kind::Uint(MAX_SAFE_INTEGER)),
("channel_name", CHANNEL_NAME),
("channel_topic", Kind::Str(255)),
("channel_flag_default", Kind::Bool),
@@ -147,18 +149,18 @@ const SERVER_EDIT: &[(&str, Kind)] = &[
("virtualserver_reserved_slots", Kind::Uint(u16::MAX as u64)),
("virtualserver_hostbanner_mode", Kind::Uint(2)),
("virtualserver_nickname", Kind::Str(MAX_SHORT)),
("virtualserver_max_download_total_bandwidth", Kind::Uint(u64::MAX)),
("virtualserver_max_upload_total_bandwidth", Kind::Uint(u64::MAX)),
("virtualserver_download_quota", Kind::Uint(u64::MAX)),
("virtualserver_upload_quota", Kind::Uint(u64::MAX)),
("virtualserver_max_download_total_bandwidth", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_max_upload_total_bandwidth", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_download_quota", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_upload_quota", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_antiflood_points_tick_reduce", Kind::Uint(u32::MAX as u64)),
("virtualserver_antiflood_points_needed_command_block", Kind::Uint(u32::MAX as u64)),
("virtualserver_antiflood_points_needed_ip_block", Kind::Uint(u32::MAX as u64)),
("virtualserver_codec_encryption_mode", Kind::Uint(2)),
("virtualserver_needed_identity_security_level", Kind::Uint(u8::MAX as u64)),
("virtualserver_default_server_group", Kind::Uint(u64::MAX)),
("virtualserver_default_channel_group", Kind::Uint(u64::MAX)),
("virtualserver_default_channel_admin_group", Kind::Uint(u64::MAX)),
("virtualserver_default_server_group", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_default_channel_group", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_default_channel_admin_group", Kind::Uint(MAX_SAFE_INTEGER)),
("virtualserver_complain_autoban_count", Kind::Uint(u32::MAX as u64)),
("virtualserver_complain_autoban_time", Kind::Uint(u32::MAX as u64)),
("virtualserver_complain_remove_time", Kind::Uint(u32::MAX as u64)),
@@ -353,12 +355,16 @@ fn check_opt(field: &str, value: &Option<String>, max: usize) -> Result<(), Erro
Ok(())
}
fn positive(field: &str, v: u64) -> Result<u64, ErrorObj> {
if v == 0 { Err(bad(format!("{field} must be positive"))) } else { Ok(v) }
fn nonnegative(field: &str, v: u64) -> Result<(), ErrorObj> {
if v > MAX_SAFE_INTEGER { Err(bad(format!("{field} exceeds the maximum safe integer"))) } else { Ok(()) }
}
fn positive(field: &str, v: u64) -> Result<(), ErrorObj> {
if v == 0 { Err(bad(format!("{field} must be positive"))) } else { nonnegative(field, v) }
}
pub(crate) fn generation(v: u64) -> Result<u64, ErrorObj> {
if !(1..=9_007_199_254_740_991).contains(&v) { return Err(bad("generation must be a positive safe integer")); }
if !(1..=MAX_SAFE_INTEGER).contains(&v) { return Err(bad("generation must be a positive safe integer")); }
Ok(v)
}
@@ -615,13 +621,18 @@ pub(crate) fn validate(command: &AdminCommand) -> Result<(), ErrorObj> {
if let C::Extra(c) = command { return c.validate(); }
if let Some(g) = command.generation() { generation(g)?; }
match command {
C::ChannelCreate { properties, .. } => properties.check_scope(PropertyScope::ChannelCreate)?,
C::ChannelCreate { properties, parent_id, .. } => {
properties.check_scope(PropertyScope::ChannelCreate)?;
nonnegative("parent_id", *parent_id)?;
}
C::ChannelEdit { channel_id, properties, .. } => {
properties.check_scope(PropertyScope::ChannelEdit)?;
positive("channel_id", *channel_id)?;
}
C::ServerEdit { properties, .. } => properties.check_scope(PropertyScope::ServerEdit)?,
C::ChannelMove { channel_id, parent_id, order, .. } => {
nonnegative("parent_id", *parent_id)?;
if let Some(order) = order { nonnegative("order", *order)?; }
if parent_id == channel_id { return Err(bad("a channel cannot be its own parent")); }
if order.as_ref() == Some(channel_id) { return Err(bad("a channel cannot precede itself")); }
positive("channel_id", *channel_id)?;
@@ -657,6 +668,7 @@ pub(crate) fn validate(command: &AdminCommand) -> Result<(), ErrorObj> {
}
C::PrivilegeKeyDelete { token, .. } | C::PrivilegeKeyUse { token, .. } => check_str("token", token, 255, false)?,
C::TempPasswordAdd { password, description, duration_s, channel_id, channel_password, .. } => {
nonnegative("channel_id", *channel_id)?;
check_str("password", password, 255, false)?;
check_str("description", description, 255, true)?;
check_str("channel_password", channel_password, 255, true)?;
+5 -2
View File
@@ -56,11 +56,14 @@ impl Command {
|Self::GroupDelete{generation,..}|Self::GroupDeletePermissions{generation,..}|Self::ChannelDeletePermissions{generation,..}=>Some(*generation), _=>None,
}}
pub(super) fn validate(&self) -> Result<(),ErrorObj> {
let positive=|id:u64| if id==0 {Err(menu::bad("target ID must be positive"))}else{Ok(())};
let positive=|id:u64| super::positive("target ID",id);
let text=|s:&str,max:usize| if s.is_empty()||s.len()>max||s.chars().any(char::is_control){Err(menu::bad("invalid text argument"))}else{Ok(())};
if let Some(g)=self.generation(){super::generation(g)?;}
match self {
Self::ServerLog{lines,..} if !(1..=1000).contains(lines)=>return Err(menu::bad("lines must be 1..1000")),
Self::ServerLog{lines,begin_pos,..}=>{
if !(1..=1000).contains(lines){return Err(menu::bad("lines must be 1..1000"));}
super::nonnegative("begin_pos",*begin_pos)?;
},
Self::ClientDatabaseList{duration,..} if !(1..=200).contains(duration)=>return Err(menu::bad("duration must be 1..200")),
Self::ClientDatabaseFind{pattern,..}=>text(pattern,255)?,
Self::ClientDatabaseId{uid}=>text(uid,128)?,
+36
View File
@@ -330,6 +330,42 @@ fn connect_and_disconnect_validate_arguments_before_network_io() {
core.shutdown();
}
#[test]
fn admin_ids_and_log_offsets_respect_the_safe_integer_boundary_before_connecting() {
let dir=TempDir::new("admin-integer-boundary");
let mut core=Core::start(&dir.0);
const MAX:u64=(1<<53)-1;
for (cmd,property,mut args) in [
("channel_create","channel_order",json!({"generation":1,"parent_id":0,"properties":{"channel_name":"Fixture"}})),
("channel_edit","channel_order",json!({"generation":1,"channel_id":2,"properties":{}})),
("server_edit","virtualserver_download_quota",json!({"generation":1,"confirm":"Fixture","properties":{}})),
("server_edit","virtualserver_default_server_group",json!({"generation":1,"confirm":"Fixture","properties":{}})),
] {
for (value,expected) in [(MAX,"not_connected"),(MAX+1,"bad_request")] {
args["properties"][property]=json!(value);
let (reply,_)=core.request("property-boundary",cmd,args.clone());
assert_eq!(reply["error"]["code"],expected,"{property}: {reply}");
}
}
for (cmd, field, mut args) in [
("channel_description", "channel_id", json!({})),
("channel_move", "parent_id", json!({"generation":1,"channel_id":2})),
("channel_move", "order", json!({"generation":1,"channel_id":2,"parent_id":0})),
("channel_create", "parent_id", json!({"generation":1,"properties":{"channel_name":"Fixture"}})),
("channel_info", "channel_id", json!({})),
("server_group_perm_list", "group_id", json!({})),
("temp_password_add", "channel_id", json!({"generation":1,"password":"fixture","duration_s":60})),
("server_log", "begin_pos", json!({"lines":1,"reverse":false,"instance":false})),
] {
for (value,expected) in [(MAX,"not_connected"),(MAX+1,"bad_request")] {
args[field]=json!(value);
let (reply,_)=core.request("boundary",cmd,args.clone());
assert_eq!(reply["error"]["code"],expected,"{cmd}.{field}: {reply}");
}
}
core.shutdown();
}
#[test]
fn talk_power_validates_its_captured_target_before_dispatch() {
let dir = TempDir::new("talk-power-args");