Merge branch 'fix-test-keychain': 測試永遠不碰使用者真的鑰匙圈
保險庫金鑰在測試版放在這次測試自己的檔案,鑰匙圈讀取在測試裡一律不做。
This commit is contained in:
commit
4eb7ac6931
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