Skip to content

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

fix(secrets): build the key file reader without the secret-file feature

M3gA-Mind18da39e15f03
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 d33a01afc8..2a082c636e 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
@@ -20,7 +20,7 @@ use zeroize::Zeroizing;
#[path = "encrypted_file_backend/key_source.rs"]
pub mod key_source;
-use key_source::{env_value, master_key_from_env};
+use key_source::{env_value, master_key_from_env, read_master_key_file};
/// Environment variable carrying the master key inline as `2 * KEY_LEN` hex
/// characters (`openssl rand -hex 32`). Lets a headless `openhuman-core
@@ -254,34 +254,6 @@ fn try_load_master_key() -> Result<([u8; KEY_LEN], String), MasterKeyError> {
.map_err(|e| MasterKeyError::Configured(e.to_string()))
}
-/// Reads the file named by [`MASTER_KEY_FILE_ENV`].
-///
-/// On Unix a key file must not be writable by other users. Read-only group or
-/// other permissions are supported for container secret mounts, where the
-/// runtime may add group-read access for a non-root core.
-fn read_master_key_file(path: &Path) -> Result<String, String> {
- #[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- let metadata = std::fs::metadata(path).map_err(|e| {
- format!("cannot inspect master key file permissions ({MASTER_KEY_FILE_ENV}): {e}")
- })?;
- let mode = metadata.permissions().mode() & 0o777;
- if key_file_mode_is_other_writable(mode) {
- return Err(format!(
- "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; \
- restrict it to a read-only secret mount"
- ));
- }
- }
- std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
-}
-
-#[cfg(unix)]
-fn key_file_mode_is_other_writable(mode: u32) -> bool {
- mode & 0o002 != 0
-}
-
/// Decodes a master key supplied as exactly `2 * KEY_LEN` hex characters.
/// The value never appears in the error.
fn parse_master_key_hex(hex: &str) -> Result<[u8; KEY_LEN], String> {
diff --git a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs
index 4664c84ce4..594acff087 100644
--- a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs
+++ b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs
@@ -95,6 +95,34 @@ pub(super) fn load_or_mint_master_key(
Ok(key)
}
+/// Reads the file named by [`MASTER_KEY_FILE_ENV`].
+///
+/// On Unix a key file must not be writable by other users. Read-only group or
+/// other permissions are supported for container secret mounts, where the
+/// runtime may add group-read access for a non-root core.
+pub(super) fn read_master_key_file(path: &Path) -> Result<String, String> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ let metadata = std::fs::metadata(path).map_err(|e| {
+ format!("cannot inspect master key file permissions ({MASTER_KEY_FILE_ENV}): {e}")
+ })?;
+ let mode = metadata.permissions().mode() & 0o777;
+ if key_file_mode_is_other_writable(mode) {
+ return Err(format!(
+ "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; \
+ restrict it to a read-only secret mount"
+ ));
+ }
+ }
+ std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
+}
+
+#[cfg(unix)]
+pub(super) fn key_file_mode_is_other_writable(mode: u32) -> bool {
+ mode & 0o002 != 0
+}
+
/// Load or durably create the workspace key under the cross-process write
/// lock. Never replace a malformed or unreadable existing key. The atomic
/// file writer creates the key with Unix mode `0600` before publication.
@@ -151,12 +179,9 @@ pub(super) fn profile_master_key_read_only(
.map_err(SecretStorageError::Backend)?;
let file = env_value(MASTER_KEY_FILE_ENV, std::env::var(MASTER_KEY_FILE_ENV))
.map_err(SecretStorageError::Backend)?;
- if let Some((key, _)) = master_key_from_env(
- inline.as_deref(),
- file.as_deref(),
- super::read_master_key_file,
- )
- .map_err(SecretStorageError::Backend)?
+ if let Some((key, _)) =
+ master_key_from_env(inline.as_deref(), file.as_deref(), read_master_key_file)
+ .map_err(SecretStorageError::Backend)?
{
return Ok(Some(Zeroizing::new(key)));
}
diff --git a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend_tests.rs b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend_tests.rs
index b89b58d765..aaef803bcc 100644
--- a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend_tests.rs
+++ b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend_tests.rs
@@ -419,10 +419,16 @@ fn read_master_key_file_rejects_other_writable_files() {
#[test]
fn key_file_permissions_reject_other_writes_but_allow_read_only_access() {
for mode in [0o400, 0o600, 0o644, 0o444, 0o604, 0o440, 0o640] {
- assert!(!key_file_mode_is_other_writable(mode), "{mode:04o}");
+ assert!(
+ !key_source::key_file_mode_is_other_writable(mode),
+ "{mode:04o}"
+ );
}
for mode in [0o602, 0o666] {
- assert!(key_file_mode_is_other_writable(mode), "{mode:04o}");
+ assert!(
+ key_source::key_file_mode_is_other_writable(mode),
+ "{mode:04o}"
+ );
}
}