Skip to content

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

fix(core): tighten core.token to 0600 when rewriting it

M3gA-Mind23ff113b8322
diff --git a/crates/openhuman-core/src/core/auth.rs b/crates/openhuman-core/src/core/auth.rs
index e9e953d2c4..0f5a4d9066 100644
--- a/crates/openhuman-core/src/core/auth.rs
+++ b/crates/openhuman-core/src/core/auth.rs
@@ -234,6 +234,9 @@ fn write_token_file(path: &Path, token: &str) -> anyhow::Result<()> {
.truncate(true)
.mode(0o600)
.open(path)?;
+ // `mode` applies only when the file is created; an existing file keeps
+ // its old mode, so tighten it before the new token is written.
+ file.set_permissions(std::os::unix::fs::PermissionsExt::from_mode(0o600))?;
file.write_all(token.as_bytes())?;
}
diff --git a/crates/openhuman-core/src/core/auth_tests.rs b/crates/openhuman-core/src/core/auth_tests.rs
index 1ae672ed80..b714e66d69 100644
--- a/crates/openhuman-core/src/core/auth_tests.rs
+++ b/crates/openhuman-core/src/core/auth_tests.rs
@@ -105,3 +105,25 @@ fn token_file_has_owner_only_permissions() {
assert_eq!(mode & 0o777, 0o600, "token file must be 0o600");
std::fs::remove_dir_all(&tmp).ok();
}
+
+#[cfg(unix)]
+#[test]
+fn token_file_tightens_preexisting_loose_permissions() {
+ use std::os::unix::fs::PermissionsExt as _;
+
+ // A token file left world-readable (an older build, a copy, a restore)
+ // must come back owner-only when the token is rewritten.
+ let tmp = tempfile::tempdir().unwrap();
+ let path = tmp.path().join("core.token");
+ std::fs::write(&path, "old").unwrap();
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
+ write_token_file(&path, "new").unwrap();
+ let mode = std::fs::metadata(&path).unwrap().permissions().mode();
+ assert_eq!(
+ mode & 0o777,
+ 0o600,
+ "rewritten token kept mode {:o}",
+ mode & 0o777
+ );
+ assert_eq!(std::fs::read_to_string(&path).unwrap(), "new");
+}