diff --git a/pkgs/fleet/crates/fleet-queue/src/client.rs b/pkgs/fleet/crates/fleet-queue/src/client.rs index 3eae38e042..c7a06635cc 100644 --- a/pkgs/fleet/crates/fleet-queue/src/client.rs +++ b/pkgs/fleet/crates/fleet-queue/src/client.rs @@ -845,6 +845,69 @@ pub(crate) fn list_limit_or_default(raw: Option) -> Result { validate_list_limit(raw.unwrap_or(LIST_LIMIT_DEFAULT)) } +/// The accepted ranges for the queue duration flags (#6420; design +/// #6448 Phase 3 pin, same as `list --limit`: out-of-range is +/// REJECTED, never clamped, at both the client and the broker). +/// A 0/negative lease is born expired, a 0/negative heartbeat extend +/// lapses the holder's own claim, and a negative park delay puts +/// `not_before` in the past and silently defeats the park; an +/// unbounded value overflows the SQL `integer` RPC parameters. +/// Maxima: a lease or extend beyond a day defeats the expiry the +/// lease exists to provide; a park delay beyond the dep-block TTL +/// ceiling (7 days, #8276) outlives every broker wait. +pub(crate) const LEASE_SECONDS_MAX: i64 = 86_400; +pub(crate) const LEASE_SECONDS_DEFAULT: i64 = 2_700; +pub(crate) const EXTEND_SECONDS_MAX: i64 = 86_400; +pub(crate) const EXTEND_SECONDS_DEFAULT: i64 = 900; +pub(crate) const DELAY_SECONDS_MAX: i64 = 604_800; + +/// Validate one duration flag's value against `1..=max`; the error +/// names the flag so a usage failure points at it. Zero is rejected +/// for every flag: for `--delay-seconds` that is the explicit #6420 +/// decision — a park that waits zero seconds is a requeue, and a +/// requeue already has its own verb (`release`). +pub(crate) fn validate_duration(flag: &str, value: i64, max: i64) -> Result { + if (1..=max).contains(&value) { + Ok(value) + } else { + Err(format!( + "argument --{flag}: value {value} out of range 1..={max}" + )) + } +} + +/// Validate a `claim --lease-seconds` value against +/// `1..=LEASE_SECONDS_MAX`; the error names `--lease-seconds`. +pub(crate) fn validate_lease_seconds(value: i64) -> Result { + validate_duration("lease-seconds", value, LEASE_SECONDS_MAX) +} + +/// The `claim --lease-seconds` value for a call: the flag's value +/// validated, or the unchanged default when the flag is absent. +pub(crate) fn lease_seconds_or_default(raw: Option) -> Result { + validate_lease_seconds(raw.unwrap_or(LEASE_SECONDS_DEFAULT)) +} + +/// Validate a `heartbeat --extend-seconds` value against +/// `1..=EXTEND_SECONDS_MAX`; the error names `--extend-seconds`. +pub(crate) fn validate_extend_seconds(value: i64) -> Result { + validate_duration("extend-seconds", value, EXTEND_SECONDS_MAX) +} + +/// The `heartbeat --extend-seconds` value for a call: the flag's +/// value validated, or the unchanged default when the flag is absent. +pub(crate) fn extend_seconds_or_default(raw: Option) -> Result { + validate_extend_seconds(raw.unwrap_or(EXTEND_SECONDS_DEFAULT)) +} + +/// Validate a `park --delay-seconds` value against +/// `1..=DELAY_SECONDS_MAX`; the error names `--delay-seconds`. +/// (No default: the flag is optional, and an absent flag leaves +/// `delay_seconds` out of the park request entirely.) +pub(crate) fn validate_delay_seconds(value: i64) -> Result { + validate_duration("delay-seconds", value, DELAY_SECONDS_MAX) +} + fn env_or_default(value: Option<&str>, default: &str) -> String { value .map(str::trim) @@ -1327,10 +1390,14 @@ fn cmd_claim(rest: &[String]) -> i32 { ); reject_extra_pos("claim", &args, 0); let worker = opt_worker(&args); + let lease_seconds = match lease_seconds_or_default(args.get_i64("lease-seconds")) { + Ok(v) => v, + Err(e) => die(2, &e), + }; let mut body = json!({ "queue": opt_queue(&args), "worker": worker, - "lease_seconds": args.get_i64("lease-seconds").unwrap_or(2700), + "lease_seconds": lease_seconds, }); if let Some(lane) = args.get("lane") { body["lane"] = json!(lane); @@ -1759,13 +1826,17 @@ fn cmd_heartbeat(rest: &[String]) -> i32 { &[], ); reject_extra_pos("heartbeat", &args, 1); + let extend_seconds = match extend_seconds_or_default(args.get_i64("extend-seconds")) { + Ok(v) => v, + Err(e) => die(2, &e), + }; let resp = unwrap_ok_fb( call_fb( "/heartbeat", &json!({ "task_id": args.pos_i64(0, "task_id"), "worker": opt_worker(&args), - "extend_seconds": args.get_i64("extend-seconds").unwrap_or(900), + "extend_seconds": extend_seconds, "progress": progress_arg(&args), }), ), @@ -1831,6 +1902,10 @@ fn cmd_park(rest: &[String]) -> i32 { if let Some(nb) = args.get("not-before") { body["not_before"] = json!(nb); } else if let Some(d) = args.get_i64("delay-seconds") { + let d = match validate_delay_seconds(d) { + Ok(v) => v, + Err(e) => die(2, &e), + }; body["delay_seconds"] = json!(d); } if let Some(rc_raw) = args.get("ready-condition") { @@ -3101,6 +3176,69 @@ mod tests { assert_eq!(list_limit_or_default(Some(0)), validate_list_limit(0)); } + #[test] + fn duration_flags_range_is_validated() { + // #6420: 0/negative durations corrupt claim state (a lease + // born expired, a heartbeat that lapses its own claim, a park + // whose not_before lands in the past) and unbounded values + // overflow the SQL integer RPC parameters. Out-of-range is a + // usage error naming the flag (design #6448 Phase 3: reject, + // not clamp). Park delay 0 is rejected by explicit decision: + // a zero-wait park is a requeue, which has its own verb. + for bad in [0, -1, LEASE_SECONDS_MAX + 1, i64::MAX] { + let err = validate_lease_seconds(bad) + .err() + .expect("out-of-range --lease-seconds must be rejected"); + assert!( + err.contains("--lease-seconds"), + "error names the flag: {err}" + ); + } + for bad in [0, -1, EXTEND_SECONDS_MAX + 1, i64::MAX] { + let err = validate_extend_seconds(bad) + .err() + .expect("out-of-range --extend-seconds must be rejected"); + assert!( + err.contains("--extend-seconds"), + "error names the flag: {err}" + ); + } + for bad in [0, -1, DELAY_SECONDS_MAX + 1, i64::MAX] { + let err = validate_delay_seconds(bad) + .err() + .expect("out-of-range --delay-seconds must be rejected"); + assert!( + err.contains("--delay-seconds"), + "error names the flag: {err}" + ); + } + // Boundaries and ordinary values pass through unchanged. + assert_eq!(validate_lease_seconds(1), Ok(1)); + assert_eq!( + validate_lease_seconds(LEASE_SECONDS_MAX), + Ok(LEASE_SECONDS_MAX) + ); + assert_eq!(validate_extend_seconds(1), Ok(1)); + assert_eq!( + validate_extend_seconds(EXTEND_SECONDS_MAX), + Ok(EXTEND_SECONDS_MAX) + ); + assert_eq!(validate_delay_seconds(1), Ok(1)); + assert_eq!( + validate_delay_seconds(DELAY_SECONDS_MAX), + Ok(DELAY_SECONDS_MAX) + ); + // Absent flags keep the existing defaults (claim 2700, + // heartbeat 900). + assert_eq!(lease_seconds_or_default(None), Ok(2700)); + assert_eq!(extend_seconds_or_default(None), Ok(900)); + assert_eq!(lease_seconds_or_default(Some(0)), validate_lease_seconds(0)); + assert_eq!( + extend_seconds_or_default(Some(-1)), + validate_extend_seconds(-1) + ); + } + #[test] fn well_formed_invocation_is_unchanged() { // Control (#5939 acceptance): list --queue alpha records diff --git a/pkgs/fleet/crates/fleet-queue/src/queue_broker.rs b/pkgs/fleet/crates/fleet-queue/src/queue_broker.rs index 31bc655fa7..eeb05dfb74 100644 --- a/pkgs/fleet/crates/fleet-queue/src/queue_broker.rs +++ b/pkgs/fleet/crates/fleet-queue/src/queue_broker.rs @@ -539,6 +539,76 @@ fn list_limit(req: &Value) -> Result { crate::client::list_limit_or_default(raw) } +/// One duration field of a request, validated before the RPC +/// (#6420): the broker is the trust boundary for non-CLI callers, so +/// an out-of-range value is rejected with the same usage error the +/// client itself raises (design #6448 Phase 3: reject, not clamp). +/// An absent or null field stays absent — the SQL defaults apply, +/// exactly as before. A field that is PRESENT but not an i64 (a +/// float, a string, an integer beyond i64) is rejected, never +/// treated as absent: before this guard it reached the SQL integer +/// parameter and failed there, and silently defaulting it would +/// mutate the task on a value the caller never chose (Codex +/// finding on PR #8425). +fn duration_field(req: &Value, field: &str, flag: &str, max: i64) -> Result, String> { + let raw = match req.get(field) { + None => return Ok(None), + Some(v) if v.is_null() => return Ok(None), + Some(v) => v, + }; + match raw.as_i64() { + Some(v) => crate::client::validate_duration(flag, v, max).map(Some), + None => Err(format!( + "argument --{flag}: value {raw} out of range 1..={max}" + )), + } +} + +/// The `/claim` lease, validated before the RPC (#6420). +fn lease_seconds(req: &Value) -> Result, String> { + duration_field( + req, + "lease_seconds", + "lease-seconds", + crate::client::LEASE_SECONDS_MAX, + ) +} + +/// The `/heartbeat` extend, validated before the RPC (#6420). +fn extend_seconds(req: &Value) -> Result, String> { + duration_field( + req, + "extend_seconds", + "extend-seconds", + crate::client::EXTEND_SECONDS_MAX, + ) +} + +/// The `/park` delay, validated before the RPC (#6420). +fn delay_seconds(req: &Value) -> Result, String> { + duration_field( + req, + "delay_seconds", + "delay-seconds", + crate::client::DELAY_SECONDS_MAX, + ) +} + +/// The duration guard for one request path (#6420), run at the top +/// of `dispatch` BEFORE backend selection: the Turso backend +/// dispatches verbatim, so validating only inside the PostgREST +/// handlers would leave the trust boundary open on that backend +/// (Codex finding on PR #8425). The PostgREST handlers keep their +/// own checks as defense in depth. +fn validate_durations(path: &str, req: &Value) -> Result<(), String> { + match path { + "/claim" => lease_seconds(req).map(|_| ()), + "/heartbeat" => extend_seconds(req).map(|_| ()), + "/park" => delay_seconds(req).map(|_| ()), + _ => Ok(()), + } +} + fn ok(fields: Value) -> Value { let mut m = json!({"ok": true}); if let (Some(dst), Some(src)) = (m.as_object_mut(), fields.as_object()) { @@ -738,6 +808,9 @@ impl crate::turso_queue::DepProbe for GhDepProbe { } fn dispatch(st: &State, path: &str, req: &Value) -> Value { + if let Err(e) = validate_durations(path, req) { + return fail(2, e); + } if let Some(t) = &st.turso { if path != "/validate" && path != "/healthz" { return crate::turso_queue::dispatch(t, path, req); @@ -801,10 +874,14 @@ fn dispatch(st: &State, path: &str, req: &Value) -> Value { } } "/claim" => { + let lease = match lease_seconds(req) { + Ok(v) => v, + Err(e) => return fail(2, e), + }; let mut args = json!({ "p_worker": req.get("worker"), "p_queue": req.get("queue"), - "p_lease_seconds": req.get("lease_seconds"), + "p_lease_seconds": lease, }); if let Some(lane) = req.get("lane").filter(|l| !l.is_null()) { args["p_lane_id"] = lane.clone(); @@ -830,16 +907,22 @@ fn dispatch(st: &State, path: &str, req: &Value) -> Value { "p_launched_by": req.get("launched_by"), }), ), - "/heartbeat" => simple_rpc( - st, - "heartbeat_task", - json!({ - "p_task_id": req.get("task_id"), - "p_worker": req.get("worker"), - "p_extend_seconds": req.get("extend_seconds"), - "p_progress": req.get("progress"), - }), - ), + "/heartbeat" => { + let extend = match extend_seconds(req) { + Ok(v) => v, + Err(e) => return fail(2, e), + }; + simple_rpc( + st, + "heartbeat_task", + json!({ + "p_task_id": req.get("task_id"), + "p_worker": req.get("worker"), + "p_extend_seconds": extend, + "p_progress": req.get("progress"), + }), + ) + } "/checkpoint" => simple_rpc( st, "checkpoint_task", @@ -851,6 +934,10 @@ fn dispatch(st: &State, path: &str, req: &Value) -> Value { }), ), "/park" => { + let delay = match delay_seconds(req) { + Ok(v) => v, + Err(e) => return fail(2, e), + }; let mut args = json!({ "p_task_id": req.get("task_id"), "p_worker": req.get("worker"), @@ -859,8 +946,8 @@ fn dispatch(st: &State, path: &str, req: &Value) -> Value { }); if let Some(nb) = req.get("not_before").filter(|v| !v.is_null()) { args["p_not_before"] = nb.clone(); - } else if let Some(d) = req.get("delay_seconds").filter(|v| !v.is_null()) { - args["p_delay_seconds"] = d.clone(); + } else if let Some(d) = delay { + args["p_delay_seconds"] = json!(d); } if let Some(rc) = req.get("ready_condition").filter(|v| !v.is_null()) { args["p_ready_condition"] = rc.clone(); @@ -1469,4 +1556,95 @@ mod tests { // Absent limit keeps the existing default of 20. assert_eq!(list_limit(&json!({})), Ok(20)); } + + #[test] + fn duration_fields_reject_out_of_range_before_the_rpc() { + // #6420: a raw caller could send a born-expired lease, a + // self-lapsing heartbeat extend, or a park delay in the past + // straight to the RPC. The broker rejects all three with the + // client's usage error before any RPC is built. + for bad in [0, -1, 86_401, i64::MAX] { + let err = lease_seconds(&json!({"lease_seconds": bad})) + .err() + .expect("out-of-range lease_seconds must be rejected"); + assert!( + err.contains("--lease-seconds"), + "error names the flag: {err}" + ); + let err = extend_seconds(&json!({"extend_seconds": bad})) + .err() + .expect("out-of-range extend_seconds must be rejected"); + assert!( + err.contains("--extend-seconds"), + "error names the flag: {err}" + ); + } + for bad in [0, -1, 604_801, i64::MAX] { + let err = delay_seconds(&json!({"delay_seconds": bad})) + .err() + .expect("out-of-range delay_seconds must be rejected"); + assert!( + err.contains("--delay-seconds"), + "error names the flag: {err}" + ); + } + assert_eq!( + lease_seconds(&json!({"lease_seconds": 2700})), + Ok(Some(2700)) + ); + assert_eq!( + extend_seconds(&json!({"extend_seconds": 900})), + Ok(Some(900)) + ); + assert_eq!( + delay_seconds(&json!({"delay_seconds": 1200})), + Ok(Some(1200)) + ); + // Absent fields stay absent (SQL defaults apply, as before). + assert_eq!(lease_seconds(&json!({})), Ok(None)); + assert_eq!(extend_seconds(&json!({})), Ok(None)); + assert_eq!(delay_seconds(&json!({})), Ok(None)); + // Present but not an i64 (beyond-i64 integer, float, string) + // is rejected, never silently defaulted (Codex finding on + // PR #8425): before this guard such a value failed at the + // SQL integer parameter instead. + for bad in [json!(9223372036854775808u64), json!(1.5), json!("2700")] { + let err = lease_seconds(&json!({"lease_seconds": bad})) + .err() + .expect("present-but-invalid lease_seconds must be rejected"); + assert!( + err.contains("--lease-seconds"), + "error names the flag: {err}" + ); + let err = extend_seconds(&json!({"extend_seconds": bad})) + .err() + .expect("present-but-invalid extend_seconds must be rejected"); + assert!( + err.contains("--extend-seconds"), + "error names the flag: {err}" + ); + let err = delay_seconds(&json!({"delay_seconds": bad})) + .err() + .expect("present-but-invalid delay_seconds must be rejected"); + assert!( + err.contains("--delay-seconds"), + "error names the flag: {err}" + ); + } + // The dispatch-level guard covers both backends: it fires + // per path before backend selection (Codex finding on + // PR #8425), and leaves unrelated paths alone. + assert!(validate_durations("/claim", &json!({"lease_seconds": 0})).is_err()); + assert!(validate_durations("/heartbeat", &json!({"extend_seconds": -1})).is_err()); + assert!(validate_durations("/park", &json!({"delay_seconds": 604_801})).is_err()); + assert_eq!( + validate_durations("/claim", &json!({"lease_seconds": 2700})), + Ok(()) + ); + assert_eq!(validate_durations("/heartbeat", &json!({})), Ok(())); + assert_eq!( + validate_durations("/list", &json!({"lease_seconds": 0})), + Ok(()) + ); + } }