Skip to main content

graphrt_core/
redact.rs

1//! Scrubbing plaintext secrets out of free text.
2//!
3//! Typed values never leak: [`Value::Secret`] serializes and displays as
4//! [`SECRET_MASK`]. Free text is different. A node that needs a secret's plaintext
5//! (an HTTP call, a code sandbox, an LLM provider) can get it echoed back inside an
6//! error message, for example a provider error body that repeats the API key. These
7//! helpers replace every occurrence of known secret values with [`SECRET_MASK`].
8//!
9//! The engine keeps a [`SecretSet`] per run (environment secrets, re-supplied resume
10//! secrets, secret-typed inputs and node outputs) and scrubs every node error message
11//! with it before the error is emitted, retried, stored or turned into outputs.
12
13use std::borrow::Cow;
14use std::sync::{Arc, Mutex};
15
16use crate::value::{SECRET_MASK, Value};
17
18/// Secrets shorter than this are not scrubbed from text: masking every occurrence
19/// of a one- to three-character value would mangle messages without protecting
20/// anything an attacker could not guess.
21pub const MIN_SCRUB_LEN: usize = 4;
22
23/// Replace every occurrence of each of `secrets` in `text` with [`SECRET_MASK`].
24///
25/// Longer secrets are replaced first, so a secret that contains another one is
26/// masked as a whole. Values shorter than [`MIN_SCRUB_LEN`] are ignored.
27///
28/// ```
29/// use graphrt_core::redact::redact_secrets;
30/// let message = redact_secrets("invalid key sk-live-1234 for org", &["sk-live-1234"]);
31/// assert_eq!(message, "invalid key ****** for org");
32/// ```
33pub fn redact_secrets<'a, S: AsRef<str>>(text: &'a str, secrets: &[S]) -> Cow<'a, str> {
34    let mut candidates: Vec<&str> =
35        secrets.iter().map(AsRef::as_ref).filter(|s| s.len() >= MIN_SCRUB_LEN && text.contains(*s)).collect();
36    if candidates.is_empty() {
37        return Cow::Borrowed(text);
38    }
39    candidates.sort_by_key(|s| std::cmp::Reverse(s.len()));
40    let mut scrubbed = text.to_owned();
41    for secret in candidates {
42        if scrubbed.contains(secret) {
43            scrubbed = scrubbed.replace(secret, SECRET_MASK);
44        }
45    }
46    Cow::Owned(scrubbed)
47}
48
49/// The plaintext of every secret a run has seen, for scrubbing free text.
50///
51/// Its `Debug` output shows only the number of values.
52#[derive(Default)]
53pub struct SecretSet {
54    values: Mutex<Vec<Arc<str>>>,
55}
56
57impl std::fmt::Debug for SecretSet {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        let count = self.values.lock().map(|v| v.len()).unwrap_or_default();
60        f.debug_struct("SecretSet").field("values", &count).finish()
61    }
62}
63
64impl SecretSet {
65    /// An empty set.
66    pub fn new() -> Self {
67        Self::default()
68    }
69
70    /// Remember the plaintext of `value` and of any secret nested in it.
71    pub fn observe(&self, value: &Value) {
72        match value {
73            Value::Secret(plaintext) => self.insert(plaintext),
74            Value::Array(items) => items.iter().for_each(|item| self.observe(item)),
75            Value::Object(map) => map.values().for_each(|item| self.observe(item)),
76            _ => {}
77        }
78    }
79
80    fn insert(&self, plaintext: &Arc<str>) {
81        if plaintext.len() < MIN_SCRUB_LEN {
82            return;
83        }
84        if let Ok(mut values) = self.values.lock() {
85            if !values.iter().any(|known| known == plaintext) {
86                values.push(Arc::clone(plaintext));
87            }
88        }
89    }
90
91    /// Whether no secret has been observed.
92    pub fn is_empty(&self) -> bool {
93        self.values.lock().map(|v| v.is_empty()).unwrap_or(true)
94    }
95
96    /// `text` with every observed secret replaced by [`SECRET_MASK`].
97    pub fn scrub<'a>(&self, text: &'a str) -> Cow<'a, str> {
98        match self.values.lock() {
99            Ok(values) => redact_secrets(text, &values),
100            // A poisoned lock means a panic while inserting; mask conservatively.
101            Err(_) => Cow::Borrowed(SECRET_MASK),
102        }
103    }
104
105    /// Scrub `message` in place.
106    pub fn scrub_in_place(&self, message: &mut String) {
107        if let Cow::Owned(scrubbed) = self.scrub(message) {
108            *message = scrubbed;
109        }
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn replaces_every_occurrence_longest_first() {
119        let text = "key=sk-abcdef, again sk-abcdef, prefix sk-abc";
120        assert_eq!(redact_secrets(text, &["sk-abc", "sk-abcdef"]), "key=******, again ******, prefix ******");
121    }
122
123    #[test]
124    fn short_values_and_misses_borrow() {
125        assert!(matches!(redact_secrets("abc and xyz", &["abc"]), Cow::Borrowed(_)));
126        assert!(matches!(redact_secrets("nothing here", &["sk-live"]), Cow::Borrowed(_)));
127    }
128
129    #[test]
130    fn set_observes_nested_secrets_and_hides_them_in_debug() {
131        let set = SecretSet::new();
132        set.observe(&Value::from_json(serde_json::json!({"a": [1, 2]})));
133        assert!(set.is_empty());
134        set.observe(&Value::array([Value::Secret(Arc::from("tok-1234")), Value::Secret(Arc::from("no"))]));
135        set.observe(&Value::Secret(Arc::from("tok-1234")));
136        assert_eq!(set.scrub("Bearer tok-1234 rejected; no"), "Bearer ****** rejected; no");
137        assert!(!format!("{set:?}").contains("tok-1234"));
138    }
139}