From 98bc3eed65c52ea3e07dd96f1d4db0ce97240204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Wed, 18 Nov 2026 20:11:36 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B8=AC=E8=A9=A6=E6=B0=B8=E9=81=A0?= =?UTF-8?q?=E4=B8=8D=E7=A2=B0=E4=BD=BF=E7=94=A8=E8=80=85=E7=9C=9F=E7=9A=84?= =?UTF-8?q?=E9=91=B0=E5=8C=99=E5=9C=88=E2=80=94=E2=80=94=E4=BF=9D=E9=9A=AA?= =?UTF-8?q?=E5=BA=AB=E9=87=91=E9=91=B0=E6=94=BE=E5=9C=A8=E9=80=99=E6=AC=A1?= =?UTF-8?q?=E6=B8=AC=E8=A9=A6=E8=87=AA=E5=B7=B1=E7=9A=84=E6=AA=94=E6=A1=88?= =?UTF-8?q?=EF=BC=8C=E9=91=B0=E5=8C=99=E5=9C=88=E8=AE=80=E5=8F=96=E5=9C=A8?= =?UTF-8?q?=E6=B8=AC=E8=A9=A6=E8=A3=A1=E4=B8=80=E5=BE=8B=E4=B8=8D=E5=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 沒設 ANYBOT_VAULT_KEY_FILE 時,全部 cargo test 會用 `security` 讀使用者真的「AnyBot vault key」 (find-generic-password -w,讀不到就 add-generic-password -U 寫一把新的),還會 dump-keychain 列出鑰匙圈。 這一輪的修改常常在跑全部測試,等於每次都拿到使用者的保險庫金鑰。 - vault_store::key_file:ANYBOT_VAULT_KEY_FILE,或測試版一律用 temp 裡這次自己的檔案;read_key、store_key 都先看它。 - vault.rs 的 keychain_login、keychain、keychain_has、legacy_keychain_logins:測試版直接不查。 - 新測試 a_test_keeps_its_vault_key_in_a_file_of_its_own。 已驗證: PATH 最前面放一個只記錄、回失敗的假 security,不設 ANYBOT_VAULT_KEY_FILE 跑全部 cargo test: main 叫了 12 次(find-generic-password 4、add-generic-password 4、dump-keychain 4),這個分支 0 次; 全部 cargo test 669 通過;clippy -D warnings 乾淨。 使用者鑰匙圈裡「AnyBot vault key」的建立和修改時間都是 2026-11-04(只讀屬性、沒讀密碼):沒有被測試改過。 未驗證: Linux 的 secret-tool 路徑(keychain_login/keychain 在測試版一律不查,含 Linux) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha5sXAsDLzxz6UZJgoC --- src/vault.rs | 13 +++++++++++++ src/vault_store.rs | 22 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/vault.rs b/src/vault.rs index 330b328..57d7d56 100644 --- a/src/vault.rs +++ b/src/vault.rs @@ -243,6 +243,10 @@ fn linux_find(host: &str) -> Option { /// One Keychain login by site and account (bringing old AnyBot logins into the vault file). pub fn keychain_login(site: &str, account: &str) -> Option { + // (A test never reads the person's saved passwords.) + if cfg!(test) { + return None; + } if !cfg!(target_os = "macos") { return linux_find(site).filter(|l| l.username == account); } @@ -254,6 +258,9 @@ pub fn keychain_login(site: &str, account: &str) -> Option { } fn keychain(host: &str) -> Option { + if cfg!(test) { + return None; + } if !cfg!(target_os = "macos") { return linux_find(host); } @@ -344,12 +351,18 @@ fn write_index(list: &[(String, String)]) { /// The keychain has this login (no password is read, so no prompt). fn keychain_has(site: &str, account: &str) -> bool { + if cfg!(test) { + return false; + } Command::new("security").args(["find-internet-password", "-s", site, "-a", account]).output().map(|o| o.status.success()).unwrap_or(false) } /// Logins saved before the index existed: keychain items with AnyBot's label, which /// `security dump-keychain` prints either as text or as hex bytes. fn legacy_keychain_logins() -> Vec<(String, String)> { + if cfg!(test) { + return vec![]; + } let Ok(out) = Command::new("security").args(["dump-keychain"]).output() else { return vec![] }; let text = String::from_utf8_lossy(&out.stdout); let hex: String = LABEL.bytes().map(|b| format!("{b:02X}")).collect(); diff --git a/src/vault_store.rs b/src/vault_store.rs index 702426b..78ae322 100644 --- a/src/vault_store.rs +++ b/src/vault_store.rs @@ -358,9 +358,19 @@ fn unhex(s: &str) -> Option> { (0..s.len() / 2).map(|i| u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).ok()).collect() } +/// Where the key lives as a file instead of the keychain: ANYBOT_VAULT_KEY_FILE — and in a test +/// build always a file of this run's own, so a test never reads or writes the person's real +/// 「AnyBot vault key」 (it used to, through `security`, whenever a test forgot the variable). +fn key_file() -> Option { + if let Ok(f) = std::env::var("ANYBOT_VAULT_KEY_FILE") { + return Some(f.into()); + } + cfg!(test).then(|| std::env::temp_dir().join(format!("anybot-test-vault-key-{}", std::process::id()))) +} + /// Read the key from wherever it lives (None = no key yet). fn read_key() -> Option> { - if let Ok(f) = std::env::var("ANYBOT_VAULT_KEY_FILE") { + if let Some(f) = key_file() { return unhex(&std::fs::read_to_string(f).ok()?); } #[cfg(target_os = "macos")] @@ -383,8 +393,8 @@ fn read_key() -> Option> { fn store_key(key: &[u8]) -> Result<(), String> { let h = hex(key); - if let Ok(f) = std::env::var("ANYBOT_VAULT_KEY_FILE") { - return crate::httpd::write_private(std::path::Path::new(&f), h.as_bytes()).map_err(|e| e.to_string()); + if let Some(f) = key_file() { + return crate::httpd::write_private(&f, h.as_bytes()).map_err(|e| e.to_string()); } #[cfg(target_os = "macos")] { @@ -1146,6 +1156,12 @@ mod tests { } /// The store round trip with a scratch key file and data folder. + #[test] + fn a_test_keeps_its_vault_key_in_a_file_of_its_own() { + // With or without ANYBOT_VAULT_KEY_FILE, a test build never asks the keychain. + assert!(key_file().is_some()); + } + #[test] fn store_keeps_secrets_inside() { let dir = std::env::temp_dir().join(format!("anybot-vault-test-{}", std::process::id()));