Skip to main content

agentmux_common/
secret_shape.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//! The shape of a secret in text a page held, for the places that hand page text to an agent (#47628, #47656).
4//!
5//! The Browser reads (`agentmux-cef`'s `hidden_secrets`) withhold hidden text that has this shape, and so does
6//! every string that comes back with an answer: a click's name in the waiting notice, a line of the pane's
7//! Log, the state an action reports. One rule, here, so they agree.
8use crate::shown_once::has_shown_once;
9
10/// What an agent reads in place of withheld text.
11pub const WITHHELD: &str = "[hidden text withheld: it may be a secret; use Browser capture to save it]";
12
13/// What an agent reads in place of a secret the page shows in plain sight, in a row named like one: it is
14/// there, and `Browser` `capture` saves it, but its text stays out of the agent's context.
15pub const UNREAD: &str = "[unread secret: use Browser capture to save it]";
16
17/// Prefixes of keys, tokens and signing secrets that are short enough to miss the one-time shape.
18const KEY_PREFIXES: &[&str] = &["sk_", "rk_", "pk_", "whsec_", "ghp_", "gho_", "ghs_", "github_pat_", "xoxb-", "xoxp-", "xoxa-", "eyj"];
19
20/// A cloud access key id: four capital letters and sixteen capitals or digits, as one word.
21fn is_access_key_id(t: &str) -> bool {
22    t.len() == 20 && (t.starts_with("AKIA") || t.starts_with("ASIA")) && t.bytes().all(|b| b.is_ascii_uppercase() || b.is_ascii_digit())
23}
24
25/// Whether `text` holds something shaped like a secret: what a page shows once, a key with a known prefix, an
26/// access key id or a PEM header.
27pub fn has_key_shape(text: &str) -> bool {
28    has_shown_once(text)
29        || text.to_ascii_lowercase().contains("-----begin")
30        || text.split_whitespace().any(|t| {
31            // A word in quotes or beside a comma is still the word.
32            let t = t.trim_matches(|c: char| !c.is_alphanumeric());
33            let lower = t.to_ascii_lowercase();
34            is_access_key_id(t) || (t.len() >= 12 && KEY_PREFIXES.iter().any(|p| lower.starts_with(p)))
35        })
36}
37
38/// One long word with a digit and a letter in it, which words are not: an odd-shaped value (an id, a key a
39/// page makes up).
40pub fn token_like(text: &str) -> bool {
41    let t = text.trim();
42    t.chars().count() >= 12
43        && !t.chars().any(char::is_whitespace)
44        && t.chars().any(|c| c.is_ascii_digit())
45        && t.chars().any(char::is_alphabetic)
46        && t.chars().all(|c| c.is_alphanumeric() || "-_=+/.:".contains(c))
47}
48
49/// `text` with every word that has a secret's shape replaced by [`WITHHELD`], and the rest as it was (line
50/// breaks and spacing too); the text itself when no word has one.
51pub fn scrub(text: &str) -> String {
52    if !has_key_shape(text) {
53        return text.to_string();
54    }
55    let mut out = String::with_capacity(text.len());
56    let mut word = String::new();
57    let mut flush = |out: &mut String, word: &mut String| {
58        out.push_str(if has_key_shape(word) { WITHHELD } else { word });
59        word.clear();
60    };
61    for c in text.chars() {
62        if c.is_whitespace() {
63            flush(&mut out, &mut word);
64            out.push(c);
65        } else {
66            word.push(c);
67        }
68    }
69    flush(&mut out, &mut word);
70    out
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn a_secret_word_is_replaced_and_the_spacing_around_it_is_kept() {
79        assert_eq!(scrub("Copy sk_live_abcdefgh12 now"), format!("Copy {WITHHELD} now"));
80        assert_eq!(scrub("a\nghp_abcdefghij12\n\nb"), format!("a\n{WITHHELD}\n\nb"));
81        assert_eq!(scrub("Account settings"), "Account settings");
82        assert_eq!(scrub("Click \"Copy sk_live_abcdefgh12\" on https://a.test"), format!("Click \"Copy {WITHHELD} on https://a.test"));
83    }
84
85    #[test]
86    fn ordinary_words_and_odd_tokens_are_told_apart() {
87        for word in ["Asia/Kolkata", "Asiatic", "keyboard", "2026-11-14"] {
88            assert!(!has_key_shape(word), "{word}");
89        }
90        assert!(token_like("q7Zp-93xaLm2Rt") && !token_like("At least eight characters") && !token_like("Reveal"));
91    }
92}