From cbc8279a804c831b57bec0d1eebdcfe39c5781df Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:00:55 +0000 Subject: [PATCH 1/4] Share bounded legacy control envelopes --- crates/jsonrpc/src/control.rs | 50 +++++++++++++++++++++++++++++++++++ crates/jsonrpc/src/lib.rs | 2 ++ 2 files changed, 52 insertions(+) create mode 100644 crates/jsonrpc/src/control.rs diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs new file mode 100644 index 000000000..f52e294b7 --- /dev/null +++ b/crates/jsonrpc/src/control.rs @@ -0,0 +1,50 @@ +//! Bounded compatibility framing for single-line `{id, method, params}` control requests. +//! This is not JSON-RPC: replies use `{id, ok, result}` or `{id, ok, error}` and every +//! request, including one without an id, receives a reply. Method routing stays with the caller. +use serde_json::{Value, json}; +use std::fmt::Display; + +/// Process one bounded control line. Blank lines yield no response. Missing `params` becomes null; +/// arbitrary JSON ids are echoed and malformed JSON has a null id, matching the legacy protocol. +pub fn process_line(line: &str, max_bytes: usize, dispatch: impl FnOnce(&str, &Value) -> Result) -> Option { + let trimmed = line.trim(); + if trimmed.is_empty() { + return None; + } + if line.len() > max_bytes { + return Some(error(None, "request exceeds byte limit")); + } + let value: Value = match serde_json::from_str(trimmed) { + Ok(v) => v, + Err(e) => return Some(error(None, &format!("bad request envelope: {e}"))), + }; + let id = value.get("id").cloned(); + let Some(method) = value.get("method").and_then(Value::as_str) else { + return Some(error(id, "missing `method`")); + }; + let params = value.get("params").cloned().unwrap_or(Value::Null); + Some(match dispatch(method, ¶ms) { + Ok(result) => json!({"id": id, "ok": true, "result": result}).to_string(), + Err(e) => error(id, &e.to_string()), + }) +} + +fn error(id: Option, message: &str) -> String { + json!({"id": id, "ok": false, "error": message}).to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn preserves_legacy_responses_and_null_defaults() { + let call = + |method: &str, params: &Value| -> Result { if method == "echo" { Ok(params.clone()) } else { Err("unknown") } }; + assert_eq!(process_line(" ", 20, call), None); + assert_eq!(process_line(r#"{"id":[1],"method":"echo"}"#, 100, call).unwrap(), r#"{"id":[1],"ok":true,"result":null}"#); + assert_eq!(process_line(r#"{"method":"bad"}"#, 100, call).unwrap(), r#"{"error":"unknown","id":null,"ok":false}"#); + assert!(process_line("{", 100, call).unwrap().contains("bad request envelope:")); + assert_eq!(process_line(r#"{"id":3}"#, 100, call).unwrap(), r#"{"error":"missing `method`","id":3,"ok":false}"#); + assert!(process_line("abcdef", 2, call).unwrap().contains("byte limit")); + } +} diff --git a/crates/jsonrpc/src/lib.rs b/crates/jsonrpc/src/lib.rs index 9f8128d1d..a674394a8 100644 --- a/crates/jsonrpc/src/lib.rs +++ b/crates/jsonrpc/src/lib.rs @@ -8,6 +8,8 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod control; + use serde_json::{Value, json}; use std::io::BufRead; From 72da2d5d056fdc1e7565ebe56014375c71c65d3f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:01:28 +0000 Subject: [PATCH 2/4] Enforce control line limit before trimming --- crates/jsonrpc/src/control.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs index f52e294b7..ed3729329 100644 --- a/crates/jsonrpc/src/control.rs +++ b/crates/jsonrpc/src/control.rs @@ -7,13 +7,13 @@ use std::fmt::Display; /// Process one bounded control line. Blank lines yield no response. Missing `params` becomes null; /// arbitrary JSON ids are echoed and malformed JSON has a null id, matching the legacy protocol. pub fn process_line(line: &str, max_bytes: usize, dispatch: impl FnOnce(&str, &Value) -> Result) -> Option { + if line.len() > max_bytes { + return Some(error(None, "request exceeds byte limit")); + } let trimmed = line.trim(); if trimmed.is_empty() { return None; } - if line.len() > max_bytes { - return Some(error(None, "request exceeds byte limit")); - } let value: Value = match serde_json::from_str(trimmed) { Ok(v) => v, Err(e) => return Some(error(None, &format!("bad request envelope: {e}"))), From d6d4f2bd4cc3578754cb42951b296eef69e4afe1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:01:38 +0000 Subject: [PATCH 3/4] Cover oversized control lines --- crates/jsonrpc/src/control.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs index ed3729329..3b87e1a97 100644 --- a/crates/jsonrpc/src/control.rs +++ b/crates/jsonrpc/src/control.rs @@ -46,5 +46,11 @@ mod tests { assert!(process_line("{", 100, call).unwrap().contains("bad request envelope:")); assert_eq!(process_line(r#"{"id":3}"#, 100, call).unwrap(), r#"{"error":"missing `method`","id":3,"ok":false}"#); assert!(process_line("abcdef", 2, call).unwrap().contains("byte limit")); + let invoked = std::cell::Cell::new(false); + let _ = process_line(" ", 2, |_, _| -> Result { + invoked.set(true); + Ok(Value::Null) + }); + assert!(!invoked.get()); } } From 8f9a719fb198c9fe3c0a72dfc030313dc8f5c62c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:06:03 +0000 Subject: [PATCH 4/4] Cover idless successful control requests --- crates/jsonrpc/src/control.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs index 3b87e1a97..c4c3a0389 100644 --- a/crates/jsonrpc/src/control.rs +++ b/crates/jsonrpc/src/control.rs @@ -43,6 +43,7 @@ mod tests { assert_eq!(process_line(" ", 20, call), None); assert_eq!(process_line(r#"{"id":[1],"method":"echo"}"#, 100, call).unwrap(), r#"{"id":[1],"ok":true,"result":null}"#); assert_eq!(process_line(r#"{"method":"bad"}"#, 100, call).unwrap(), r#"{"error":"unknown","id":null,"ok":false}"#); + assert_eq!(process_line(r#"{"method":"echo"}"#, 100, call).unwrap(), r#"{"id":null,"ok":true,"result":null}"#); assert!(process_line("{", 100, call).unwrap().contains("bad request envelope:")); assert_eq!(process_line(r#"{"id":3}"#, 100, call).unwrap(), r#"{"error":"missing `method`","id":3,"ok":false}"#); assert!(process_line("abcdef", 2, call).unwrap().contains("byte limit"));