Skip to main content

agentmux_srv/drone/
value.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A value in a run, with what the engine must remember about where it came
5//! from (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md §4.2, §4.5).
6//!
7//! Two bits travel with every value:
8//!
9//! * `tainted`: some text in it came from outside the person's own design (an
10//!   event payload, an API response, a Run step's output, an Agent's answer). It
11//!   may be quoted, never used as a command, a path or a target.
12//! * `derived`: a clean number or boolean made *inside the run* from tainted
13//!   input (a `count` of a tainted list, an exit code, an enum cleared by
14//!   `in_set`). It is clean as data, and still **chosen by an attacker** as
15//!   control (a branch, a loop guard) and as a numeric argument. Only a field
16//!   typed by the trigger's own schema is exempt (§4.5, the derived-value rule).
17//!
18//! Every module of the engine takes and returns [`Tv`], so a taint bit is never
19//! dropped at a module boundary. Both bits are always written and always
20//! required when read: a value with a bit missing is an error, never "clean".
21
22use serde::{Deserialize, Serialize};
23use serde_json::Value;
24
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub struct Tv {
27    pub v: Value,
28    pub tainted: bool,
29    pub derived: bool,
30}
31
32impl Tv {
33    /// A value the person's own design wrote, or a field typed by the trigger's schema.
34    pub fn clean(v: Value) -> Self {
35        Self { v, tainted: false, derived: false }
36    }
37
38    /// A value that carries outside text.
39    pub fn tainted(v: Value) -> Self {
40        Self { v, tainted: true, derived: false }
41    }
42
43    /// A clean number or boolean made in the run from tainted input.
44    pub fn derived(v: Value) -> Self {
45        Self { v, tainted: false, derived: true }
46    }
47
48    /// Whether an attacker could have chosen this value when it is used as a
49    /// branch, a loop guard, an item count or a numeric argument (§4.5).
50    pub fn attacker_chosen_as_control(&self) -> bool {
51        self.tainted || self.derived
52    }
53
54    /// May this value be an argument of a command, a path or a target? Only a
55    /// value that is neither tainted nor derived (§3.6, §4.1).
56    pub fn usable_as_argument(&self) -> bool {
57        !self.tainted && !self.derived
58    }
59}
60
61/// The bits of a value made from `inputs`: tainted if any input is, derived if
62/// any input is derived and the result is not tainted. This is the default for
63/// anything computed from several values; an operation that cannot carry text
64/// may lower `tainted` to `derived` (see `transform::taint_out`).
65pub fn join(inputs: &[&Tv]) -> (bool, bool) {
66    let tainted = inputs.iter().any(|t| t.tainted);
67    let derived = !tainted && inputs.iter().any(|t| t.derived);
68    (tainted, derived)
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use serde_json::json;
75
76    #[test]
77    fn a_value_says_whether_an_attacker_could_have_chosen_it() {
78        assert!(!Tv::clean(json!(3)).attacker_chosen_as_control());
79        assert!(Tv::tainted(json!("x")).attacker_chosen_as_control());
80        assert!(Tv::derived(json!(3)).attacker_chosen_as_control(), "a count of a tainted list gates nothing alone");
81    }
82
83    #[test]
84    fn only_a_clean_value_is_an_argument() {
85        assert!(Tv::clean(json!("v1.2.3")).usable_as_argument());
86        assert!(!Tv::tainted(json!("v1.2.3")).usable_as_argument());
87        assert!(!Tv::derived(json!(3)).usable_as_argument());
88    }
89
90    #[test]
91    fn joining_taints_if_any_input_is_and_derives_otherwise() {
92        let (c, t, d) = (Tv::clean(json!(1)), Tv::tainted(json!("x")), Tv::derived(json!(2)));
93        assert_eq!(join(&[&c, &c]), (false, false));
94        assert_eq!(join(&[&c, &t]), (true, false));
95        assert_eq!(join(&[&c, &d]), (false, true));
96        assert_eq!(join(&[&t, &d]), (true, false), "tainted wins: it is the stronger claim");
97        assert_eq!(join(&[]), (false, false));
98    }
99
100    #[test]
101    fn the_bits_survive_serialization_and_a_missing_bit_is_an_error_not_clean() {
102        let t = Tv::tainted(json!({ "a": 1 }));
103        assert_eq!(serde_json::from_value::<Tv>(serde_json::to_value(&t).unwrap()).unwrap(), t);
104        let d = Tv::derived(json!(2));
105        assert_eq!(serde_json::from_value::<Tv>(serde_json::to_value(&d).unwrap()).unwrap(), d);
106        assert_eq!(serde_json::to_value(Tv::clean(json!(1))).unwrap(), json!({ "v": 1, "tainted": false, "derived": false }));
107        // A value with a bit missing is refused: restoring it as clean would launder taint.
108        assert!(serde_json::from_value::<Tv>(json!({ "v": 1 })).is_err());
109        assert!(serde_json::from_value::<Tv>(json!({ "v": 1, "tainted": false })).is_err());
110    }
111}