Skip to content

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

fix(keyring): verify encrypted migration before plaintext cleanup

Steven Enamakelff3bd0c3848d
diff --git a/crates/openhuman-core/src/security/keyring/README.md b/crates/openhuman-core/src/security/keyring/README.md
index 9a59e6b7fc..236a77f7b0 100644
--- a/crates/openhuman-core/src/security/keyring/README.md
+++ b/crates/openhuman-core/src/security/keyring/README.md
@@ -112,7 +112,7 @@ Discovered consumers (`crate::security::keyring::*`):
- A corrupt file is never overwritten. `file` reads degrade to an empty map (so a missing token means "sign in again"), but a `set`/`delete` over an unparseable file quarantines it and returns an error. Returning empty on the write path is what turned a corrupt file into a wipe: the write that followed persisted a map holding nothing but the key being set.
- Mutations hold a cross-process lock, on `<secrets file>.lock` rather than the secrets file itself, because `write_atomic` replaces the file by rename, so a lock on the old inode would guard nothing. Callers must hold it across the read and the write.
- The `SecretStore` master-key file is write-once. On Windows it survives transient AV-scanner sharing violations via retry/backoff and attempts `icacls` ACL self-repair on permission errors. Decoded keys are cached so repeated decrypts (for example, snapshot polls) hit memory.
-- Legacy formats: `SecretStore` migrates `enc:` (XOR) to `enc2:` (ChaCha20-Poly1305) on decrypt; `EncryptedFileBackend` migrates plaintext `dev-keychain.json` to `secrets.enc` (renaming the legacy file `.json.migrated`).
+- Legacy formats: `SecretStore` migrates `enc:` (XOR) to `enc2:` (ChaCha20-Poly1305) on decrypt. `EncryptedFileBackend` writes `dev-keychain.json` into `secrets.enc`, reads back and verifies the encrypted map, then removes the plaintext source. It also removes older `.json.migrated` copies when their entries match the decrypted store. Invalid or divergent copies remain for manual recovery.
- Errors never carry secret values, only namespaced keys. `diagnostic()` is safe to log and preserves the underlying `keyring::Error` variant and `OSStatus`.
## Further reading
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 a816771867..2762299ccc 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -450,16 +450,20 @@ impl EncryptedFileBackend {
}
match crypto::chacha20_decrypt(key, &blob) {
- Ok(plaintext) => serde_json::from_slice::<HashMap<String, String>>(&plaintext)
- .map_err(|e| {
+ Ok(plaintext) => match serde_json::from_slice::<HashMap<String, String>>(&plaintext) {
+ Ok(map) => {
+ self.cleanup_matching_legacy_files(&map)?;
+ Ok(map)
+ }
+ Err(e) => {
log::warn!(
"[keyring:encrypted_file] decrypted data is not valid JSON: {e}; \
treating as corrupt"
);
self.handle_corruption();
- KeyringError::Backend("corrupt secrets file (invalid JSON)".to_string())
- })
- .or_else(|_| Ok(HashMap::new())),
+ Ok(HashMap::new())
+ }
+ },
Err(e) => {
log::error!(
"[keyring:encrypted_file] decryption failed: {e}; master key may have \
@@ -493,6 +497,24 @@ impl EncryptedFileBackend {
if !legacy_path.exists() {
return Ok(HashMap::new());
}
+ // The plaintext backend uses this same sidecar lock. Keep it across
+ // the read, encrypted write, verification, and source removal so a
+ // concurrent plaintext writer cannot lose an update during migration.
+ let _legacy_guard = file_store::lock_for_write(&legacy_path)?;
+ if !legacy_path.exists() {
+ return Ok(HashMap::new());
+ }
+ let metadata = std::fs::symlink_metadata(&legacy_path).map_err(|source| {
+ KeyringError::MigrationReadFailed {
+ path: legacy_path.display().to_string(),
+ source,
+ }
+ })?;
+ if !metadata.file_type().is_file() {
+ return Err(KeyringError::Backend(format!(
+ "legacy {LEGACY_DEV_KEYCHAIN} is not a regular file; preserving it for recovery"
+ )));
+ }
log::info!(
"[keyring:encrypted_file] found legacy {} — migrating to encrypted file",
@@ -507,36 +529,148 @@ impl EncryptedFileBackend {
let map: HashMap<String, String> = if bytes.is_empty() {
HashMap::new()
} else {
- serde_json::from_slice(&bytes).unwrap_or_else(|e| {
- log::warn!(
- "[keyring:encrypted_file] legacy {LEGACY_DEV_KEYCHAIN} is corrupt ({e}); \
- starting fresh"
- );
- HashMap::new()
- })
+ serde_json::from_slice(&bytes).map_err(|e| {
+ KeyringError::Backend(format!(
+ "legacy {LEGACY_DEV_KEYCHAIN} is invalid JSON; preserving it for recovery: {e}"
+ ))
+ })?
};
- if !map.is_empty() {
- self.write_map(key, &map)?;
- }
+ self.write_map(key, &map)?;
+ self.verify_migrated_map(key, &map)?;
- let migrated_path = legacy_path.with_extension("json.migrated");
- if let Err(e) = std::fs::rename(&legacy_path, &migrated_path) {
- log::warn!(
- "[keyring:encrypted_file] could not rename legacy file: {e}; \
- migration still succeeded"
- );
- } else {
- log::info!(
- "[keyring:encrypted_file] legacy {LEGACY_DEV_KEYCHAIN} migrated \
- ({} entries) and renamed to .migrated",
- map.len()
- );
+ std::fs::remove_file(&legacy_path).map_err(|source| {
+ KeyringError::MigrationDeleteFailed {
+ path: legacy_path.display().to_string(),
+ source,
+ }
+ })?;
+ log::info!(
+ "[keyring:encrypted_file] legacy {LEGACY_DEV_KEYCHAIN} migrated \
+ ({} entries), verified, and removed",
+ map.len()
+ );
+ if let Err(e) = self.cleanup_matching_legacy_file(
+ &legacy_path.with_extension("json.migrated"),
+ &map,
+ false,
+ ) {
+ log::warn!("[keyring:encrypted_file] could not clean up older legacy copy: {e}");
}
Ok(map)
}
+ /// Read the encrypted file back without the normal corruption quarantine.
+ /// A failed verification must leave the plaintext source available to retry.
+ fn verify_migrated_map(
+ &self,
+ key: &[u8; KEY_LEN],
+ expected: &HashMap<String, String>,
+ ) -> Result<(), KeyringError> {
+ let blob = std::fs::read(&self.path).map_err(|e| {
+ KeyringError::Backend(format!("cannot read encrypted migration result: {e}"))
+ })?;
+ let plaintext = crypto::chacha20_decrypt(key, &blob)
+ .map_err(|e| KeyringError::Backend(format!("cannot decrypt migration result: {e}")))?;
+ let actual: HashMap<String, String> = serde_json::from_slice(&plaintext).map_err(|e| {
+ KeyringError::Backend(format!("cannot parse decrypted migration result: {e}"))
+ })?;
+ if &actual != expected {
+ return Err(KeyringError::Backend(
+ "encrypted migration result differs from plaintext source".into(),
+ ));
+ }
+ Ok(())
+ }
+
+ /// Remove an old plaintext copy only when every entry is present with the
+ /// same value in the decrypted store. A changed or invalid copy may hold
+ /// data needed for recovery and is left in place for manual inspection.
+ fn cleanup_matching_legacy_file(
+ &self,
+ path: &Path,
+ encrypted: &HashMap<String, String>,
+ strict: bool,
+ ) -> Result<(), KeyringError> {
+ if !path.exists() {
+ return Ok(());
+ }
+ let _guard = file_store::lock_for_write(path)?;
+ let metadata = match std::fs::symlink_metadata(path) {
+ Ok(metadata) => metadata,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
+ Err(source) => {
+ return Err(KeyringError::MigrationReadFailed {
+ path: path.display().to_string(),
+ source,
+ });
+ }
+ };
+ if !metadata.file_type().is_file() {
+ if strict {
+ return Err(KeyringError::Backend(format!(
+ "legacy {LEGACY_DEV_KEYCHAIN} is not a regular file; preserving it for recovery"
+ )));
+ }
+ log::warn!("[keyring:encrypted_file] legacy path {} is not a regular file; leaving it untouched", path.display());
+ return Ok(());
+ }
+ let bytes = std::fs::read(path).map_err(|source| KeyringError::MigrationReadFailed {
+ path: path.display().to_string(),
+ source,
+ })?;
+ let legacy: HashMap<String, String> = match serde_json::from_slice(&bytes) {
+ Ok(map) => map,
+ Err(e) => {
+ if strict {
+ return Err(KeyringError::Backend(format!(
+ "legacy {LEGACY_DEV_KEYCHAIN} is invalid JSON; preserving it for recovery: {e}"
+ )));
+ }
+ log::warn!("[keyring:encrypted_file] legacy copy {} is invalid JSON ({e}); leaving it for recovery", path.display());
+ return Ok(());
+ }
+ };
+ if !legacy
+ .iter()
+ .all(|(key, value)| encrypted.get(key) == Some(value))
+ {
+ if strict {
+ return Err(KeyringError::Backend(format!(
+ "legacy {LEGACY_DEV_KEYCHAIN} differs from encrypted secrets; preserving it for recovery"
+ )));
+ }
+ log::warn!("[keyring:encrypted_file] legacy copy {} differs from encrypted secrets; leaving it for recovery", path.display());
+ return Ok(());
+ }
+ std::fs::remove_file(path).map_err(|source| KeyringError::MigrationDeleteFailed {
+ path: path.display().to_string(),
+ source,
+ })?;
+ log::info!(
+ "[keyring:encrypted_file] removed verified legacy plaintext copy {}",
+ path.display()
+ );
+ Ok(())
+ }
+
+ fn cleanup_matching_legacy_files(
+ &self,
+ encrypted: &HashMap<String, String>,
+ ) -> Result<(), KeyringError> {
+ let legacy_path = self.workspace_dir.join(LEGACY_DEV_KEYCHAIN);
+ self.cleanup_matching_legacy_file(&legacy_path, encrypted, true)?;
+ if let Err(e) = self.cleanup_matching_legacy_file(
+ &legacy_path.with_extension("json.migrated"),
+ encrypted,
+ false,
+ ) {
+ log::warn!("[keyring:encrypted_file] could not clean up older legacy copy: {e}");
+ }
+ Ok(())
+ }
+
/// Move an undecryptable / unparseable secrets file aside so the next call
/// starts fresh without destroying the bytes.
fn handle_corruption(&self) {
diff --git a/docs/gitbooks/en/features/cloud-deploy.md b/docs/gitbooks/en/features/cloud-deploy.md
index 0c7c6b9dce..eb37cce52c 100644
--- a/docs/gitbooks/en/features/cloud-deploy.md
+++ b/docs/gitbooks/en/features/cloud-deploy.md
@@ -447,7 +447,7 @@ Custom cloud providers (`inference_url` and `api_key`, or an entry in `cloud_pro
### Secret storage in containers
-The keyring backend is `encrypted_file` unless `OPENHUMAN_APP_ENV=dev`/`development` or an explicit backend override selects plaintext. Its master key comes from the OS keychain by default. A container normally has no keychain, so a provider-key write fails with `Failed to encrypt api_key` (`[keyring] set error ... master key unavailable`) until you supply `OPENHUMAN_KEYRING_MASTER_KEY` (64 hex characters) or `OPENHUMAN_KEYRING_MASTER_KEY_FILE` (a secret mount). Keep that key stable across restarts; losing it makes stored secrets unreadable. Older releases without these variables supported `OPENHUMAN_KEYRING_BACKEND=file`, which stored plaintext in `$OPENHUMAN_WORKSPACE/dev-keychain.json` (mode `0600`). On upgrade, the encrypted backend migrates that file, but currently leaves a plaintext `.migrated` copy; remove that copy only after confirming the encrypted data is readable.
+The keyring backend is `encrypted_file` unless `OPENHUMAN_APP_ENV=dev`/`development` or an explicit backend override selects plaintext. Its master key comes from the OS keychain by default. A container normally has no keychain, so a provider-key write fails with `Failed to encrypt api_key` (`[keyring] set error ... master key unavailable`) until you supply `OPENHUMAN_KEYRING_MASTER_KEY` (64 hex characters) or `OPENHUMAN_KEYRING_MASTER_KEY_FILE` (a secret mount). Keep that key stable across restarts; losing it makes stored secrets unreadable. Older releases without these variables supported `OPENHUMAN_KEYRING_BACKEND=file`, which stored plaintext in `$OPENHUMAN_WORKSPACE/dev-keychain.json` (mode `0600`). On upgrade, the encrypted backend writes and reads back `secrets.enc` before removing that plaintext file. It also removes older `dev-keychain.json.migrated` copies when their entries match the decrypted store. Copies with different or unreadable contents remain for manual recovery; inspect them before removal.
Mount the key file without group or world write permission. The core rejects group-writable and world-writable files. Read-only group or world access may be accepted for container secret mounts, but any process that can read the file can decrypt the whole keyring. Restrict the mode where the mount is declared:
diff --git a/gitbooks/features/cloud-deploy.md b/gitbooks/features/cloud-deploy.md
index 0c7c6b9dce..eb37cce52c 100644
--- a/gitbooks/features/cloud-deploy.md
+++ b/gitbooks/features/cloud-deploy.md
@@ -447,7 +447,7 @@ Custom cloud providers (`inference_url` and `api_key`, or an entry in `cloud_pro
### Secret storage in containers
-The keyring backend is `encrypted_file` unless `OPENHUMAN_APP_ENV=dev`/`development` or an explicit backend override selects plaintext. Its master key comes from the OS keychain by default. A container normally has no keychain, so a provider-key write fails with `Failed to encrypt api_key` (`[keyring] set error ... master key unavailable`) until you supply `OPENHUMAN_KEYRING_MASTER_KEY` (64 hex characters) or `OPENHUMAN_KEYRING_MASTER_KEY_FILE` (a secret mount). Keep that key stable across restarts; losing it makes stored secrets unreadable. Older releases without these variables supported `OPENHUMAN_KEYRING_BACKEND=file`, which stored plaintext in `$OPENHUMAN_WORKSPACE/dev-keychain.json` (mode `0600`). On upgrade, the encrypted backend migrates that file, but currently leaves a plaintext `.migrated` copy; remove that copy only after confirming the encrypted data is readable.
+The keyring backend is `encrypted_file` unless `OPENHUMAN_APP_ENV=dev`/`development` or an explicit backend override selects plaintext. Its master key comes from the OS keychain by default. A container normally has no keychain, so a provider-key write fails with `Failed to encrypt api_key` (`[keyring] set error ... master key unavailable`) until you supply `OPENHUMAN_KEYRING_MASTER_KEY` (64 hex characters) or `OPENHUMAN_KEYRING_MASTER_KEY_FILE` (a secret mount). Keep that key stable across restarts; losing it makes stored secrets unreadable. Older releases without these variables supported `OPENHUMAN_KEYRING_BACKEND=file`, which stored plaintext in `$OPENHUMAN_WORKSPACE/dev-keychain.json` (mode `0600`). On upgrade, the encrypted backend writes and reads back `secrets.enc` before removing that plaintext file. It also removes older `dev-keychain.json.migrated` copies when their entries match the decrypted store. Copies with different or unreadable contents remain for manual recovery; inspect them before removal.
Mount the key file without group or world write permission. The core rejects group-writable and world-writable files. Read-only group or world access may be accepted for container secret mounts, but any process that can read the file can decrypt the whole keyring. Restrict the mode where the mount is declared: