Skip to main content

agentmux_srv_core/backend/wconfig/
redact.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Redact secret settings keys from the full-config JSON before it is sent to
5//! any renderer/frontend connection.
6//!
7//! Found in PR #2751 review: `voice:groqApiKey`'s own doc comment claims
8//! "Read server-side only, never sent to the renderer," but `get_full_config()`'s
9//! outbound JSON (the initial `config` event, the `getfullconfig` RPC, and the
10//! post-`setconfig` broadcast — all three call sites in `server/websocket.rs`)
11//! was never actually filtered, so the real plaintext key has always reached
12//! every renderer connection's JS memory. PR #2751 introduced a masked-dot
13//! Settings UI for this key (`MaskedKeyField`) that visually implies the
14//! opposite. This module makes the doc comment's claim actually true.
15//!
16//! The renderer's UI only needs to know a value IS set, never what it is, so
17//! redaction replaces the real value with a fixed non-empty placeholder
18//! (distinguishable from "unset") rather than merely blanking it. The write
19//! path (`setconfig` → `merge_settings_to_disk`/`merge_settings_into_current`)
20//! is untouched — this only filters the server → renderer read direction.
21
22use serde_json::Value;
23
24/// Settings keys whose values must never reach a connection that reads the config: the
25/// renderer's, an agent's. srv itself reads them from its own copy.
26const REDACTED_SETTINGS_KEYS: &[&str] = &[
27    "voice:groqApiKey",
28    "messaging:discord:token",
29    "messaging:telegram:token",
30    "messaging:slack:bot_token",
31    "messaging:slack:app_token",
32    "messaging:whatsapp:access_token",
33    "messaging:whatsapp:app_secret",
34    "messaging:whatsapp:webhook_verify_token",
35];
36
37/// Placeholder written in place of a redacted value. Any renderer code that
38/// merely checks "is a key set" (e.g. `MaskedKeyField`'s locked-state gate)
39/// keeps working unchanged; nothing should ever display this string as if it
40/// were the real value.
41const REDACTED_PLACEHOLDER: &str = "\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}";
42
43/// Redact known-secret keys in-place on an already-serialized full-config
44/// JSON value. Call this on every path that sends config to a renderer;
45/// never on the in-memory `ConfigState` state or the on-disk file.
46pub fn redact_full_config_for_renderer(v: &mut Value) {
47    let Some(settings) = v.get_mut("settings").and_then(|s| s.as_object_mut()) else {
48        return;
49    };
50    for key in REDACTED_SETTINGS_KEYS {
51        let is_set = settings
52            .get(*key)
53            .map(|existing| match existing {
54                // A reference to a Vault entry (`vault:<id>`) holds no secret, and Settings needs it to say
55                // which entry the key is in.
56                Value::String(s) if crate::backend::vault::setting_key::entry_of(s).is_some() => false,
57                Value::String(s) => !s.is_empty(),
58                Value::Null => false,
59                _ => true,
60            })
61            .unwrap_or(false);
62        if is_set {
63            settings.insert((*key).to_string(), Value::String(REDACTED_PLACEHOLDER.to_string()));
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use serde_json::json;
72
73    /// A key kept in the Vault is a reference, not a secret: Settings reads it to name the entry. The
74    /// plain key it replaced is still redacted, whatever the other keys hold.
75    #[test]
76    fn a_vault_reference_reaches_the_renderer_and_a_plain_key_beside_it_does_not() {
77        let mut v = json!({ "settings": { "voice:groqApiKey": "vault:entry-1", "messaging:discord:token": "plain-token" } });
78        redact_full_config_for_renderer(&mut v);
79        assert_eq!(v["settings"]["voice:groqApiKey"], "vault:entry-1");
80        assert_eq!(v["settings"]["messaging:discord:token"], REDACTED_PLACEHOLDER);
81    }
82
83    #[test]
84    fn redacts_a_set_secret_key() {
85        let mut v = json!({ "settings": { "voice:groqApiKey": "sk-real-secret-value" } });
86        redact_full_config_for_renderer(&mut v);
87        assert_eq!(v["settings"]["voice:groqApiKey"], REDACTED_PLACEHOLDER);
88        assert_ne!(v["settings"]["voice:groqApiKey"], "sk-real-secret-value");
89    }
90
91    #[test]
92    fn redacts_every_messaging_secret() {
93        let secrets: Vec<&str> = REDACTED_SETTINGS_KEYS.iter().copied().filter(|k| k.starts_with("messaging:")).collect();
94        assert_eq!(secrets.len(), 7);
95        let mut settings = serde_json::Map::new();
96        for key in &secrets {
97            settings.insert((*key).to_string(), json!("real-secret-value"));
98        }
99        settings.insert("messaging:discord:channel".to_string(), json!("12345"));
100        let mut v = json!({ "settings": settings });
101        redact_full_config_for_renderer(&mut v);
102        for key in &secrets {
103            assert_eq!(v["settings"][*key], REDACTED_PLACEHOLDER, "{key}");
104        }
105        assert_eq!(v["settings"]["messaging:discord:channel"], "12345", "a channel id is not a secret");
106        assert!(!v.to_string().contains("real-secret-value"));
107    }
108
109    #[test]
110    fn leaves_an_unset_secret_key_alone() {
111        let mut v = json!({ "settings": { "term:fontsize": 14 } });
112        redact_full_config_for_renderer(&mut v);
113        assert!(v["settings"].get("voice:groqApiKey").is_none());
114    }
115
116    #[test]
117    fn does_not_touch_unrelated_settings() {
118        let mut v = json!({ "settings": { "voice:groqApiKey": "sk-real", "term:fontsize": 14 } });
119        redact_full_config_for_renderer(&mut v);
120        assert_eq!(v["settings"]["term:fontsize"], 14);
121    }
122
123    #[test]
124    fn handles_missing_settings_object_gracefully() {
125        let mut v = json!({ "mimetypes": {} });
126        redact_full_config_for_renderer(&mut v); // must not panic
127        assert_eq!(v, json!({ "mimetypes": {} }));
128    }
129
130    #[test]
131    fn treats_empty_string_as_unset() {
132        let mut v = json!({ "settings": { "voice:groqApiKey": "" } });
133        redact_full_config_for_renderer(&mut v);
134        assert_eq!(v["settings"]["voice:groqApiKey"], "");
135    }
136}