Reject non-object requests and arguments before stdio dispatch

This commit is contained in:
omar committed 2026-11-27 21:43:48 +03:00
1 parent 0d7edae8cf
commit 0e7d0b9bf4
6 files changed
+45 -12

No files matched your search

+2 -10
View File
@@ -338,14 +338,6 @@ fn args<T: serde::de::DeserializeOwned>(raw: Option<&serde_json::value::RawValue
serde_json::from_str(raw.map_or("{}", |r| r.get())).map_err(|e| bad(format!("invalid args: {e}")))
}
fn object_args<T: serde::de::DeserializeOwned>(raw: Option<&serde_json::value::RawValue>) -> Result<T, ErrorObj> {
// Serde's derived struct deserializer also accepts positional arrays.
if raw.is_some_and(|r| !r.get().trim_start().starts_with('{')) {
return Err(bad("invalid args: expected an object"));
}
args(raw)
}
fn check_str(field: &str, value: &str, max: usize, allow_empty: bool) -> Result<(), ErrorObj> {
if !allow_empty && value.is_empty() {
return Err(bad(format!("{field} must not be empty")));
@@ -547,10 +539,10 @@ pub fn parse(cmd: &str, raw: Option<&serde_json::value::RawValue>) -> Result<Adm
C::ChannelDelete { generation: a.generation, channel_id: a.channel_id, force: a.force, confirm: a.confirm }
}
"channel_subscribe" => {
let a: SubscribeArgs = object_args(raw)?;
let a: SubscribeArgs = args(raw)?;
C::ChannelSubscribe { channel_id: a.channel_id, subscribe: a.subscribe }
}
"channel_subscribe_all" => C::ChannelSubscribeAll { subscribe: object_args::<SubscribeAllArgs>(raw)?.subscribe },
"channel_subscribe_all" => C::ChannelSubscribeAll { subscribe: args::<SubscribeAllArgs>(raw)?.subscribe },
"channel_description" => C::ChannelDescription { channel_id: args::<ChannelArgs>(raw)?.channel_id },
"set_away" => {
let a: AwayArgs = args(raw)?;
+14 -1
View File
@@ -262,10 +262,15 @@ struct Envelope<'a> {
id: Option<Cow<'a, str>>,
#[serde(borrow)]
cmd: Cow<'a, str>,
#[serde(default, borrow)]
#[serde(default, borrow, deserialize_with = "present_args")]
args: Option<&'a RawValue>,
}
// A missing field uses the default; an explicit null must still be checked.
fn present_args<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Option<&'de RawValue>, D::Error> {
<&RawValue>::deserialize(deserializer).map(Some)
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Empty {}
@@ -480,6 +485,9 @@ pub fn decode_frame(b64: &str) -> Result<Box<Frame>, ErrorObj> {
/// Parse one stdin line (without trailing newline).
pub fn parse_line(line: &str) -> Input {
if !line.trim_start().starts_with('{') {
return Input::Invalid { id: None, error: bad("request must be an object") };
}
let env: Envelope = match serde_json::from_str(line) {
Ok(e) => e,
Err(e) => {
@@ -492,6 +500,11 @@ pub fn parse_line(line: &str) -> Input {
}
};
if env.args.is_some_and(|raw| !raw.get().starts_with('{')) {
let id = if matches!(env.cmd.as_ref(), "audio_frame" | "audio_end") { None }
else { env.id.filter(|id| valid_id(id)).map(Cow::into_owned) };
return Input::Invalid { id, error: bad("args must be an object") };
}
// High-rate audio commands carry no id and get no response.
match env.cmd.as_ref() {
"audio_frame" => {
+24
View File
@@ -483,6 +483,30 @@ fn channel_queries_validate_arguments_before_connection_lookup() {
core.shutdown();
}
#[test]
fn stdio_requires_objects_without_dispatching_positional_commands() {
let dir = TempDir::new("object-envelope");
let mut core = Core::start(&dir.0);
for (cmd, args) in [("profile_create", json!(["Positional"])), ("channel_description", json!([1])),
("admin_context", json!([])), ("shutdown", Value::Null)] {
let (reply, _) = core.request("invalid", cmd, args);
assert_eq!(reply["error"]["code"], "bad_request", "{cmd}: {reply}");
}
for input in [json!(["unexpected", "shutdown", {}]), json!({"id":"ignored", "cmd":"audio_end", "args":[]})] {
core.send(input);
let event = core.next();
assert_eq!(event["event"], "error", "{event}");
assert_eq!(event["error"]["code"], "bad_request");
assert!(event.get("id").is_none());
}
core.send(json!({"id":"alive", "cmd":"profile_get"}));
let profile = core.next();
assert_eq!(profile["id"], "alive");
assert_eq!(profile["ok"], true, "{profile}");
assert_eq!(profile["result"]["exists"], false, "{profile}");
core.shutdown();
}
#[test]
fn channel_subscriptions_validate_arguments_before_connection_lookup() {
const SINGLE: &str = "channel_subscribe";