Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(keyring): cover encrypted default and migration safeguards

Steven Enamakele46c3c41fb7d
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
index a0a30730da..2911276dd6 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
@@ -1,6 +1,78 @@
use super::*;
use std::cell::{Cell, RefCell};
+fn legacy_secret_map() -> HashMap<String, String> {
+ HashMap::from([("user:token".to_string(), "secret-value".to_string())])
+}
+
+#[test]
+fn legacy_plaintext_is_removed_only_after_verified_encrypted_migration() {
+ let dir = tempfile::tempdir().unwrap();
+ let legacy_path = dir.path().join(LEGACY_DEV_KEYCHAIN);
+ let expected = legacy_secret_map();
+ std::fs::write(&legacy_path, serde_json::to_vec(&expected).unwrap()).unwrap();
+
+ let backend = EncryptedFileBackend::new(dir.path());
+ let key = [0x42; KEY_LEN];
+ assert_eq!(backend.read_map(&key).unwrap(), expected);
+ assert!(!legacy_path.exists());
+ assert!(backend.path.exists());
+ assert_eq!(backend.read_map(&key).unwrap(), expected);
+}
+
+#[test]
+fn invalid_legacy_plaintext_is_preserved_for_recovery() {
+ let dir = tempfile::tempdir().unwrap();
+ let legacy_path = dir.path().join(LEGACY_DEV_KEYCHAIN);
+ std::fs::write(&legacy_path, b"invalid JSON").unwrap();
+
+ let backend = EncryptedFileBackend::new(dir.path());
+ assert!(backend.read_map(&[0x42; KEY_LEN]).is_err());
+ assert_eq!(std::fs::read(&legacy_path).unwrap(), b"invalid JSON");
+ assert!(!backend.path.exists());
+}
+
+#[test]
+fn encrypted_store_removes_only_matching_legacy_plaintext() {
+ let dir = tempfile::tempdir().unwrap();
+ let backend = EncryptedFileBackend::new(dir.path());
+ let key = [0x42; KEY_LEN];
+ let encrypted = legacy_secret_map();
+ backend.write_map(&key, &encrypted).unwrap();
+
+ let legacy_path = dir.path().join(LEGACY_DEV_KEYCHAIN);
+ let mut different = encrypted.clone();
+ different.insert("user:token".to_string(), "different".to_string());
+ std::fs::write(&legacy_path, serde_json::to_vec(&different).unwrap()).unwrap();
+ assert!(backend.read_map(&key).is_err());
+ assert!(legacy_path.exists());
+
+ std::fs::write(&legacy_path, serde_json::to_vec(&encrypted).unwrap()).unwrap();
+ assert_eq!(backend.read_map(&key).unwrap(), encrypted);
+ assert!(!legacy_path.exists());
+}
+
+#[test]
+fn older_plaintext_copy_is_removed_only_when_it_matches() {
+ let dir = tempfile::tempdir().unwrap();
+ let backend = EncryptedFileBackend::new(dir.path());
+ let key = [0x42; KEY_LEN];
+ let encrypted = legacy_secret_map();
+ backend.write_map(&key, &encrypted).unwrap();
+
+ let old_copy = dir
+ .path()
+ .join(LEGACY_DEV_KEYCHAIN)
+ .with_extension("json.migrated");
+ std::fs::write(&old_copy, serde_json::to_vec(&encrypted).unwrap()).unwrap();
+ assert_eq!(backend.read_map(&key).unwrap(), encrypted);
+ assert!(!old_copy.exists());
+
+ std::fs::write(&old_copy, b"invalid JSON").unwrap();
+ assert_eq!(backend.read_map(&key).unwrap(), encrypted);
+ assert_eq!(std::fs::read(&old_copy).unwrap(), b"invalid JSON");
+}
+
/// In-memory fake of the keychain entry, so [`load_or_mint_master_key`] can
/// be exercised without a real OS keychain. `absent_error` is a fn pointer
/// because `keyring::Error` is not `Clone` — we mint a fresh error per call.
diff --git a/crates/openhuman-core/src/security/keyring/store_tests_2_tests.rs b/crates/openhuman-core/src/security/keyring/store_tests_2_tests.rs
index 6d1cdcfed4..9572e22597 100644
--- a/crates/openhuman-core/src/security/keyring/store_tests_2_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/store_tests_2_tests.rs
@@ -31,3 +31,30 @@ fn unknown_backend_override_falls_back_to_environment_default() {
BackendKind::EncryptedFile
);
}
+
+#[test]
+fn unset_app_environment_defaults_to_encrypted_file() {
+ assert_eq!(
+ effective_backend_kind_for(None, None, false),
+ BackendKind::EncryptedFile
+ );
+}
+
+#[test]
+fn only_explicit_development_environments_default_to_plain_file() {
+ for app_env in ["dev", "development", " dev "] {
+ assert_eq!(
+ effective_backend_kind_for(Some(app_env), None, false),
+ BackendKind::File,
+ "{app_env} should explicitly select the development default"
+ );
+ }
+
+ for app_env in ["", "production", "staging", "unknown"] {
+ assert_eq!(
+ effective_backend_kind_for(Some(app_env), None, false),
+ BackendKind::EncryptedFile,
+ "{app_env} must not silently select plaintext storage"
+ );
+ }
+}