test: 測試永遠不碰使用者真的鑰匙圈——保險庫金鑰放在這次測試自己的檔案,鑰匙圈讀取在測試裡一律不做
沒設 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha5sXAsDLzxz6UZJgoC
This commit is contained in:
parent
d12a3b8cb0
commit
98bc3eed65
13
src/vault.rs
13
src/vault.rs
|
|
@ -243,6 +243,10 @@ fn linux_find(host: &str) -> Option<Login> {
|
|||
|
||||
/// One Keychain login by site and account (bringing old AnyBot logins into the vault file).
|
||||
pub fn keychain_login(site: &str, account: &str) -> Option<Login> {
|
||||
// (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<Login> {
|
|||
}
|
||||
|
||||
fn keychain(host: &str) -> Option<Login> {
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -358,9 +358,19 @@ fn unhex(s: &str) -> Option<Vec<u8>> {
|
|||
(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<std::path::PathBuf> {
|
||||
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<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
|||
|
||||
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()));
|
||||
|
|
|
|||
Loading…
Reference in New Issue