Skip to content

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

fix(credentials): create the auth profile store owner-only\n\n`fs::write` creates at `0o666 & ~umask` — 0644 under the usual 022 — and\n`fs::rename` carries the source mode onto the destination, so the credential\nstore inherited it on every save. Any process running under a different UID\ncould read it, and on the encrypted-JSON path — the normal state on Linux and\non any headless install without a keyring — that file is OAuth token\nciphertext, leaving `.secret_key` as the only remaining control.\n\nSame shape as the fix #2360 landed for the secret key in\n`keyring/encrypted_store.rs`: create with the mode already set rather than\nwidening then narrowing, so the file is never briefly world-readable. The\nexplicit `set_permissions` covers a leftover tmp from an interrupted save,\nwhich `.mode()` alone would not, since it only applies at creation.\n\nRebased onto main's split layout: `write_owner_only` sits in profiles.rs, the\ncall site moved to profiles_impl_01_part_02.rs, and the tests are a sibling\nfile — the layout gate rejects inline `#[cfg(test)] mod` blocks.\n\nThe added `a_saved_store_file_is_owner_only` goes through `upsert_profile`\nrather than calling the helper, because the three helper tests cannot see the\nwiring: swapping the call site back to `fs::write` leaves all three green. It\ncompiles on every platform so the store API usage is type-checked everywhere;\nonly the mode assertion is unix-gated.\n

Nguyen Thanh Dat486eb631285d
diff --git a/src/openhuman/security/credentials/profiles.rs b/src/openhuman/security/credentials/profiles.rs
index d01b1b9e97..a2c385e032 100644
--- a/src/openhuman/security/credentials/profiles.rs
+++ b/src/openhuman/security/credentials/profiles.rs
@@ -252,6 +252,43 @@ pub struct AuthProfilesStore {
#[cfg(test)]
force_lock_unwritable: Arc<AtomicBool>,
}
+/// Write `bytes` to `path` with the file readable only by its owner.
+///
+/// `fs::write` creates at `0o666 & ~umask` — `0644` under the usual `022` — and
+/// `fs::rename` carries the source mode onto the destination, so the credential
+/// store inherited it on every save. Any process running under a different UID
+/// could read it, and on the encrypted-JSON path — the normal state on Linux and
+/// on any headless install without a keyring — that file is OAuth token
+/// ciphertext, leaving `.secret_key` as the only remaining control.
+///
+/// Same shape as the fix #2360 landed for the secret key in
+/// `keyring/encrypted_store.rs`: create with the mode already set rather than
+/// widening then narrowing, so the file is never briefly world-readable.
+///
+/// `.mode()` only applies when the file is *created*, so a leftover tmp from an
+/// interrupted save would keep its old mode — hence the explicit
+/// `set_permissions` afterwards.
+fn write_owner_only(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::{OpenOptionsExt as _, PermissionsExt as _};
+
+ let mut file = OpenOptions::new()
+ .write(true)
+ .create(true)
+ .truncate(true)
+ .mode(0o600)
+ .open(path)?;
+ file.set_permissions(std::fs::Permissions::from_mode(0o600))?;
+ file.write_all(bytes)
+ }
+ #[cfg(not(unix))]
+ {
+ // Windows has no mode to set here; the file inherits the ACL of the
+ // per-user profile directory it is created in.
+ fs::write(path, bytes)
+ }
+}
include!("profiles_impl_01_part_01.rs");
include!("profiles_impl_01_part_02.rs");
include!("profiles_impl_01_part_03.rs");
diff --git a/src/openhuman/security/credentials/profiles_impl_01_part_02.rs b/src/openhuman/security/credentials/profiles_impl_01_part_02.rs
index 48dbf4d070..a050efd1f5 100644
--- a/src/openhuman/security/credentials/profiles_impl_01_part_02.rs
+++ b/src/openhuman/security/credentials/profiles_impl_01_part_02.rs
@@ -178,7 +178,7 @@ impl AuthProfilesStore {
PERSIST_RETRY_BASE_MS,
|| {
self.consume_test_transient_failure_write()?;
- fs::write(&tmp_path, &json).context("write auth profile tmp")
+ write_owner_only(&tmp_path, &json).context("write auth profile tmp")
},
)
.with_context(|| {
diff --git a/src/openhuman/security/credentials/profiles_owner_only_tests.rs b/src/openhuman/security/credentials/profiles_owner_only_tests.rs
new file mode 100644
index 0000000000..49f756603a
--- /dev/null
+++ b/src/openhuman/security/credentials/profiles_owner_only_tests.rs
@@ -0,0 +1,99 @@
+// `super` is the `tests` module, whose own `use super::*` re-exports the
+// private items of `profiles` -- including `write_owner_only`.
+use super::*;
+
+#[cfg(unix)]
+fn mode_of(path: &std::path::Path) -> u32 {
+ use std::os::unix::fs::PermissionsExt as _;
+ std::fs::metadata(path)
+ .expect("metadata")
+ .permissions()
+ .mode()
+ & 0o777
+}
+
+/// The property this change is actually about, asserted on the store rather
+/// than on the helper: after a real save, the credential file on disk is not
+/// readable by anyone but its owner.
+///
+/// The helper tests below cannot see the wiring — they call `write_owner_only`
+/// directly, so swapping the call site back to `fs::write` leaves them green.
+/// This one goes through `upsert_profile`, which writes the tmp and renames it
+/// over the store, so it covers the mode *and* the fact that `rename` carries
+/// it across.
+///
+/// Compiled on every platform so the store API usage is type-checked
+/// everywhere; only the assertion is unix-gated, since Windows has no mode to
+/// look at.
+#[test]
+fn a_saved_store_file_is_owner_only() {
+ let tmp = TempDir::new().unwrap();
+ let store = AuthProfilesStore::new(tmp.path(), false);
+
+ let profile = AuthProfile::new_oauth(
+ "openai-codex",
+ "default",
+ TokenSet {
+ access_token: "access-123".into(),
+ refresh_token: Some("refresh-123".into()),
+ id_token: None,
+ expires_at: Some(Utc::now() + chrono::Duration::hours(1)),
+ token_type: Some("Bearer".into()),
+ scope: None,
+ },
+ );
+ store.upsert_profile(profile, true).unwrap();
+
+ let path = store.path();
+ assert!(path.exists(), "the save must have produced a store file");
+
+ #[cfg(unix)]
+ assert_eq!(
+ mode_of(&path),
+ 0o600,
+ "the credential store must not be group/world readable"
+ );
+}
+
+#[cfg(unix)]
+#[test]
+fn a_new_credential_file_is_owner_only() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let path = dir.path().join("auth-profiles.json.tmp");
+ write_owner_only(&path, b"{}").expect("write");
+ assert_eq!(
+ mode_of(&path),
+ 0o600,
+ "credential store must not be group/world readable"
+ );
+}
+
+#[cfg(unix)]
+#[test]
+fn a_leftover_world_readable_tmp_is_repaired() {
+ // `.mode()` only applies at creation, so an interrupted save that left a
+ // 0644 tmp behind would otherwise keep it and carry it through `rename`.
+ use std::os::unix::fs::PermissionsExt as _;
+
+ let dir = tempfile::tempdir().expect("tempdir");
+ let path = dir.path().join("auth-profiles.json.tmp");
+ std::fs::write(&path, b"stale").expect("seed");
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).expect("chmod");
+ assert_eq!(mode_of(&path), 0o644, "precondition");
+
+ write_owner_only(&path, b"{}").expect("write");
+ assert_eq!(mode_of(&path), 0o600);
+}
+
+#[test]
+fn the_contents_are_written_and_truncated() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let path = dir.path().join("auth-profiles.json.tmp");
+ write_owner_only(&path, b"aaaaaaaaaaaaaaaaaaaa").expect("write long");
+ write_owner_only(&path, b"bb").expect("write short");
+ assert_eq!(
+ std::fs::read(&path).expect("read"),
+ b"bb",
+ "stale bytes must not survive"
+ );
+}
diff --git a/src/openhuman/security/credentials/profiles_tests.rs b/src/openhuman/security/credentials/profiles_tests.rs
index 7e13eef9b0..e8795b5691 100644
--- a/src/openhuman/security/credentials/profiles_tests.rs
+++ b/src/openhuman/security/credentials/profiles_tests.rs
@@ -12,3 +12,6 @@ const SYNTHETIC_DEAD_PID: u32 = i32::MAX as u32;
mod part_01_tests;
#[path = "profiles_tests_part_02_tests.rs"]
mod part_02_tests;
+
+#[path = "profiles_owner_only_tests.rs"]
+mod owner_only_tests;