From db33516bf602f61aea763015256c9a3a4858d67d Mon Sep 17 00:00:00 2001 From: Martin Ball <98639962+benandez@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:48:37 +0000 Subject: [PATCH] Tests: restore control-connection and X11 clipboard guards lost in WordCraft's port (wordcraft#7317) (#3115) * storytold/wordcraft#7317: restore control-server and clipboard security tests Port the lost tests onto craft-automation (EOF request, connection limit) and craft-platform (X11 property read cap). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE * storytold/wordcraft#7317: the EOF trial requires a real close, not a read timeout Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE * wordcraft#7317: the restored connection-limit test retries when Windows resets a refused connection's write Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE --------- Co-authored-by: Claude Opus 5.5 --- crates/automation/tests/it/control.rs | 57 ++++++++++++++++++++ crates/platform/src/clipboard/offer/tests.rs | 12 +++++ 2 files changed, 69 insertions(+) diff --git a/crates/automation/tests/it/control.rs b/crates/automation/tests/it/control.rs index 5191dabc7..d04fee092 100644 --- a/crates/automation/tests/it/control.rs +++ b/crates/automation/tests/it/control.rs @@ -371,3 +371,60 @@ fn a_repeated_auth_is_answered_already_authenticated_without_dispatching() { assert_eq!(after["result"], "still serving"); handler.join().expect("handler ran"); } + +/// Ported from WordCraft (#7317). A request that is complete JSON but ends at EOF with no newline (the client wrote it +/// and shut its end down) still reaches the app and is answered: a client cannot know it was dropped. +#[trial] +fn a_complete_json_request_at_eof_can_reach_the_app() { + let (rx, server) = server::listen(0, TOKEN.into(), host()).expect("started"); + let handler = std::thread::spawn(move || { + let req = rx.recv_timeout(Duration::from_secs(5)).expect("the request reached the app"); + assert_eq!(req.method, "test.once"); + req.reply.send(json!({"ok": true, "result": "once"})).expect("replied"); + assert!(rx.recv_timeout(Duration::from_millis(300)).is_err(), "it ran exactly once"); + }); + let (mut stream, mut reader) = authenticated(server.addr); + stream.write_all(json!({"id": 3, "method": "test.once"}).to_string().as_bytes()).expect("written"); + stream.shutdown(std::net::Shutdown::Write).expect("shut down"); + let reply = read_json(&mut reader).expect("a reply"); + assert_eq!((&reply["id"], &reply["result"]), (&json!(3), &json!("once"))); + let mut rest = String::new(); + assert_eq!(reader.read_line(&mut rest).expect("closed, not timed out"), 0, "closed after the last request: {rest:?}"); + handler.join().expect("handler ran"); +} + +/// Ported from WordCraft (#7317). Connections that never authenticate cannot fill the server: past the limit a new +/// one is refused, and a slot freed by a closed connection is admitted again. +#[trial] +fn idle_unauthenticated_connections_are_limited() { + use craft_automation::security::MAX_CONNECTIONS; + // Far longer than the trial, so only the limit refuses. + let (rx, server) = server::serve_with_deadline(TcpListener::bind("127.0.0.1:0").expect("bound"), TOKEN.into(), host(), Duration::from_secs(60)) + .expect("started"); + let idle: Vec = (0..MAX_CONNECTIONS).map(|_| TcpStream::connect(server.addr).expect("connected")).collect(); + // Admission is counted by the accept thread: it is done once the next connection is answered. + let refused = |addr| { + let stream = TcpStream::connect(addr).expect("connected"); + stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + read_json(&mut BufReader::new(stream)) + }; + let reply = refused(server.addr).expect("a refusal"); + assert_eq!(reply["ok"], false); + assert!(reply["error"].as_str().is_some_and(|e| e.contains("limit")), "{reply}"); + drop(idle); + // The freed slots go back as the closed connections are noticed. + let started = motion::now(); + loop { + let mut stream = TcpStream::connect(server.addr).expect("connected"); + stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + let mut reader = BufReader::new(stream.try_clone().expect("cloned")); + // A refused connection may already be closed when the request is written (Windows resets it). + let auth = json!({"id": "auth", "method": "auth", "params": {"token": TOKEN}}); + if writeln!(stream, "{auth}").is_ok() && read_json(&mut reader).is_some_and(|r| r["ok"] == true) { + break; + } + assert!(started.elapsed() < Duration::from_secs(10), "no slot came back"); + std::thread::sleep(Duration::from_millis(50)); + } + assert!(rx.try_recv().is_err()); +} diff --git a/crates/platform/src/clipboard/offer/tests.rs b/crates/platform/src/clipboard/offer/tests.rs index cd667d8f6..0f330430e 100644 --- a/crates/platform/src/clipboard/offer/tests.rs +++ b/crates/platform/src/clipboard/offer/tests.rs @@ -26,3 +26,15 @@ fn cf_html_offsets_delimit_the_fragment_bytes() { assert_eq!(&data[end_html..], b"\0", "EndHTML excludes the trailing NUL"); assert_eq!(end_html, data.len() - 1); } + +/// Ported from WordCraft (#7317). An X11 property read is taken only when it came whole and within the cap: over the +/// cap, or with bytes still left to read, it is refused rather than pasted truncated. +#[cfg(all(unix, not(target_os = "macos")))] +#[test] +fn property_reads_must_be_whole_and_capped() { + use super::unix::complete; + assert!(complete(10, 0, 10)); + assert!(!complete(11, 0, 10), "over the cap"); + assert!(!complete(8, 4, 10), "more left to read"); + assert!(complete(0, 0, 0)); +}