Skip to content

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

fix(core): publish core.token through a fresh 0600 inode

M3gA-Mind5865e20d633b
diff --git a/crates/openhuman-core/src/core/auth.rs b/crates/openhuman-core/src/core/auth.rs
index 0f5a4d9066..bbdabecfbd 100644
--- a/crates/openhuman-core/src/core/auth.rs
+++ b/crates/openhuman-core/src/core/auth.rs
@@ -35,8 +35,6 @@ use std::sync::OnceLock;
#[cfg(unix)]
use std::io::Write as _;
-#[cfg(unix)]
-use std::os::unix::fs::OpenOptionsExt as _;
// The axum RPC-auth middleware + the `/v1` external-inference bearer helpers
// are exclusive to the `http-server` feature (#5048). The always-compiled token
@@ -228,16 +226,17 @@ fn write_token_file(path: &Path, token: &str) -> anyhow::Result<()> {
#[cfg(unix)]
{
- let mut file = std::fs::OpenOptions::new()
- .write(true)
- .create(true)
- .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))?;
+ // Publish through a fresh 0600 inode and rename it over the old file:
+ // chmod on an existing file neither revokes descriptors already open on
+ // it nor survives a failed rewrite, and the old token stays intact
+ // until the rename succeeds.
+ let dir = path
+ .parent()
+ .filter(|d| !d.as_os_str().is_empty())
+ .unwrap_or_else(|| Path::new("."));
+ let mut file = tempfile::NamedTempFile::new_in(dir)?;
file.write_all(token.as_bytes())?;
+ file.persist(path)?;
}
#[cfg(not(unix))]
diff --git a/crates/openhuman-core/src/core/auth_tests.rs b/crates/openhuman-core/src/core/auth_tests.rs
index b714e66d69..f3ab82ae33 100644
--- a/crates/openhuman-core/src/core/auth_tests.rs
+++ b/crates/openhuman-core/src/core/auth_tests.rs
@@ -127,3 +127,22 @@ fn token_file_tightens_preexisting_loose_permissions() {
);
assert_eq!(std::fs::read_to_string(&path).unwrap(), "new");
}
+
+#[cfg(unix)]
+#[test]
+fn a_descriptor_open_on_the_old_token_never_sees_the_new_one() {
+ use std::io::{Read as _, Seek as _};
+
+ // Someone who opened the old (loose) file keeps that descriptor after a
+ // chmod; the rewrite must land on a new inode so it reads only the old token.
+ let tmp = tempfile::tempdir().unwrap();
+ let path = tmp.path().join("core.token");
+ std::fs::write(&path, "old").unwrap();
+ let mut held = std::fs::File::open(&path).unwrap();
+ write_token_file(&path, "new").unwrap();
+ let mut seen = String::new();
+ held.rewind().unwrap();
+ held.read_to_string(&mut seen).unwrap();
+ assert_eq!(seen, "old");
+ assert_eq!(std::fs::read_to_string(&path).unwrap(), "new");
+}