Skip to content

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

fix(keyring): reserve quarantine names atomically

Steven Enamakele4479f3f37b6
diff --git a/crates/openhuman-core/src/security/keyring/file_store.rs b/crates/openhuman-core/src/security/keyring/file_store.rs
index eef3657306..bddf9394a9 100644
--- a/crates/openhuman-core/src/security/keyring/file_store.rs
+++ b/crates/openhuman-core/src/security/keyring/file_store.rs
@@ -201,34 +201,65 @@ fn temp_path_for(path: &Path) -> PathBuf {
/// a recognisable extension.
pub fn quarantine_corrupt(path: &Path, suffix: &str) -> Option<PathBuf> {
let stamp = chrono::Utc::now().timestamp();
- let mut target = path.with_extension(format!("{suffix}.corrupt.{stamp}"));
- // Two recoveries within one second must not share a name: `rename`
- // replaces an existing destination on every platform, which would destroy
- // the earlier preserved bytes.
- let mut attempt = 1u32;
- while target.exists() {
- target = path.with_extension(format!("{suffix}.corrupt.{stamp}.{attempt}"));
+ let mut attempt = 0u32;
+ loop {
+ let target = if attempt == 0 {
+ path.with_extension(format!("{suffix}.corrupt.{stamp}"))
+ } else {
+ path.with_extension(format!("{suffix}.corrupt.{stamp}.{attempt}"))
+ };
attempt += 1;
- }
- match std::fs::rename(path, &target) {
- Ok(()) => {
- log::error!(
- "[keyring] {} could not be parsed; moved to {} and treated as empty",
- path.display(),
- target.display()
- );
- Some(target)
- }
- Err(e) => {
- log::error!(
- "[keyring] {} could not be parsed and could not be moved aside: {e}",
- path.display()
- );
- None
+ // `hard_link` refuses an existing destination, so the name is
+ // reserved atomically: two processes quarantining in the same second
+ // can never overwrite each other's preserved bytes (a `rename` would
+ // replace the destination on every platform).
+ match std::fs::hard_link(path, &target) {
+ Ok(()) => {
+ if let Err(e) = std::fs::remove_file(path) {
+ log::warn!(
+ "[keyring] quarantined {} but could not remove the original: {e}",
+ path.display()
+ );
+ }
+ return Some(log_quarantined(path, target));
+ }
+ Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
+ Err(link_error) => {
+ if !path.exists() {
+ log::error!(
+ "[keyring] {} could not be parsed and could not be moved aside: {link_error}",
+ path.display()
+ );
+ return None;
+ }
+ // Filesystems without hard links: fall back to a checked rename.
+ if target.exists() {
+ continue;
+ }
+ return match std::fs::rename(path, &target) {
+ Ok(()) => Some(log_quarantined(path, target)),
+ Err(e) => {
+ log::error!(
+ "[keyring] {} could not be parsed and could not be moved aside: {e}",
+ path.display()
+ );
+ None
+ }
+ };
+ }
}
}
}
+fn log_quarantined(path: &Path, target: PathBuf) -> PathBuf {
+ log::error!(
+ "[keyring] {} could not be parsed; moved to {} and treated as empty",
+ path.display(),
+ target.display()
+ );
+ target
+}
+
#[cfg(test)]
#[path = "file_store_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/security/keyring/ops.rs b/crates/openhuman-core/src/security/keyring/ops.rs
index 185bf6b5e6..ac31d60ea4 100644
--- a/crates/openhuman-core/src/security/keyring/ops.rs
+++ b/crates/openhuman-core/src/security/keyring/ops.rs
@@ -67,7 +67,16 @@ pub fn get(user_id: &str, key: &str) -> Result<Option<String>, KeyringError> {
};
log::debug!("[keyring] get (storage)");
get_adopting(secrets, &namespaced_key(user_id, key), key, || {
- process_get(user_id, key).ok().flatten()
+ // The storage backend is authoritative once configured, so a failing
+ // legacy backend must not fail the read; it is logged so a broken OS
+ // keychain does not look like "nothing stored".
+ match process_get(user_id, key) {
+ Ok(value) => value,
+ Err(error) => {
+ log::warn!("[keyring] legacy process-backend read failed during adoption: {error}");
+ None
+ }
+ }
})
}