2be8e6147a
Ten hand-rolled "write a token file safely" routines each carried a
different subset of {0600-on-create, fsync, atomic_replace, parent-0700
guard, BaseException cleanup}. Two of them (iron_proxy state files,
the exchanged-JWT store) still opened the temp file at process umask
and chmod'ed afterwards - the exact TOCTOU window the others document
as fixed. None of the bare-os.replace copies got atomic_replace's
Windows-contention retry or EXDEV fallback.
utils gains fsync_dir= (absorbs auth.py's dir fsync), atomic_write_bytes
(vault blob) and mode= on atomic_write_text; the ten sites become 1-3
line callers. mkstemp creates the temp file O_EXCL at 0600 regardless of
umask, so the payload is never umask-readable.
Behavior change: iron_proxy proxy.yaml/mappings.json and the exchanged-JWT
store are now 0600 from creation and fsync'd; every credential write goes
through atomic_replace (symlink-preserving, Windows retry, EXDEV copy).
auth_nous shared store now uses atomic_replace too (it forced os.replace
with no recorded reason). secret_sources cache parent-0700 goes through
the guarded secure_parent_dir instead of an unguarded chmod.
154 lines
5.7 KiB
Python
154 lines
5.7 KiB
Python
"""Regression tests for TOCTOU-safe credential file writers in ``hermes_cli.auth``.
|
|
|
|
Background
|
|
==========
|
|
The three writers below used to create a temp file via ``Path.write_text`` /
|
|
``Path.open('w')`` and only ``chmod``'d it to ``0o600`` afterward. Between
|
|
create and chmod the file existed at the process umask (typically ``0o644``),
|
|
briefly exposing OAuth tokens to other local users on multi-user hosts. The
|
|
writers now go through ``utils.atomic_json_write(mode=0o600)`` whose mkstemp temp
|
|
file is ``O_EXCL`` at 0600 on creation (the cross-writer invariant lives in
|
|
``tests/test_private_credential_writers.py``).
|
|
|
|
These tests stay green only while the token file and its parent directory
|
|
end up at ``0o600`` / ``0o700`` after every write. POSIX-only — the mode-bit
|
|
enforcement does not exist on Windows.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import stat
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
sys.platform.startswith("win"),
|
|
reason="POSIX mode bits not enforced on Windows",
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _save_auth_store (~/.hermes/auth.json — every native OAuth provider)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_save_auth_store_writes_0o600_with_0o700_parent(tmp_path, monkeypatch):
|
|
"""``_save_auth_store`` must land ``auth.json`` at 0o600 and parent at 0o700."""
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
old_umask = os.umask(0o022) # make the race observable if it regresses
|
|
try:
|
|
from hermes_cli import auth as auth_mod
|
|
|
|
auth_store = {
|
|
"version": auth_mod.AUTH_STORE_VERSION,
|
|
"providers": {"openai-codex": {"tokens": {"access_token": "secret-x"}}},
|
|
"active_provider": "openai-codex",
|
|
}
|
|
auth_path = auth_mod._save_auth_store(auth_store)
|
|
finally:
|
|
os.umask(old_umask)
|
|
|
|
mode = stat.S_IMODE(auth_path.stat().st_mode)
|
|
parent_mode = stat.S_IMODE(auth_path.parent.stat().st_mode)
|
|
|
|
assert mode == 0o600, (
|
|
f"auth.json mode 0o{mode:o} != 0o600 — TOCTOU race regressed"
|
|
)
|
|
assert parent_mode == 0o700, (
|
|
f"auth.json parent dir mode 0o{parent_mode:o} != 0o700 — siblings can traverse"
|
|
)
|
|
|
|
# Content survived the rewrite
|
|
data = json.loads(auth_path.read_text())
|
|
assert data["providers"]["openai-codex"]["tokens"]["access_token"] == "secret-x"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _save_qwen_cli_tokens (Qwen CLI OAuth tokens)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_save_qwen_cli_tokens_writes_0o600_with_0o700_parent(tmp_path, monkeypatch):
|
|
"""``_save_qwen_cli_tokens`` must land the token file at 0o600 and parent at 0o700."""
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
# The Qwen CLI auth path lives under $HOME/.qwen by default — isolate it.
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
old_umask = os.umask(0o022)
|
|
try:
|
|
from hermes_cli import auth as auth_mod
|
|
|
|
tokens = {
|
|
"access_token": "qwen-secret",
|
|
"refresh_token": "qwen-refresh",
|
|
"token_type": "Bearer",
|
|
"expiry_date": 123,
|
|
}
|
|
auth_path = auth_mod._save_qwen_cli_tokens(tokens)
|
|
finally:
|
|
os.umask(old_umask)
|
|
|
|
mode = stat.S_IMODE(auth_path.stat().st_mode)
|
|
parent_mode = stat.S_IMODE(auth_path.parent.stat().st_mode)
|
|
|
|
assert mode == 0o600, (
|
|
f"Qwen token file mode 0o{mode:o} != 0o600 — TOCTOU race regressed"
|
|
)
|
|
assert parent_mode == 0o700, (
|
|
f"Qwen token parent dir mode 0o{parent_mode:o} != 0o700"
|
|
)
|
|
|
|
data = json.loads(auth_path.read_text())
|
|
assert data["access_token"] == "qwen-secret"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Nous shared-credential store write (inside _write_shared_nous_state)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_shared_nous_store_writes_0o600_with_0o700_parent(tmp_path, monkeypatch):
|
|
"""The Nous shared-credential store must land at 0o600 / parent 0o700."""
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
# _nous_shared_store_path() refuses to touch the real shared store during
|
|
# pytest runs; redirect it into tmp_path explicitly. Use a distinct
|
|
# subdirectory name (``shared_override``) so the guard's "real user
|
|
# home" reference — which currently tracks HERMES_HOME via
|
|
# get_default_hermes_root() — can't collide with our override and
|
|
# falsely claim we're writing to the real user's shared store.
|
|
monkeypatch.setenv("HERMES_SHARED_AUTH_DIR", str(tmp_path / "shared_override"))
|
|
old_umask = os.umask(0o022)
|
|
try:
|
|
from hermes_cli import auth as auth_mod
|
|
|
|
state = {
|
|
"access_token": "nous-access-xxx",
|
|
"refresh_token": "nous-refresh-xxx",
|
|
"token_type": "Bearer",
|
|
"scope": "openid profile",
|
|
"client_id": "test-client",
|
|
"obtained_at": "2026-01-01T00:00:00Z",
|
|
"expires_at": "2026-01-01T01:00:00Z",
|
|
}
|
|
auth_mod._write_shared_nous_state(state)
|
|
path = auth_mod._nous_shared_store_path()
|
|
finally:
|
|
os.umask(old_umask)
|
|
|
|
assert path.exists(), "shared Nous store was not written"
|
|
mode = stat.S_IMODE(path.stat().st_mode)
|
|
parent_mode = stat.S_IMODE(path.parent.stat().st_mode)
|
|
|
|
assert mode == 0o600, (
|
|
f"Nous shared store mode 0o{mode:o} != 0o600 — TOCTOU race regressed"
|
|
)
|
|
assert parent_mode == 0o700, (
|
|
f"Nous shared store parent dir mode 0o{parent_mode:o} != 0o700"
|
|
)
|
|
|
|
data = json.loads(path.read_text())
|
|
assert data["refresh_token"] == "nous-refresh-xxx"
|