|
| 1 | +"""Regression tests for TOCTOU-safe credential file writers in ``hermes_cli.auth``. |
| 2 | +
|
| 3 | +Background |
| 4 | +========== |
| 5 | +The three writers below used to create a temp file via ``Path.write_text`` / |
| 6 | +``Path.open('w')`` and only ``chmod``'d it to ``0o600`` afterward. Between |
| 7 | +create and chmod the file existed at the process umask (typically ``0o644``), |
| 8 | +briefly exposing OAuth tokens to other local users on multi-user hosts. The |
| 9 | +fix switches them to ``os.open(O_EXCL, mode=0o600)`` + ``os.fdopen`` + |
| 10 | +``fsync`` so the file is atomic at ``0o600`` on creation. Mirrors the fixes |
| 11 | +shipped for ``agent/google_oauth.py`` (#19673) and ``tools/mcp_oauth.py`` |
| 12 | +(#21148). |
| 13 | +
|
| 14 | +These tests stay green only while the token file and its parent directory |
| 15 | +end up at ``0o600`` / ``0o700`` after every write. POSIX-only — the mode-bit |
| 16 | +enforcement does not exist on Windows. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import json |
| 22 | +import os |
| 23 | +import stat |
| 24 | +import sys |
| 25 | +from unittest.mock import patch |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | + |
| 30 | +pytestmark = pytest.mark.skipif( |
| 31 | + sys.platform.startswith("win"), |
| 32 | + reason="POSIX mode bits not enforced on Windows", |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | +# _save_auth_store (~/.hermes/auth.json — every native OAuth provider) |
| 38 | +# --------------------------------------------------------------------------- |
| 39 | + |
| 40 | + |
| 41 | +def test_save_auth_store_writes_0o600_with_0o700_parent(tmp_path, monkeypatch): |
| 42 | + """``_save_auth_store`` must land ``auth.json`` at 0o600 and parent at 0o700.""" |
| 43 | + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) |
| 44 | + old_umask = os.umask(0o022) # make the race observable if it regresses |
| 45 | + try: |
| 46 | + from hermes_cli import auth as auth_mod |
| 47 | + |
| 48 | + auth_store = { |
| 49 | + "version": auth_mod.AUTH_STORE_VERSION, |
| 50 | + "providers": {"openai-codex": {"tokens": {"access_token": "secret-x"}}}, |
| 51 | + "active_provider": "openai-codex", |
| 52 | + } |
| 53 | + auth_path = auth_mod._save_auth_store(auth_store) |
| 54 | + finally: |
| 55 | + os.umask(old_umask) |
| 56 | + |
| 57 | + mode = stat.S_IMODE(auth_path.stat().st_mode) |
| 58 | + parent_mode = stat.S_IMODE(auth_path.parent.stat().st_mode) |
| 59 | + |
| 60 | + assert mode == 0o600, ( |
| 61 | + f"auth.json mode 0o{mode:o} != 0o600 — TOCTOU race regressed" |
| 62 | + ) |
| 63 | + assert parent_mode == 0o700, ( |
| 64 | + f"auth.json parent dir mode 0o{parent_mode:o} != 0o700 — siblings can traverse" |
| 65 | + ) |
| 66 | + |
| 67 | + # Content survived the rewrite |
| 68 | + data = json.loads(auth_path.read_text()) |
| 69 | + assert data["providers"]["openai-codex"]["tokens"]["access_token"] == "secret-x" |
| 70 | + |
| 71 | + |
| 72 | +# --------------------------------------------------------------------------- |
| 73 | +# _save_qwen_cli_tokens (Qwen CLI OAuth tokens) |
| 74 | +# --------------------------------------------------------------------------- |
| 75 | + |
| 76 | + |
| 77 | +def test_save_qwen_cli_tokens_writes_0o600_with_0o700_parent(tmp_path, monkeypatch): |
| 78 | + """``_save_qwen_cli_tokens`` must land the token file at 0o600 and parent at 0o700.""" |
| 79 | + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) |
| 80 | + # The Qwen CLI auth path lives under $HOME/.qwen by default — isolate it. |
| 81 | + monkeypatch.setenv("HOME", str(tmp_path)) |
| 82 | + old_umask = os.umask(0o022) |
| 83 | + try: |
| 84 | + from hermes_cli import auth as auth_mod |
| 85 | + |
| 86 | + tokens = { |
| 87 | + "access_token": "qwen-secret", |
| 88 | + "refresh_token": "qwen-refresh", |
| 89 | + "token_type": "Bearer", |
| 90 | + "expiry_date": 123, |
| 91 | + } |
| 92 | + auth_path = auth_mod._save_qwen_cli_tokens(tokens) |
| 93 | + finally: |
| 94 | + os.umask(old_umask) |
| 95 | + |
| 96 | + mode = stat.S_IMODE(auth_path.stat().st_mode) |
| 97 | + parent_mode = stat.S_IMODE(auth_path.parent.stat().st_mode) |
| 98 | + |
| 99 | + assert mode == 0o600, ( |
| 100 | + f"Qwen token file mode 0o{mode:o} != 0o600 — TOCTOU race regressed" |
| 101 | + ) |
| 102 | + assert parent_mode == 0o700, ( |
| 103 | + f"Qwen token parent dir mode 0o{parent_mode:o} != 0o700" |
| 104 | + ) |
| 105 | + |
| 106 | + data = json.loads(auth_path.read_text()) |
| 107 | + assert data["access_token"] == "qwen-secret" |
| 108 | + |
| 109 | + |
| 110 | +# --------------------------------------------------------------------------- |
| 111 | +# Nous shared-credential store write (inside _write_shared_nous_state) |
| 112 | +# --------------------------------------------------------------------------- |
| 113 | + |
| 114 | + |
| 115 | +def test_shared_nous_store_writes_0o600_with_0o700_parent(tmp_path, monkeypatch): |
| 116 | + """The Nous shared-credential store must land at 0o600 / parent 0o700.""" |
| 117 | + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) |
| 118 | + # _nous_shared_store_path() refuses to touch the real shared store during |
| 119 | + # pytest runs; redirect it into tmp_path explicitly. |
| 120 | + monkeypatch.setenv("HERMES_SHARED_AUTH_DIR", str(tmp_path / "shared")) |
| 121 | + old_umask = os.umask(0o022) |
| 122 | + try: |
| 123 | + from hermes_cli import auth as auth_mod |
| 124 | + |
| 125 | + state = { |
| 126 | + "access_token": "nous-access-xxx", |
| 127 | + "refresh_token": "nous-refresh-xxx", |
| 128 | + "token_type": "Bearer", |
| 129 | + "scope": "openid profile", |
| 130 | + "client_id": "test-client", |
| 131 | + "obtained_at": "2026-01-01T00:00:00Z", |
| 132 | + "expires_at": "2026-01-01T01:00:00Z", |
| 133 | + } |
| 134 | + auth_mod._write_shared_nous_state(state) |
| 135 | + path = auth_mod._nous_shared_store_path() |
| 136 | + finally: |
| 137 | + os.umask(old_umask) |
| 138 | + |
| 139 | + assert path.exists(), "shared Nous store was not written" |
| 140 | + mode = stat.S_IMODE(path.stat().st_mode) |
| 141 | + parent_mode = stat.S_IMODE(path.parent.stat().st_mode) |
| 142 | + |
| 143 | + assert mode == 0o600, ( |
| 144 | + f"Nous shared store mode 0o{mode:o} != 0o600 — TOCTOU race regressed" |
| 145 | + ) |
| 146 | + assert parent_mode == 0o700, ( |
| 147 | + f"Nous shared store parent dir mode 0o{parent_mode:o} != 0o700" |
| 148 | + ) |
| 149 | + |
| 150 | + data = json.loads(path.read_text()) |
| 151 | + assert data["refresh_token"] == "nous-refresh-xxx" |
| 152 | + |
| 153 | + |
| 154 | +# --------------------------------------------------------------------------- |
| 155 | +# Atomicity: verify ``os.open`` is called with an explicit 0o600 mode. |
| 156 | +# --------------------------------------------------------------------------- |
| 157 | + |
| 158 | + |
| 159 | +def test_save_auth_store_uses_os_open_with_0o600_mode(tmp_path, monkeypatch): |
| 160 | + """Regression: the writer must call ``os.open`` with an explicit restricted |
| 161 | + mode so the file is created at 0o600 atomically — closing the TOCTOU |
| 162 | + window the previous ``Path.open('w')`` left open (fd inherited process |
| 163 | + umask and was briefly 0o644 before post-write chmod).""" |
| 164 | + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) |
| 165 | + |
| 166 | + observed_opens: list[tuple[str, int, int]] = [] |
| 167 | + real_os_open = os.open |
| 168 | + |
| 169 | + def spying_os_open(path, flags, mode=0o777, *args, **kwargs): |
| 170 | + observed_opens.append((str(path), flags, mode)) |
| 171 | + return real_os_open(path, flags, mode, *args, **kwargs) |
| 172 | + |
| 173 | + with patch.object(os, "open", spying_os_open): |
| 174 | + from hermes_cli import auth as auth_mod |
| 175 | + |
| 176 | + auth_mod._save_auth_store( |
| 177 | + {"version": auth_mod.AUTH_STORE_VERSION, "providers": {}} |
| 178 | + ) |
| 179 | + |
| 180 | + auth_tmp_opens = [ |
| 181 | + (p, fl, m) for (p, fl, m) in observed_opens if "auth.json.tmp" in p |
| 182 | + ] |
| 183 | + assert auth_tmp_opens, ( |
| 184 | + f"os.open was never called for the auth.json temp file; " |
| 185 | + f"observed={observed_opens!r}" |
| 186 | + ) |
| 187 | + for path, flags, mode in auth_tmp_opens: |
| 188 | + assert flags & os.O_CREAT, f"auth.json temp open missing O_CREAT: path={path}" |
| 189 | + assert flags & os.O_EXCL, ( |
| 190 | + f"auth.json temp open missing O_EXCL — TOCTOU-safe pattern regressed: " |
| 191 | + f"path={path}, flags={flags}" |
| 192 | + ) |
| 193 | + # Must be exactly S_IRUSR | S_IWUSR (0o600) — no group/other bits. |
| 194 | + expected = stat.S_IRUSR | stat.S_IWUSR |
| 195 | + assert mode == expected, ( |
| 196 | + f"auth.json temp open mode 0o{mode:o} != 0o{expected:o} — " |
| 197 | + f"umask would apply and potentially expose tokens" |
| 198 | + ) |
0 commit comments