PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
control: close the connection on malformed input before auth (#1259)
SteamSmith486b1bbcb1be
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rsindex 342998ffd..65a12e199 100644--- a/crates/ui-egui/src/control.rs+++ b/crates/ui-egui/src/control.rs@@ -788,7 +788,8 @@ pub fn serve(client: ControlClient) -> std::io::Result<Endpoint> {continue;}let (reply, close) = match serde_json::from_str::<Value>(&line) {- Err(e) => (rpc_error(Value::Null, -32700, &format!("parse error: {e}")), false),+ // Before `auth`, like any other first request, this closes the connection.+ Err(e) => (rpc_error(Value::Null, -32700, &format!("parse error: {e}")), !authed),Ok(msg) => {let id = msg.get("id").cloned().unwrap_or(Value::Null);let method = msg.get("method").and_then(Value::as_str).unwrap_or("");@@ -839,3 +840,31 @@ fn random_token() -> std::io::Result<String> {getrandom::fill(&mut bytes).map_err(|e| std::io::Error::other(e.to_string()))?;Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())}++#[cfg(test)]+mod transport_tests {+ /// Before authenticating, a line that isn't JSON gets a parse error and closes the+ /// connection, as [`super::serve`] documents for any first request other than `auth`.+ #[cfg(not(target_arch = "wasm32"))]+ #[test]+ fn malformed_input_before_auth_closes_the_connection() {+ use std::io::{BufRead, BufReader, ErrorKind, Write};+ let (_control, client) = super::attach(&egui::Context::default());+ let ep = super::serve(client).unwrap();+ let s = std::net::TcpStream::connect(("127.0.0.1", ep.port)).unwrap();+ s.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap();+ writeln!(s.try_clone().unwrap(), "not json").unwrap();+ let mut r = BufReader::new(s);+ let mut first = String::new();+ r.read_line(&mut first).unwrap();+ let reply: serde_json::Value = serde_json::from_str(&first).unwrap();+ assert_eq!(reply["error"]["code"], -32700, "{first}");+ // Closed: end of stream (or a reset), not a connection left waiting for more input.+ let mut next = String::new();+ let closed = match r.read_line(&mut next) {+ Ok(n) => n == 0,+ Err(e) => matches!(e.kind(), ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted),+ };+ assert!(closed, "the connection stayed open: {next:?}");+ }+}