Skip to content

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

fix(storage): abort credential migration on unreadable files

Steven Enamakel38164ac9e468
diff --git a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs
index 505558994a..aa32f0414e 100644
--- a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs
@@ -173,14 +173,16 @@ pub(crate) fn storage_master_key_resolvable() -> bool {
if matches!(MASTER_KEY.get(), Some(Ok(Some(_)))) {
return true;
}
- let host_denied = HOST_KEY_UNAVAILABLE.get().is_some();
+ let configured = env_set(MASTER_KEY_ENV) || env_set(MASTER_KEY_FILE_ENV);
+ // A configured key or a denied keychain skips the host probe entirely.
+ let host_skipped = configured || HOST_KEY_UNAVAILABLE.get().is_some();
let dir = crate::security::secret_storage::store::workspace_dir_for_file_backend();
resolvable(
false,
- env_set(MASTER_KEY_ENV) || env_set(MASTER_KEY_FILE_ENV),
+ configured,
// A host source counts only when it already holds the key (an empty
// one would have a key minted into it by the load), and is read last.
- !host_denied
+ !host_skipped
&& key_source::installed_master_key_source()
.is_some_and(|source| matches!(source.load(), Ok(Some(_)))),
dir.join("keyring/master.key").is_file(),
diff --git a/crates/openhuman-core/src/storage/layout/credentials.rs b/crates/openhuman-core/src/storage/layout/credentials.rs
index 48d4343f40..32f7fc2316 100644
--- a/crates/openhuman-core/src/storage/layout/credentials.rs
+++ b/crates/openhuman-core/src/storage/layout/credentials.rs
@@ -39,14 +39,30 @@ pub const FILES: [(&str, &str); 2] = [
/// A credential file the import would take: a regular file beside
/// `config.toml` holding the document its store persists. `None` otherwise
/// (missing, a symlink, or not that document: left in place).
-fn eligible(profile_dir: &Path, file: &str) -> Option<zeroize::Zeroizing<Vec<u8>>> {
+fn eligible(
+ profile_dir: &Path,
+ file: &str,
+) -> Result<Option<zeroize::Zeroizing<Vec<u8>>>, LayoutError> {
let path = profile_dir.join(file);
- let meta = std::fs::symlink_metadata(&path).ok()?;
+ let meta = match std::fs::symlink_metadata(&path) {
+ Ok(meta) => meta,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
+ Err(e) => return Err(e.into()),
+ };
if !meta.is_file() {
tracing::warn!(target: "openhuman::storage", file, "[layout] credential entry is not a regular file; left in place");
- return None;
+ return Ok(None);
}
- let bytes = zeroize::Zeroizing::new(std::fs::read(&path).ok()?);
+ // A read that fails for any reason but disappearance aborts, so the
+ // migration stays resumable instead of publishing v2 without the file.
+ let bytes = match std::fs::read(&path) {
+ Ok(bytes) => zeroize::Zeroizing::new(bytes),
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
+ Err(e) => {
+ tracing::warn!(target: "openhuman::storage", file, error = %e, "[layout] credential file unreadable");
+ return Err(e.into());
+ }
+ };
let valid = if file == PROFILES_FILENAME {
profiles::is_persisted_document(&bytes)
} else {
@@ -54,23 +70,25 @@ fn eligible(profile_dir: &Path, file: &str) -> Option<zeroize::Zeroizing<Vec<u8>
};
if !valid {
tracing::warn!(target: "openhuman::storage", file, "[layout] credential file is not a valid credential document; left in place");
- return None;
+ return Ok(None);
}
- Some(bytes)
+ Ok(Some(bytes))
}
/// The credential files a migration of `profile_dir` would import: the
/// eligible ones, and none when no master key is resolvable. Read-only; the
/// dry run uses it so its plan matches the run.
-pub fn candidates(profile_dir: &Path) -> Vec<String> {
+pub fn candidates(profile_dir: &Path) -> Result<Vec<String>, LayoutError> {
if !crate::security::secret_storage::encrypted_file_backend::storage_master_key_resolvable() {
- return Vec::new();
+ return Ok(Vec::new());
+ }
+ let mut found = Vec::new();
+ for (file, _) in FILES {
+ if eligible(profile_dir, file)?.is_some() {
+ found.push(file.to_owned());
+ }
}
- FILES
- .iter()
- .filter(|(file, _)| eligible(profile_dir, file).is_some())
- .map(|(file, _)| (*file).to_owned())
- .collect()
+ Ok(found)
}
/// Imports the credential files of `target` into the v1 database's `local`
@@ -87,7 +105,7 @@ pub fn candidates(profile_dir: &Path) -> Vec<String> {
pub async fn import(target: &Target) -> Result<Vec<String>, LayoutError> {
let mut files = Vec::new();
for (file, secret) in FILES {
- if let Some(bytes) = eligible(&target.profile_dir, file) {
+ if let Some(bytes) = eligible(&target.profile_dir, file)? {
files.push((file, secret, bytes));
}
}
diff --git a/crates/openhuman-core/src/storage/layout/credentials_tests.rs b/crates/openhuman-core/src/storage/layout/credentials_tests.rs
index 09b6973987..07fa12a0aa 100644
--- a/crates/openhuman-core/src/storage/layout/credentials_tests.rs
+++ b/crates/openhuman-core/src/storage/layout/credentials_tests.rs
@@ -207,3 +207,22 @@ async fn a_resumed_dry_run_lists_what_the_finished_import_recorded() {
assert!(listed.contains(&"@profile/auth-profiles.json"));
assert!(!listed.contains(&"@profile/http-credentials.json"));
}
+
+#[cfg(unix)]
+#[tokio::test(flavor = "current_thread")]
+async fn an_unreadable_credential_file_aborts_the_migration_before_v2_is_published() {
+ use std::os::unix::fs::PermissionsExt;
+ install_master_key_source(Arc::new(FixedKey));
+ let fx = with_credentials();
+ let path = fx.target.profile_dir.join(PROFILES_FILENAME);
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();
+ if std::fs::read(&path).is_ok() {
+ clear_master_key_source();
+ return; // running as root: permissions do not apply
+ }
+ let result = run(&fx).await;
+ clear_master_key_source();
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
+ assert!(result.is_err());
+ assert!(!fx.target.profile_dir.join(".layout").exists());
+}
diff --git a/crates/openhuman-core/src/storage/layout/run.rs b/crates/openhuman-core/src/storage/layout/run.rs
index ffcb8d7e56..25dcd93224 100644
--- a/crates/openhuman-core/src/storage/layout/run.rs
+++ b/crates/openhuman-core/src/storage/layout/run.rs
@@ -201,7 +201,7 @@ fn dry_run(target: &Target, resuming: bool) -> Result<DryRun, LayoutError> {
// Once Import finished, a resume uses the list it recorded.
let candidates = match state.as_ref() {
Some(s) if s.has(Step::Import) => s.credentials.clone(),
- _ => super::credentials::candidates(&target.profile_dir),
+ _ => super::credentials::candidates(&target.profile_dir)?,
};
plan.entries
.extend(plan::scan_credentials(&target.profile_dir, &candidates));