agentmux_common/
secret_shape.rs1use crate::shown_once::has_shown_once;
9
10pub const WITHHELD: &str = "[hidden text withheld: it may be a secret; use Browser capture to save it]";
12
13pub const UNREAD: &str = "[unread secret: use Browser capture to save it]";
16
17const KEY_PREFIXES: &[&str] = &["sk_", "rk_", "pk_", "whsec_", "ghp_", "gho_", "ghs_", "github_pat_", "xoxb-", "xoxp-", "xoxa-", "eyj"];
19
20fn 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
25pub 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 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
38pub 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
49pub 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}