Skip to content

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

fix(keyring): retry durable publication before removing legacy copy

Steven Enamakel653b2a318d00
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 193a320f5b..f733c9e579 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -612,6 +612,21 @@ impl EncryptedFileBackend {
path: &Path,
encrypted: &HashMap<String, String>,
strict: bool,
+ ) -> Result<(), KeyringError> {
+ self.cleanup_matching_legacy_file_with_sync(
+ path,
+ encrypted,
+ strict,
+ file_store::sync_parent_dir,
+ )
+ }
+
+ fn cleanup_matching_legacy_file_with_sync(
+ &self,
+ path: &Path,
+ encrypted: &HashMap<String, String>,
+ strict: bool,
+ sync_encrypted_parent: impl FnOnce(&Path) -> Result<(), KeyringError>,
) -> Result<(), KeyringError> {
if !path.exists() {
return Ok(());
@@ -664,6 +679,10 @@ impl EncryptedFileBackend {
log::warn!("[keyring:encrypted_file] legacy copy {} differs from encrypted secrets; leaving it for recovery", path.display());
return Ok(());
}
+ // A previous write may have renamed secrets.enc successfully and then
+ // failed its directory sync. Recheck durability before deleting any
+ // surviving plaintext copy on a later read.
+ sync_encrypted_parent(&self.path)?;
std::fs::remove_file(path).map_err(|source| KeyringError::MigrationDeleteFailed {
path: path.display().to_string(),
source,
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 4e7251c14e..8d92f616ed 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
@@ -77,6 +77,31 @@ fn encrypted_store_remains_readable_when_verified_plaintext_cannot_be_deleted()
);
}
+#[test]
+fn existing_encrypted_file_does_not_remove_plaintext_until_directory_sync_succeeds() {
+ let dir = tempfile::tempdir().unwrap();
+ let backend = EncryptedFileBackend::new(dir.path());
+ let key = [0x42; KEY_LEN];
+ let expected = legacy_secret_map();
+ backend.write_map(&key, &expected).unwrap();
+ let legacy_path = dir.path().join(LEGACY_DEV_KEYCHAIN);
+ std::fs::write(&legacy_path, serde_json::to_vec(&expected).unwrap()).unwrap();
+
+ let result = backend.read_map_with_cleanup(&key, |map| {
+ backend.cleanup_matching_legacy_file_with_sync(&legacy_path, map, true, |_| {
+ Err(KeyringError::Backend("directory sync failed".into()))
+ })
+ });
+ assert!(result.is_err());
+ assert!(
+ legacy_path.exists(),
+ "plaintext must remain until encrypted publication is durable"
+ );
+
+ assert_eq!(backend.read_map(&key).unwrap(), expected);
+ assert!(!legacy_path.exists());
+}
+
#[test]
fn older_plaintext_copy_is_removed_only_when_it_matches() {
let dir = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/security/keyring/file_store.rs b/crates/openhuman-core/src/security/keyring/file_store.rs
index 34ff35b00f..859f424aec 100644
--- a/crates/openhuman-core/src/security/keyring/file_store.rs
+++ b/crates/openhuman-core/src/security/keyring/file_store.rs
@@ -169,6 +169,56 @@ fn replace_durably(source: &Path, destination: &Path) -> std::io::Result<()> {
replace_durably_with_sync(source, destination, |parent| File::open(parent)?.sync_all())
}
+/// Ensure a previously published encrypted file is durable before removing a
+/// matching plaintext migration source. This retries an earlier publication
+/// that returned an error after the destination had already appeared.
+pub fn sync_parent_dir(path: &Path) -> Result<(), KeyringError> {
+ #[cfg(unix)]
+ {
+ let parent = path.parent().ok_or_else(|| {
+ KeyringError::Backend(format!("keyring path {} has no parent", path.display()))
+ })?;
+ File::open(parent)
+ .and_then(|directory| directory.sync_all())
+ .map_err(|error| {
+ KeyringError::Backend(format!(
+ "could not sync keyring directory {}: {error}",
+ parent.display()
+ ))
+ })
+ }
+ #[cfg(windows)]
+ {
+ // A failed MoveFileExW can leave the destination visible. Republish
+ // the same bytes with WRITE_THROUGH and verify the result before the
+ // caller deletes the last plaintext copy.
+ sync_parent_dir_with_republish(path, write_atomic)
+ }
+}
+
+#[cfg(windows)]
+fn sync_parent_dir_with_republish(
+ path: &Path,
+ republish: impl FnOnce(&Path, &[u8]) -> Result<(), KeyringError>,
+) -> Result<(), KeyringError> {
+ let expected = std::fs::read(path).map_err(|source| KeyringError::MigrationReadFailed {
+ path: path.display().to_string(),
+ source,
+ })?;
+ republish(path, &expected)?;
+ let actual = std::fs::read(path).map_err(|source| KeyringError::MigrationReadFailed {
+ path: path.display().to_string(),
+ source,
+ })?;
+ if actual != expected {
+ return Err(KeyringError::Backend(format!(
+ "durable keyring publication at {} failed verification",
+ path.display()
+ )));
+ }
+ Ok(())
+}
+
#[cfg(unix)]
fn replace_durably_with_sync(
source: &Path,
diff --git a/crates/openhuman-core/src/security/keyring/file_store_tests.rs b/crates/openhuman-core/src/security/keyring/file_store_tests.rs
index 65bdafc015..d4e2b23147 100644
--- a/crates/openhuman-core/src/security/keyring/file_store_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/file_store_tests.rs
@@ -26,6 +26,22 @@ fn durable_replace_syncs_parent_after_rename_and_propagates_failure() {
assert!(!source.exists());
}
+#[cfg(windows)]
+#[test]
+fn existing_ciphertext_is_republished_and_verified_before_plaintext_cleanup() {
+ let dir = tempfile::TempDir::new().unwrap();
+ let path = dir.path().join("secrets.enc");
+ std::fs::write(&path, b"ciphertext").unwrap();
+
+ super::sync_parent_dir_with_republish(&path, |destination, bytes| {
+ assert_eq!(destination, path);
+ assert_eq!(bytes, b"ciphertext");
+ super::write_atomic(destination, bytes)
+ })
+ .unwrap();
+ assert_eq!(std::fs::read(&path).unwrap(), b"ciphertext");
+}
+
#[test]
fn write_atomic_replaces_contents() {
let dir = tempfile::TempDir::new().unwrap();