agentmux_common/
shown_once.rs1pub const SHOWN_ONCE: &str = "[secret: shown once; use Browser capture to save it]";
16
17const MIN: usize = 20;
18const MAX: usize = 512;
19
20fn 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
33pub 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
53pub fn has_shown_once(text: &str) -> bool {
55 text.split_whitespace().any(looks_shown_once)
56}
57
58pub 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 "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}