Skip to content

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

fix(keyring): make the key-file permission warning actionable and pin the env tests' isolation

fede-kamel810bbbc96a4f
diff --git a/crates/openhuman-core/src/security/keyring/README.md b/crates/openhuman-core/src/security/keyring/README.md
index 628ce92858..ea5cffa180 100644
--- a/crates/openhuman-core/src/security/keyring/README.md
+++ b/crates/openhuman-core/src/security/keyring/README.md
@@ -36,7 +36,7 @@ Re-exported from `mod.rs`:
- `KeyringBackend`: backend trait (`get`/`set`/`delete`/`name`).
- `SecretStore`: config-field encrypt/decrypt; `encrypt`/`decrypt`/`decrypt_and_migrate`/`needs_migration`/`is_encrypted`/`new`.
- `KeyringError`: error enum with `diagnostic()`.
-- `init_master_key`: load the app master key at startup (staging/prod only) — from the environment (`OPENHUMAN_KEYRING_MASTER_KEY` inline, or `OPENHUMAN_KEYRING_MASTER_KEY_FILE` naming a file; 64 hex characters, exactly one of the two) when set, otherwise from the OS keychain. A set-but-malformed variable is a boot error, not a fall-through; the source is logged at `info`, the value never.
+- `init_master_key`: load the app master key at startup (staging/prod only) — from the environment (`OPENHUMAN_KEYRING_MASTER_KEY` inline, or `OPENHUMAN_KEYRING_MASTER_KEY_FILE` naming a file; 64 hex characters, exactly one of the two) when set, otherwise from the OS keychain. A set-but-malformed variable is a boot error, not a fall-through; the source is logged at `info`, the value never. A key file any user can read (Kubernetes' default `0644`, Docker's `0444`) is loaded with a `warn` naming the fix (`defaultMode: 0400`); group read alone (`0440`, from a pod `fsGroup`) is not warned about.
- `init_workspace`: register the workspace dir for file and encrypted-file backends.
- `get`, `set`, `delete`, `get_or_create_random`, `is_available`, `migrate_from_file`, `MigrationOutcome`.
- `force_backend_for_test`: `pub(crate)`, test-only.
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 d0607c954b..16b17cfd04 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -166,6 +166,9 @@ impl MasterKeyEntry for keyring::Entry {
/// Loads the master key, returning it with a human-readable description of
/// the source it came from (for the startup log; never the value).
///
+/// Uncached: every call reads the environment (and the key file) afresh.
+/// Only [`init_master_key`] stores an outcome in [`MASTER_KEY`].
+///
/// The environment is consulted first so a headless deployment never touches
/// the OS keychain. An environment variable that is set but unusable is an
/// error, not a fall-through: silently continuing to the keychain would mask
@@ -251,20 +254,30 @@ fn master_key_from_env(
}
}
-/// Reads the file named by [`MASTER_KEY_FILE_ENV`]. On Unix a file readable
-/// by group or others is accepted but logged at `warn`: Docker secrets mount
-/// `0444` and Kubernetes secret volumes default to `0644`, so refusing would
-/// break the very deployments this path exists for.
+/// Reads the file named by [`MASTER_KEY_FILE_ENV`].
+///
+/// On Unix a file any user on the host can access is accepted but logged at
+/// `warn`, with how to tighten it. It is not refused: the deployments this
+/// path exists for mount secrets read-only with modes the process cannot
+/// change — Docker secrets `0444`, Kubernetes secret volumes `0644` unless
+/// `defaultMode` is set — so refusing would break them out of the box.
+/// Group access alone is not warned about: a non-root pod reads a root-owned
+/// secret through its `fsGroup`, and kubelet then grants group read, so
+/// `defaultMode: 0400` lands as `0440` — the tightest mode that deployment
+/// can have. The config loader treats a foreign-owned `0644` config the same
+/// way (`config::schema::load`).
fn read_master_key_file(path: &Path) -> Result<String, String> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(metadata) = std::fs::metadata(path) {
let mode = metadata.permissions().mode() & 0o777;
- if mode & 0o077 != 0 {
+ if key_file_mode_is_world_accessible(mode) {
log::warn!(
"[keyring:encrypted_file] master key file ({MASTER_KEY_FILE_ENV}) has \
- mode {mode:04o}; 0600 is recommended"
+ mode {mode:04o}, so every user on the host can read it; restrict it to \
+ 0400 or 0600 (Kubernetes: `defaultMode: 0400` on the secret volume; \
+ Docker Swarm: `mode: 0400` on the secret)"
);
}
}
@@ -272,6 +285,13 @@ fn read_master_key_file(path: &Path) -> Result<String, String> {
std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
}
+/// Whether a master-key file's permission bits grant anything to "other"
+/// users. See [`read_master_key_file`] for why group access is not counted.
+#[cfg(unix)]
+fn key_file_mode_is_world_accessible(mode: u32) -> bool {
+ mode & 0o007 != 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/keyring/encrypted_file_backend_tests.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
index 3c1248f74d..2906433632 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
@@ -255,9 +255,9 @@ fn read_master_key_file_returns_contents_and_reports_a_missing_file() {
#[cfg(unix)]
#[test]
-fn read_master_key_file_accepts_a_group_readable_secret_mount() {
- // Docker secrets are mounted 0444 and Kubernetes secret volumes 0644:
- // permissive modes are warned about, never refused.
+fn read_master_key_file_accepts_a_world_readable_secret_mount() {
+ // Docker secrets are mounted 0444 and Kubernetes secret volumes 0644 by
+ // default, read-only: permissive modes are warned about, never refused.
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::TempDir::new().unwrap();
let path = tmp.path().join("master.key");
@@ -268,6 +268,20 @@ fn read_master_key_file_accepts_a_group_readable_secret_mount() {
assert_eq!(contents, hex_key(0x44));
}
+#[cfg(unix)]
+#[test]
+fn key_file_warning_covers_default_mounts_but_not_owner_or_fsgroup_modes() {
+ // Warned: the Kubernetes and Docker defaults, and anything world-accessible.
+ for mode in [0o644, 0o444, 0o604, 0o666, 0o602] {
+ assert!(key_file_mode_is_world_accessible(mode), "{mode:04o}");
+ }
+ // Not warned: owner-only, and `defaultMode: 0400` under a pod `fsGroup`,
+ // which kubelet turns into 0440.
+ for mode in [0o400, 0o600, 0o440, 0o640] {
+ assert!(!key_file_mode_is_world_accessible(mode), "{mode:04o}");
+ }
+}
+
#[test]
fn parse_master_key_hex_round_trips_an_encoded_key() {
let key_bytes = crypto::generate_random_bytes(KEY_LEN);
@@ -302,6 +316,14 @@ fn env_value_distinguishes_unset_from_invalid_unicode() {
}
// ── Wiring: the real env → `try_load_master_key` → key, no keychain ─────────
+//
+// `try_load_master_key` is uncached: it reads the environment on every call
+// and never touches the process-wide `MASTER_KEY` (only `init_master_key`
+// does, and no test here calls it; the one-time logic is tested through
+// `init_once` with a local `OnceLock` below). Each test mutates the env only
+// under `EnvVarGuard::locked()`, which serializes on the crate's shared
+// `TEST_ENV_LOCK` and restores the previous values before releasing it, so
+// the tests cannot observe each other's values in any order.
#[test]
fn try_load_master_key_prefers_the_inline_env_key_and_never_touches_the_keychain() {
@@ -369,8 +391,8 @@ fn a_secret_written_under_a_configured_key_reads_back_after_a_restart() {
// First "process": load the configured key and store a secret through the
// same lock → read → encrypt → write path `KeyringBackend::set` uses.
- // `MASTER_KEY` is a process-wide `OnceLock` that cannot be reset, so the
- // key is passed explicitly instead of going through it.
+ // `get`/`set` read the key from the process-wide `MASTER_KEY`, which
+ // this test must not populate, so the key is passed explicitly.
{
let (key, _) = try_load_master_key().expect("configured key loads");
let backend = EncryptedFileBackend::new(workspace.path());
diff --git a/gitbooks/features/cloud-deploy.md b/gitbooks/features/cloud-deploy.md
index 8d9f7026dc..60af504513 100644
--- a/gitbooks/features/cloud-deploy.md
+++ b/gitbooks/features/cloud-deploy.md
@@ -488,6 +488,19 @@ need before you start" (#6926). On a release that predates those variables, set
`$OPENHUMAN_WORKSPACE/dev-keychain.json` (plaintext, `0600`); put the workspace
volume on encrypted storage and treat the host as the secret boundary.
+Mount the key file owner-only. The usual secret mounts are readable by every
+user in the container: Kubernetes secret volumes default to `0644` and Docker
+secrets to `0444`. The core still loads such a file, because the mount is
+read-only and it cannot fix the mode, but it logs a warning at every start.
+Restrict it where the mount is declared:
+
+- Kubernetes: set `defaultMode: 0400` on the secret volume (or `mode: 0400` on
+ the item). A pod with `fsGroup` set gets `0440` instead, since kubelet adds
+ group read so the non-root user can open it. That mode does not warn.
+- Docker Swarm: set `mode: 0400` (with `uid`/`gid` for a non-root user) on the
+ service's secret.
+- A plain file: `chmod 600` it, owned by the user the core runs as.
+
---
## Named-volume ownership and the Docker entrypoint