Skip to main content

agentmux_common/
shown_once.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A secret a web page shows once: a new OAuth client secret, an app registration's
5//! client secret, a key next to a Copy button
6//! (docs/specs/SPEC_VAULT_GENERATE_CAPTURE_AND_BROWSER_SIGNINS_2026_10_28.md ยง15.1).
7//!
8//! The shape alone would hide too much (a long identifier, a path), so callers use it
9//! only where a page shows such a value: a read-only field, a `code`, `pre`, `kbd` or
10//! `samp` element, or the row or container of a button named like "Copy". There, a
11//! value with the shape is hidden from the agent, which saves it with `Browser`
12//! `capture` instead of reading it.
13
14/// What the agent sees in place of the value.
15pub const SHOWN_ONCE: &str = "[secret: shown once; use Browser capture to save it]";
16
17const MIN: usize = 20;
18const MAX: usize = 512;
19
20/// `8-4-4-4-12` hex, with or without braces: an Azure client or tenant id, which
21/// isn't secret.
22fn is_guid(s: &str) -> bool {
23    let s = s.strip_prefix('{').and_then(|s| s.strip_suffix('}')).unwrap_or(s);
24    let groups: Vec<&str> = s.split('-').collect();
25    groups.len() == 5 && groups.iter().zip([8, 4, 4, 4, 12]).all(|(g, n)| g.len() == n && g.bytes().all(|b| b.is_ascii_hexdigit()))
26}
27
28fn is_url(s: &str) -> bool {
29    let lower = s.to_ascii_lowercase();
30    lower.contains("://") || lower.starts_with("www.") || lower.starts_with("mailto:")
31}
32
33/// Whether one whitespace-free piece of text has the shape of a one-time secret:
34/// 20 to 512 characters, at least two of lowercase, uppercase, digits and symbols,
35/// and not a GUID, pure hex (a commit SHA, which agents need) or a URL.
36pub fn looks_shown_once(token: &str) -> bool {
37    let n = token.chars().count();
38    if !(MIN..=MAX).contains(&n) || token.chars().any(char::is_whitespace) {
39        return false;
40    }
41    if is_guid(token) || token.bytes().all(|b| b.is_ascii_hexdigit()) || is_url(token) {
42        return false;
43    }
44    let classes = [
45        token.chars().any(|c| c.is_lowercase()),
46        token.chars().any(|c| c.is_uppercase()),
47        token.chars().any(|c| c.is_ascii_digit()),
48        token.chars().any(|c| !c.is_alphanumeric()),
49    ];
50    classes.iter().filter(|c| **c).count() >= 2
51}
52
53/// Whether `text` holds a value with that shape.
54pub fn has_shown_once(text: &str) -> bool {
55    text.split_whitespace().any(looks_shown_once)
56}
57
58/// `text` with every value of that shape replaced by [`SHOWN_ONCE`]; the rest, and
59/// its spacing, as it was.
60pub fn mask(text: &str) -> String {
61    if !has_shown_once(text) {
62        return text.to_string();
63    }
64    let mut out = String::with_capacity(text.len());
65    let mut word = String::new();
66    let flush = |word: &mut String, out: &mut String| {
67        if looks_shown_once(word) {
68            out.push_str(SHOWN_ONCE);
69        } else {
70            out.push_str(word);
71        }
72        word.clear();
73    };
74    for c in text.chars() {
75        if c.is_whitespace() {
76            flush(&mut word, &mut out);
77            out.push(c);
78        } else {
79            word.push(c);
80        }
81    }
82    flush(&mut word, &mut out);
83    out
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn real_one_time_secrets_have_the_shape() {
92        for s in [
93            // A GitHub OAuth app client secret is 40 hex: not masked by shape (see below), but
94            // a GitHub App's or an Azure client secret is.
95            "Xy8Q~abcDEF1234ghijKLmnoPQRstu.vwxYZ0123",
96            "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
97            "tok_Example_AbCdEfGhIjKlMnOpQr2",
98            "AbCdEfGhIjKlMnOpQrStUvWx",
99        ] {
100            assert!(looks_shown_once(s), "{s}");
101        }
102    }
103
104    #[test]
105    fn ids_shas_urls_and_words_are_left_alone() {
106        for s in [
107            "0f4c2a1e-9b7d-4e3a-8c6f-1a2b3c4d5e6f",
108            "{0F4C2A1E-9B7D-4E3A-8C6F-1A2B3C4D5E6F}",
109            "c58ebaa8e2f1b0d9c7a6e5f4d3c2b1a098765432",
110            "https://github.com/settings/applications/new",
111            "www.example.com/a/very/long/path/here",
112            "internationalization",
113            "short-Secret-1",
114            "this is twenty-plus chars but has spaces",
115        ] {
116            assert!(!looks_shown_once(s), "{s}");
117        }
118        assert!(!looks_shown_once(&"aB3".repeat(200)), "longer than 512");
119    }
120
121    #[test]
122    fn mask_replaces_only_the_secret_and_keeps_the_rest() {
123        let text = "Client secret:  Xy8Q~abcDEF1234ghijKLmnoPQRstu.vwxYZ0123 (copy it now)";
124        assert_eq!(mask(text), format!("Client secret:  {SHOWN_ONCE} (copy it now)"));
125        assert_eq!(mask("nothing secret here"), "nothing secret here");
126        assert!(has_shown_once(text) && !has_shown_once("app id 0f4c2a1e-9b7d-4e3a-8c6f-1a2b3c4d5e6f"));
127    }
128}