diff --git a/docs/design.md b/docs/design.md index 1868882..a9d8539 100644 --- a/docs/design.md +++ b/docs/design.md @@ -102,7 +102,8 @@ and `slop-approve`. How the code does it is in - **One connection per request**, carrying in order: 1. the request; 2. the operation's stdin, only after approval and only for operations that read it (`ssh` - carries its stdin in the request instead, below); + carries its stdin in the request instead, below). The broker drops stdin an operation + doesn't read, and treats a requester sending more than 1 MiB as gone; 3. its stdout and stderr; 4. a final message with the exit status and any structured result. diff --git a/src/exec.rs b/src/exec.rs index 46f7c51..4154c30 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -32,7 +32,7 @@ impl Output { pub struct Io { /// Chunks of the requester's stdin, which it sends after approval for operations that ask for /// it; closed at EOF. - pub stdin: mpsc::Receiver>, + pub stdin: mpsc::UnboundedReceiver>, pub out: Output, /// Cancelled when the requester disconnects. pub cancel: CancellationToken, diff --git a/src/server.rs b/src/server.rs index 96df041..ecb08af 100644 --- a/src/server.rs +++ b/src/server.rs @@ -23,6 +23,10 @@ use crate::proto::{ApprovedBy, ClientMessage, PROTOCOL_VERSION, Request, ServerM use crate::queue::{NewRequest, Outcome, Queue, new_id}; use crate::targets::Targets; +/// The most stdin a requester may send. Operations that read it have their own, smaller limits; +/// a requester sending more than this is treated as gone. +const MAX_STDIN: usize = 1 << 20; + pub struct Broker { pub config: Config, pub queue: Queue, @@ -123,18 +127,25 @@ pub async fn handle(broker: Arc(&frame) { Ok(ClientMessage::Stdin(data)) => { if let Some(tx) = &stdin_tx { - let _ = tx.send(data).await; + total += data.len(); + if total > MAX_STDIN { + break; + } + if tx.send(data).is_err() { + stdin_tx = None; + } } } Ok(ClientMessage::StdinEnd) => stdin_tx = None, @@ -182,6 +193,10 @@ async fn process(broker: &Broker, requester: &str, request: Request, io: &mut Io } }; + if !parsed.stdin { + io.stdin.close(); + } + // Built-in operations only read the broker's own state, so they aren't logged. let builtin = parsed.op.builtin(); let log = |event| { diff --git a/tests/broker.rs b/tests/broker.rs index 9e24bc6..0a320ba 100644 --- a/tests/broker.rs +++ b/tests/broker.rs @@ -362,6 +362,34 @@ async fn disconnecting_kills_the_process() { assert!(!alive, "process {pid} survived its requester"); } +#[tokio::test] +async fn stdin_an_operation_doesnt_read_doesnt_hide_a_disconnect() { + let directory = tempfile::tempdir().unwrap(); + let broker = broker(directory.path(), json!({ "ssh": { "action": "auto" } })); + + let mut client = Client::start(&broker, ssh_with(&["true"], Some(b"block\n"))).await; + client.next(&mut String::new()).await; + for _ in 0..64 { + client.send(ClientMessage::Stdin(b"ignored\n".to_vec())).await; + } + + let pid_file = directory.path().join("pid"); + let mut pid = None; + for _ in 0..50 { + tokio::time::sleep(Duration::from_millis(50)).await; + pid = std::fs::read_to_string(&pid_file).ok().and_then(|p| p.trim().parse::().ok()); + if pid.is_some() { + break; + } + } + let pid = pid.expect("fake ssh started"); + + drop(client); + tokio::time::sleep(Duration::from_millis(500)).await; + let alive = kill(Pid::from_raw(pid), None).is_ok(); + assert!(!alive, "process {pid} survived its requester"); +} + #[tokio::test] async fn queries_run_a_fixed_quoted_command_without_stdin() { let directory = tempfile::tempdir().unwrap();