From 648e8304b0a7958901f40784301980e0e68d6050 Mon Sep 17 00:00:00 2001 From: javis-bot Date: Fri, 13 Nov 2026 10:11:42 +0900 Subject: [PATCH] fix(app): keep masking the secrets the running services got after a key is replaced or removed The log hub masks the stored secrets and the values the services were started with (bot token, MCP environment values), so a late worker or MCP line carrying a key replaced or removed while the services run stays masked on the screen, in copies, files and exports; the next start replaces the in-use values. Co-Authored-By: Claude Opus 5.5 --- app/src/admin.spec.md | 11 +++++---- app/src/logs.rs | 54 ++++++++++++++++++++++++++++++++++++++++--- app/src/services.rs | 28 ++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) diff --git a/app/src/admin.spec.md b/app/src/admin.spec.md index ddbaf64..3f6e70c 100644 --- a/app/src/admin.spec.md +++ b/app/src/admin.spec.md @@ -112,9 +112,12 @@ STT, LLM and TTS lines go to `stt`, `llm`, `tts` by their worker category), `bro left out and a note says how many. - Every line runs through `redact` before it is kept or written to a file, and every copy and export again: each stored secret's value, and anything shaped like a Discord bot token (also with - sentence dots around it), becomes `[비밀값]`. So a secret replaced or removed later (the hub then - no longer knows the old value) never shows on the screen, in a copy, a file or an export, also - after a restart. + sentence dots around it), becomes `[비밀값]`. So a secret replaced or removed later never shows on + the screen, in a copy, a file or an export, also after a restart. The values masked are the + stored secrets and the ones the running services were started with (the bot token, the MCP + servers' values; `set_secrets_in_use` on each start): a key replaced or removed while the + services run stays masked in their late lines (the worker and MCP servers keep it until they are + restarted), until the next start gives them only the current values. - Errors the voice worker reports to the app (`error` messages: code and request id) are always logged, debug logging or not, so the log screen and the error report show them. @@ -366,7 +369,7 @@ the key, the feature tools named as the brain names them, only enabled servers i settings without values, stored values passed for enabled servers only, values of removed servers and names deleted, a value refused for an unsaved server, kept out of the settings file and masked in logs, also by a new app on the same folder, and a value replaced or removed later still masked -in lines written before); chat channel names and topics (only new or edited +in lines written before and in lines the running services print afterwards, until the next start); chat channel names and topics (only new or edited syncing channels sent, the topic limit refused before saving, Discord's form of the name written back, a missing permission failing with the reason and the text kept, a rate-limited edit pending and retried once with the latest text, a new channel never clearing Discord (also on its retry), a diff --git a/app/src/logs.rs b/app/src/logs.rs index d15e4d0..1425e5f 100644 --- a/app/src/logs.rs +++ b/app/src/logs.rs @@ -65,9 +65,20 @@ struct State { next_seq: u64, files: HashMap<(String, NaiveDate), std::fs::File>, secrets: Vec, + /// The values the running services were started with (`set_secrets_in_use`). + in_use: Vec, last_cleanup: Option, } +impl State { + /// Every value to mask: the stored secrets and those the running services use. + fn masked(&self) -> Vec { + let mut all = self.secrets.clone(); + all.extend(self.in_use.iter().filter(|v| !self.secrets.contains(v)).cloned()); + all + } +} + pub struct LogHub { state: Mutex, } @@ -96,6 +107,13 @@ impl LogHub { self.state.lock().unwrap().secrets = values; } + /// The secret values the running services were started with (the bot token, the MCP servers' + /// values). They stay masked until the next start, also after one is replaced or removed in + /// the store: the running worker and MCP servers keep using them until they are restarted. + pub fn set_secrets_in_use(&self, values: Vec) { + self.state.lock().unwrap().in_use = values; + } + pub fn push(&self, level: Level, category: &str, text: &str) { self.push_at(Local::now(), level, tab_of(category), text); } @@ -120,7 +138,7 @@ impl LogHub { let mut s = self.state.lock().unwrap(); // Masked before it is kept or written, so a secret replaced or removed later (the hub then // no longer knows the old value) never shows on the screen, in a copy, a file or an export. - let text = redact(text, &s.secrets); + let text = redact(text, &s.masked()); let line = LogLine { seq: s.next_seq, at_ms: at.timestamp_millis(), level, tab: tab.into(), text }; s.next_seq += 1; if let Some(dir) = s.dir.clone() { @@ -197,7 +215,7 @@ impl LogHub { text.push_str(&format_line(l)); text.push('\n'); } - Some(redact(&text, &s.secrets)) + Some(redact(&text, &s.masked())) } /// The log files of days `from..=to` in a zip at `out`, masked, at most `ZIP_MAX_BYTES`: when @@ -205,7 +223,7 @@ impl LogHub { pub fn export_zip(&self, from: NaiveDate, to: NaiveDate, out: &Path) -> std::io::Result { let (secrets, dir) = { let s = self.state.lock().unwrap(); - (s.secrets.clone(), s.dir.clone()) + (s.masked(), s.dir.clone()) }; let Some(dir) = dir else { return Err(std::io::Error::other("no log folder")) }; let mut files: Vec<(NaiveDate, String, Vec)> = vec![]; @@ -506,6 +524,36 @@ mod tests { assert!(!text.contains("old-api-key-1")); } + #[test] + fn a_key_replaced_while_the_services_run_stays_masked_in_their_late_lines_until_the_next_start() { + let dir = temp_dir("loginuse"); + let hub = LogHub::new(Some(&dir)); + let today = Local::now().date_naive(); + // The services start with the old key. + hub.set_secrets(vec!["old-api-key-1".into()]); + hub.set_secrets_in_use(vec!["old-api-key-1".into()]); + // While they run the key is replaced (or its server removed): only the new one is stored. + hub.set_secrets(vec!["new-api-key-2".into()]); + // A late line of the running worker still carries the old key. + hub.push_worker("[ llm ] Error: MCP server brave failed: bad key old-api-key-1"); + let shown: String = hub.query(None, Level::Debug, None, None, 100).iter().map(|l| l.text.clone()).collect(); + assert!(shown.contains("bad key [비밀값]") && !shown.contains("old-api-key-1"), "{shown}"); + assert!(!hub.recent_error_report("0.1.0").unwrap().contains("old-api-key-1")); + let file = std::fs::read_to_string(dir.join(format!("llm-{today}.log"))).unwrap(); + assert!(!file.contains("old-api-key-1"), "{file}"); + let out = dir.join("inuse.zip"); + hub.export_zip(today, today, &out).unwrap(); + let mut z = zip::ZipArchive::new(std::fs::File::open(&out).unwrap()).unwrap(); + let mut text = String::new(); + std::io::Read::read_to_string(&mut z.by_name(&format!("llm-{today}.log")).unwrap(), &mut text).unwrap(); + assert!(!text.contains("old-api-key-1")); + // The next start gives the services only the new key: the old one is no longer held. + hub.set_secrets_in_use(vec!["new-api-key-2".into()]); + hub.push(Level::Info, "app", "after restart old-api-key-1 new-api-key-2"); + let last = hub.query(None, Level::Debug, None, None, 1).pop().unwrap().text; + assert_eq!(last, "after restart old-api-key-1 [비밀값]"); + } + #[test] fn the_zip_stays_under_the_limit_by_leaving_out_the_oldest_lines() { let dir = temp_dir("logzip"); diff --git a/app/src/services.rs b/app/src/services.rs index 676e4e7..b8b22f3 100644 --- a/app/src/services.rs +++ b/app/src/services.rs @@ -49,6 +49,12 @@ pub async fn start_services(data_dir: &Path, llm: Arc) -> Result std::env::var("JARVIS_DISCORD_TOKEN").map_err(|_| "no bot token: set it in the app or JARVIS_DISCORD_TOKEN")?, }; + // What these services are given stays masked in the logs until the next start, also if it is + // replaced or removed in the store meanwhile (admin.spec.md, "Logs"). + if let Some(hub) = crate::logs::installed() { + hub.set_secrets_in_use(secrets_in_use(&token, &mcp_env)); + } + let (base_url, model) = llm.start(&settings).await?; let brain_config = match crate::strata::write_brain_config(data_dir, &base_url, &model, &settings) { Ok(f) => f, @@ -106,6 +112,15 @@ pub async fn start_services(data_dir: &Path, llm: Arc) -> Result Vec { + let mut v = vec![token.to_owned()]; + for vars in mcp_env.as_object().into_iter().flat_map(|m| m.values()) { + v.extend(vars.as_object().into_iter().flat_map(|m| m.values()).filter_map(Value::as_str).map(str::to_owned)); + } + v +} + /// Stop the LLM after a failed start; the error also says if its server may still run. async fn after_llm_stop(llm: &Llm, error: String) -> String { match llm.stop().await { @@ -113,3 +128,16 @@ async fn after_llm_stop(llm: &Llm, error: String) -> String { Err(stop) => format!("{error} ({stop})"), } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_services_secrets_are_the_token_and_every_mcp_value() { + let env = serde_json::json!({"brave": {"BRAVE_API_KEY": "k1"}, "other": {"A": "k2", "B": "k3"}}); + let mut v = secrets_in_use("tok", &env); + v.sort(); + assert_eq!(v, vec!["k1", "k2", "k3", "tok"]); + } +}