From 4bccd21980a8859c1a23816fc95a2f2313955639 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Tue, 17 Nov 2026 04:23:16 -0600 Subject: [PATCH] fix(fleet-queue): validate session id format in verb_bind (#8287) (task 20115) --- pkgs/fleet/crates/fleet-queue/src/client.rs | 5 +- .../crates/fleet-queue/src/turso_queue.rs | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/pkgs/fleet/crates/fleet-queue/src/client.rs b/pkgs/fleet/crates/fleet-queue/src/client.rs index d734dda4c1..86c9e0b28b 100644 --- a/pkgs/fleet/crates/fleet-queue/src/client.rs +++ b/pkgs/fleet/crates/fleet-queue/src/client.rs @@ -2468,7 +2468,10 @@ fn unbound_append(worker: &str, row: &Value, trigger: &str) { /// #9046: fleet session ids are `ses_` + alphanumerics — the /// shape every session surface mints. Anything else is not a /// session id and must never reach a route_attempt SQL string. -fn valid_session_id(s: &str) -> bool { +/// #8287: this is the crate's one session-id predicate — the +/// broker's bind verb (turso_queue::verb_bind) validates against +/// it too; do not fork a second definition. +pub(crate) fn valid_session_id(s: &str) -> bool { s.len() > 4 && s.starts_with("ses_") && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') } diff --git a/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs b/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs index 41ef53358c..0802ab37aa 100644 --- a/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs +++ b/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs @@ -5233,6 +5233,20 @@ fn verb_bind(t: &TursoBackend, req: &Value) -> Value { let launched_by = req_str(req, "launched_by") .unwrap_or("opencode-claimant") .to_string(); + // #8287: a malformed session id refuses here, as a usage + // error, before any state is touched — binding a ghost id + // strands the task until stuck-bound supervision recovers + // it. The predicate is the one the client/claim paths keep + // (client::valid_session_id); do not fork a second one. + if !crate::client::valid_session_id(&session_id) { + return fail( + 2, + format!( + "bind_repo_task_lane_session: invalid session_id {session_id:?} \ + (expected ses_ + alphanumeric/underscore)" + ), + ); + } // #9046: a bind may carry the bound session's own model (the // claimant derives it from the session record, #8176); when // it does, the bind stamps it via stamp_claim_model below. @@ -8654,6 +8668,48 @@ mod tests { assert_eq!(out["task"]["max_attempts"], 1000, "{out}"); } + #[test] + fn bind_rejects_malformed_session_id_8287() { + // #8287 oracle: verb_bind refuses a malformed session id + // as a usage error (exit 2) before any store access, so + // the task is left unbound — the dummy backend proves the + // refusal happens pre-store (any store touch would fail + // differently). A well-formed id must NOT produce the + // invalid-session refusal (it proceeds to the store path + // and fails there against this backend, or on the task). + let backend = TursoBackend::new( + "http://127.0.0.1:0".to_string(), + "test".to_string(), + ureq::agent(), + ); + for bad in ["not-a-session", "", "ses_", "ses_a'; DROP TABLE tasks; --"] { + let out = verb_bind( + &backend, + &json!({"task_id": 1, "worker": "w", "session_id": bad}), + ); + assert_eq!(out["ok"], false, "{bad}: {out}"); + assert_eq!(out["exit"], 2, "{bad}: {out}"); + assert!( + out["error"] + .as_str() + .unwrap_or("") + .contains("invalid session_id"), + "{bad}: {out}" + ); + } + let out = verb_bind( + &backend, + &json!({"task_id": 1, "worker": "w", "session_id": "ses_e5e37addeb0ffeumu1UPO2bI8G"}), + ); + assert!( + !out["error"] + .as_str() + .unwrap_or("") + .contains("invalid session_id"), + "well-formed id refused as invalid: {out}" + ); + } + #[test] fn model_stamp_detail_9046_carries_session_and_trigger() { // #9046 oracle: a stamp detail names the session it