Skip to content

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

fix(keyring): recheck legacy file under lock before import

Steven Enamakel1dd9e9a2db41
diff --git a/crates/openhuman-core/src/security/keyring/adapter.rs b/crates/openhuman-core/src/security/keyring/adapter.rs
index 66c91598d7..9e7f6f4aed 100644
--- a/crates/openhuman-core/src/security/keyring/adapter.rs
+++ b/crates/openhuman-core/src/security/keyring/adapter.rs
@@ -98,3 +98,7 @@ pub(super) fn recover_corrupt_file(
}
Ok(())
}
+
+#[cfg(test)]
+#[path = "adapter_tests.rs"]
+pub(crate) mod tests;
diff --git a/crates/openhuman-core/src/security/keyring/adapter_tests.rs b/crates/openhuman-core/src/security/keyring/adapter_tests.rs
index 07f8495458..350c0a41d6 100644
--- a/crates/openhuman-core/src/security/keyring/adapter_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/adapter_tests.rs
@@ -2,6 +2,7 @@
//! persistent fake credential store (never the real keychain), the encrypted
//! file backend's desktop corruption policy, and the legacy-file import.
+use super::*;
use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
@@ -9,9 +10,9 @@ use std::sync::{Arc, Mutex};
use keyring::credential::{Credential, CredentialApi, CredentialBuilderApi};
use tinystoragedrivers::secrets::{crypto, KeyringSecrets};
-use super::backend::OsBackend;
-use super::encrypted_file_backend::EncryptedFileBackend;
-use super::{KeyringBackend, KeyringError};
+use crate::security::keyring::backend::OsBackend;
+use crate::security::keyring::encrypted_file_backend::EncryptedFileBackend;
+use crate::security::keyring::KeyringBackend;
type Shared = Arc<Mutex<HashMap<(String, String), Vec<u8>>>>;
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
index 8066843b26..ed63996501 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -476,7 +476,9 @@ impl EncryptedFileBackend {
// The driver's own writes take this same lock, so it is released
// before they run: this import stages the file itself.
let _guard = file_store::lock_for_write(&self.path)?;
- if self.path.exists() {
+ // Rechecked under the lock: another process may have imported (and
+ // renamed) the legacy file between the unlocked check and here.
+ if self.path.exists() || !legacy_path.exists() {
return Ok(());
}
@@ -597,3 +599,7 @@ impl KeyringBackend for EncryptedFileBackend {
#[cfg(test)]
#[path = "encrypted_file_backend_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "encrypted_file_backend_fixture_tests.rs"]
+mod fixture_tests;
diff --git a/crates/openhuman-core/src/security/keyring/fixture_compat_tests.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_fixture_tests.rs
similarity index 97%
rename from crates/openhuman-core/src/security/keyring/fixture_compat_tests.rs
rename to crates/openhuman-core/src/security/keyring/encrypted_file_backend_fixture_tests.rs
index 6be2f4f93f..27a16f843f 100644
--- a/crates/openhuman-core/src/security/keyring/fixture_compat_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_fixture_tests.rs
@@ -7,7 +7,7 @@
//! still read every secret, and a downgrade must still read what the new code
//! writes. Never regenerate them to make a test pass.
-use super::encrypted_file_backend::EncryptedFileBackend;
+use super::*;
use tinystoragedrivers::secrets::crypto;
const FIXTURE_KEY: [u8; 32] = [0x42; 32];
diff --git a/crates/openhuman-core/src/security/keyring/mod.rs b/crates/openhuman-core/src/security/keyring/mod.rs
index 7a43c5857c..05e359d077 100644
--- a/crates/openhuman-core/src/security/keyring/mod.rs
+++ b/crates/openhuman-core/src/security/keyring/mod.rs
@@ -64,14 +64,6 @@ pub use store::init_workspace;
#[allow(unused_imports)]
pub(crate) use ops::force_backend_for_test;
-#[cfg(test)]
-#[path = "adapter_tests.rs"]
-pub(crate) mod adapter_tests;
-
-#[cfg(test)]
-#[path = "fixture_compat_tests.rs"]
-mod fixture_compat_tests;
-
#[cfg(test)]
#[path = "keyring_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/security/keyring/ops_adoption_tests.rs b/crates/openhuman-core/src/security/keyring/ops_adoption_tests.rs
index 684e3bd26b..f5740451c1 100644
--- a/crates/openhuman-core/src/security/keyring/ops_adoption_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/ops_adoption_tests.rs
@@ -1,13 +1,13 @@
//! Migration from the process/OS keyring into storage-backed secrets, with a
//! fake keychain standing in for the OS.
+use super::*;
use std::sync::Arc;
use tinystoragedrivers::secrets::{DerivedKeys, SecretStore as _};
use zeroize::Zeroizing;
-use super::*;
-use crate::security::keyring::adapter_tests::fake_os_backend;
+use crate::security::keyring::adapter::tests::fake_os_backend;
use crate::security::keyring::KeyringBackend;
use crate::storage::{MemoryStorage, Scope, StorageBackend};