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 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-13 10:11:42 +09:00
parent 8b3905f1c2
commit 648e8304b0
3 changed files with 86 additions and 7 deletions

View File

@@ -65,9 +65,20 @@ struct State {
next_seq: u64,
files: HashMap<(String, NaiveDate), std::fs::File>,
secrets: Vec<String>,
/// The values the running services were started with (`set_secrets_in_use`).
in_use: Vec<String>,
last_cleanup: Option<NaiveDate>,
}
impl State {
/// Every value to mask: the stored secrets and those the running services use.
fn masked(&self) -> Vec<String> {
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<State>,
}
@@ -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<String>) {
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<usize> {
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<String>)> = 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");