The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
security(photon): create auth.json temp file with 0o600 atomically
solyanviktor-star2b30744c34ae
diff --git a/plugins/platforms/photon/auth.py b/plugins/platforms/photon/auth.pyindex 227b90d79dd..8fbb87f57f7 100644--- a/plugins/platforms/photon/auth.py+++ b/plugins/platforms/photon/auth.py@@ -40,7 +40,9 @@ import jsonimport loggingimport osimport re+import statimport time+import uuidfrom base64 import b64encodefrom dataclasses import dataclassfrom pathlib import Path@@ -109,14 +111,31 @@ def _load_auth() -> Dict[str, Any]:def _save_auth(data: Dict[str, Any]) -> None:path = _auth_json_path()path.parent.mkdir(parents=True, exist_ok=True)- tmp = path.with_suffix(".json.tmp")- with tmp.open("w", encoding="utf-8") as fh:- json.dump(data, fh, indent=2, sort_keys=True)+ # Per-process random temp suffix avoids collisions between concurrent+ # writers and stale leftovers from a crashed prior write.+ tmp = path.with_name(f"{path.name}.tmp.{os.getpid()}.{uuid.uuid4().hex}")+ # Create with 0o600 atomically via os.open(O_EXCL) + fdopen: the old+ # open() → write → chmod() sequence left a window where the bearer+ # token sat world-readable at process umask (typically 0o644), and the+ # predictable temp name could be pre-planted (symlink attack). Mirrors+ # hermes_cli/auth.py:_save_auth_store (#19673, #21148).+ fd = os.open(+ str(tmp),+ os.O_WRONLY | os.O_CREAT | os.O_EXCL,+ stat.S_IRUSR | stat.S_IWUSR,+ )try:- os.chmod(tmp, 0o600)- except OSError:- pass- tmp.replace(path)+ with os.fdopen(fd, "w", encoding="utf-8") as fh:+ json.dump(data, fh, indent=2, sort_keys=True)+ fh.flush()+ os.fsync(fh.fileno())+ tmp.replace(path)+ except BaseException:+ try:+ tmp.unlink()+ except OSError:+ pass+ raisedef load_photon_token() -> Optional[str]:diff --git a/tests/plugins/platforms/photon/test_auth.py b/tests/plugins/platforms/photon/test_auth.pyindex 3e6208de251..828d02b0221 100644--- a/tests/plugins/platforms/photon/test_auth.py+++ b/tests/plugins/platforms/photon/test_auth.py@@ -71,6 +71,20 @@ def test_store_and_load_photon_token(tmp_hermes_home: Path) -> None:assert auth_json["credential_pool"]["photon"][0]["access_token"] == "abc123def456"[email protected](os.name != "posix", reason="POSIX mode bits only")+def test_save_auth_never_world_readable(tmp_hermes_home: Path) -> None:+ """auth.json must be created 0o600 — no window at process umask."""+ photon_auth.store_photon_token("secret-token")+ mode = (tmp_hermes_home / "auth.json").stat().st_mode & 0o777+ assert mode == 0o600+++def test_save_auth_leaves_no_temp_files(tmp_hermes_home: Path) -> None:+ photon_auth.store_photon_token("secret-token")+ leftovers = [p.name for p in tmp_hermes_home.iterdir() if p.name != "auth.json"]+ assert leftovers == []++def test_store_project_credentials_round_trip(tmp_hermes_home: Path, monkeypatch: pytest.MonkeyPatch,) -> None: