agentmux_srv_core/backend/transcript/
withhold_sources.rs1use serde_json::Value;
19
20use super::withhold;
21
22const SECRET_WORDS: [&str; 9] = ["token", "key", "secret", "password", "passwd", "auth", "credential", "bearer", "cookie"];
24
25fn hold(owner: &str, value: &str, expires_at: Option<i64>) {
27 if value.len() < withhold::MIN_LEN {
28 return;
29 }
30 match expires_at {
31 Some(at) if at > agentmux_common::time::now_ms() => withhold::hold_until(owner, value, at),
32 _ => withhold::hold_kept(owner, value),
33 }
34}
35
36fn secret_name(name: &str) -> bool {
37 let name = name.to_ascii_lowercase();
38 SECRET_WORDS.iter().any(|w| name.contains(w))
39}
40
41fn walk(owner: &str, value: &Value, in_secret: bool) -> bool {
43 match value {
44 Value::String(s) if in_secret && !s.is_empty() => {
45 hold(owner, s, None);
46 true
47 }
48 Value::Object(fields) => fields.iter().fold(false, |any, (name, inner)| {
49 walk(&format!("{owner}.{name}"), inner, in_secret || secret_name(name)) | any
50 }),
51 Value::Array(items) => items.iter().enumerate().fold(false, |any, (i, inner)| walk(&format!("{owner}.{i}"), inner, in_secret) | any),
52 _ => false,
53 }
54}
55
56pub fn keychain(account_id: &str, secret: &str) {
60 let owner = format!("keychain:{account_id}");
61 let trimmed = secret.trim_start();
62 if trimmed.starts_with('{') {
63 if let Ok(json) = serde_json::from_str::<Value>(secret) {
64 if walk(&owner, &json, false) {
65 return;
66 }
67 }
68 }
69 hold(&owner, secret, None);
70}
71
72pub fn account_key(entry: &str, key: &str) {
74 hold(&format!("account-key:{entry}"), key, None);
75}
76
77pub fn cloud_sign_in(access: &str, refresh: &str, id: &str, expires_at: i64) {
80 let until = (expires_at > 0).then(|| expires_at.saturating_mul(1000));
81 hold("cloud:access", access, until);
82 hold("cloud:id", id, until);
83 hold("cloud:refresh", refresh, None);
84}
85
86pub fn agent_relay_credential(agent_id: &str, client_secret: &str, access: &str, expires_at: i64) {
88 hold(&format!("relay-credential:{agent_id}:secret"), client_secret, None);
89 hold(&format!("relay-credential:{agent_id}:access"), access, (expires_at > 0).then(|| expires_at.saturating_mul(1000)));
90}
91
92pub fn bridge_token(bridge: &str, token: &str) {
94 hold(&format!("bridge:{bridge}"), token, None);
95}
96
97pub fn env_credential(agent_id: &str, name: &str, value: &str) {
101 let value = value.trim();
102 if value.is_empty() || !crate::backend::agent_env::is_credential_name(name) {
103 return;
104 }
105 if crate::backend::agent_env::vault_ref(value).is_some() || crate::backend::agent_env::is_placeholder(value) {
106 return;
107 }
108 hold(&format!("env:{agent_id}:{}", name.to_ascii_uppercase()), value, None);
109}
110
111pub fn relay_token(token: &str) {
113 hold("relay:container-proxy", token, None);
114}