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()));