hermes-agent/tests/agent/test_private_credential_writers.py
ethernet 59624b7ace tests: replace every bare host skipif with platforms()
The OS lanes are marker-driven: list_os_marked_tests.py picks the files
a lane imports from their platforms() specs and the lane selects with
-m platforms. A test gated with skipif(sys.platform != "win32") is
therefore never imported on the Windows lane and skipped everywhere else
— it runs on no host. skipif(sys.platform == "win32") tests were merely
invisible to the lane bookkeeping, but the rule the tree now follows is
one host marker, never a bare skipif.

Mechanical mapping, semantics preserved: skip-on-Windows → "posix",
skip-off-Windows → "windows", skip-off-Linux → "linux", skip-on-macOS →
"not macos". The former skip reasons stay as trailing comments. A
non-host condition (os.geteuid() == 0) stays a separate skipif beside
the marker, spelled getattr(os, "geteuid", ...) so the decorator still
imports on Windows.

Where the conversion would stack two platforms() marks on one test (the
conftest rejects that at collection) the narrower mark wins:
- test_update_wedged_gateway: the class is already platforms("linux");
  its per-test "needs UNIX sockets" marks were redundant and are gone.
- test_process_registry.TestSystemdCgroupIsolation: the class-level
  skip-on-Windows moves onto the 11 methods that had no host mark; the
  11 platforms("linux") methods keep theirs.
- test_file_ops_single_roundtrip: the two fifo tests drop their
  platforms("linux") in favour of the module's "posix" (mkfifo exists on
  macOS; both tests already skip when it does not).
- test_linux_desktop_entry / test_gateway_job_teardown_live: duplicate
  or wider marks removed.
2026-09-21 19:18:15 -04:00

120 lines
5.7 KiB
Python

"""Cross-writer invariant: every private-credential file is 0600 from the moment its temp file exists.
The credential writers (auth.json, MCP OAuth tokens, secret-source cache, iron-proxy state, the
exchanged-JWT store, the Photon sidecar record, pairing data, the vault blob, the third-party
credential file, the spawn ledger, the meet-node token) all funnel through ``utils.atomic_json_write`` / ``atomic_write_text`` /
``atomic_write_bytes`` with ``mode=0o600``. The contract under test: the *temp* file is created
with mode 0600 (``O_EXCL``) BEFORE any byte lands and the final file carries 0600 — never
"open at umask, then chmod" (the #19673 window). POSIX-only: mode bits are not enforced on Windows.
"""
from __future__ import annotations
import json
import os
import stat
import sys
from pathlib import Path
import pytest
pytestmark = pytest.mark.platforms("posix") # POSIX mode bits not enforced on Windows
_PRIVATE = stat.S_IRUSR | stat.S_IWUSR
@pytest.fixture
def opens_spy(monkeypatch):
"""Record every ``os.open`` create (path, flags, mode) issued through ``utils``."""
import utils
observed: list[tuple[str, int, int]] = []
real_open = os.open
def spying(path, flags, mode=0o777, *args, **kwargs):
if flags & os.O_CREAT:
observed.append((os.fspath(path), flags, mode))
return real_open(path, flags, mode, *args, **kwargs)
# mkstemp resolves ``os.open`` at call time from the ``tempfile`` module namespace.
monkeypatch.setattr(utils.tempfile._os, "open", spying)
return observed
def _writers(home: Path, monkeypatch):
"""``(label, callable, target_path)`` for every private-credential writer."""
from agent import anthropic_credentials
from agent.secret_sources._cache import CachedFetch, DiskCache
from agent.proxy_sources import iron_proxy
from agent.vault_store import VaultStore
from gateway import pairing
from hermes_cli import auth as auth_mod, copilot_auth
from hermes_cli import process_identity
from tools import mcp_oauth
from plugins.google_meet.node.server import NodeServer
from plugins.platforms.photon import adapter as photon_adapter
photon_record = home / "runtime" / "photon.json"
monkeypatch.setattr(photon_adapter, "_runtime_record_path", lambda: photon_record)
ledger = home / "spawn-ledger.json"
monkeypatch.setattr(process_identity, "_ledger_path", lambda: ledger)
cache = DiskCache("probe.json", key_serializer=str)
vault = VaultStore(home / "vault")
meet_node = NodeServer(token_path=home / "meetings" / "node_token.json")
return [
("auth.json", lambda: auth_mod._save_auth_store({"version": auth_mod.AUTH_STORE_VERSION, "providers": {}}),
auth_mod._auth_file_path()),
("third-party credentials", lambda: anthropic_credentials._atomic_write_private_json(
home / "cc" / ".credentials.json", {"tok": 1}), home / "cc" / ".credentials.json"),
("mcp oauth tokens", lambda: mcp_oauth._write_json(home / "mcp" / "probe.tokens.json", {"access_token": "x"}),
home / "mcp" / "probe.tokens.json"),
("secret-source cache", lambda: cache.write("k", CachedFetch(secrets={"A": "b"}, fetched_at=1.0), 60, home),
cache.path(home)),
("iron-proxy mappings", lambda: iron_proxy.write_mappings([]), home / "proxy" / "mappings.json"),
("exchanged JWT store", lambda: copilot_auth._save_jwt_to_disk("fp", "jwt", 9e12, None),
copilot_auth._jwt_disk_path()),
("photon sidecar record", lambda: photon_adapter._write_runtime_record(1, "tok", 2), photon_record),
("pairing", lambda: pairing._save_json_file(home / "pairing" / "p.json", {"a": 1}), home / "pairing" / "p.json"),
("vault blob", lambda: vault._write_all([]), vault._vault_path),
("spawn ledger", lambda: process_identity.register_self("probe"), ledger),
("meet node token", meet_node.ensure_token, meet_node.token_path),
]
def test_every_credential_writer_creates_its_temp_file_at_0600(tmp_path, monkeypatch, opens_spy):
pytest.importorskip("cryptography")
home = tmp_path / "home"
home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
old_umask = os.umask(0o022) # a "write then chmod" regression would surface as 0o644
try:
for label, write, target in _writers(home, monkeypatch):
del opens_spy[:]
write()
assert target.exists(), f"{label}: nothing written at {target}"
assert stat.S_IMODE(target.stat().st_mode) == _PRIVATE, f"{label}: final file not 0600"
creates = [(p, m) for p, fl, m in opens_spy if Path(p).parent == target.parent]
assert creates, f"{label}: no temp file created in {target.parent}; opens={opens_spy!r}"
for path, mode in creates:
assert mode == _PRIVATE, f"{label}: temp file {path} created 0o{mode:o}, not 0600"
finally:
os.umask(old_umask)
def test_canonical_private_writers_round_trip_json_text_and_bytes(tmp_path):
from utils import atomic_json_write, atomic_write_bytes, atomic_write_text
target = tmp_path / "nested" / "creds.json"
payload = {"token": "sk-\u00e9\u2603", "n": [1, 2]}
atomic_json_write(target, payload, mode=0o600, fsync_dir=True)
assert json.loads(target.read_text(encoding="utf-8")) == payload
assert stat.S_IMODE(target.stat().st_mode) == 0o600
atomic_write_text(target, "plain\n", mode=0o600)
assert target.read_text(encoding="utf-8") == "plain\n"
blob = bytes(range(256)) * 3
atomic_write_bytes(target, blob, mode=0o600, fsync_dir=True)
assert target.read_bytes() == blob
assert stat.S_IMODE(target.stat().st_mode) == 0o600
assert [p.name for p in target.parent.iterdir()] == ["creds.json"], "temp files must not survive"