diff --git a/agent/file_safety.py b/agent/file_safety.py index 13d0912b61..bda39ad27f 100644 --- a/agent/file_safety.py +++ b/agent/file_safety.py @@ -157,11 +157,13 @@ def build_write_denied_paths(home: str) -> set[str]: (".ssh", "authorized_keys"), (".ssh", "id_rsa"), (".ssh", "id_ed25519"), (".netrc",), (".pgpass",), (".npmrc",), (".pypirc",), (".git-credentials",), ) - # Both the active-profile and top-level copies: overwriting the root .env leaks - # credentials across every profile that inherits from it; the root Anthropic - # PKCE store is still read by default/non-profile sessions when a profile is - # active; bws_cache.enc.json is the Bitwarden Secrets Manager encrypted cache. - hermes_files = (".env", ".anthropic_oauth.json", os.path.join("cache", "bws_cache.enc.json")) + # Same HERMES_HOME credential files the read guard already blocks, on both + # the active profile and the global root. bws_cache.enc.json is write-only + # extra: the plaintext sibling is in ``_CREDENTIAL_FILE_NAMES``. + hermes_files = ( + *_CREDENTIAL_FILE_NAMES, + os.path.join("cache", "bws_cache.enc.json"), + ) paths = [ *(os.path.join(home, *f) for f in home_files), *(str(base / f) for f in hermes_files for base in (_hermes_home_path(), _hermes_root_path())), @@ -231,6 +233,12 @@ def _classify_write_denial(path: str) -> Optional[str]: with suppress(Exception): if _is_under(resolved, os.path.realpath(os.path.join(str(base), sub))): return "credential" + # vault/ and browser-profile/ are credential dirs on the read path; + # mcp-tokens/ is already in _HERMES_PROTECTED_SUBPATHS. + for sub, _, _ in _READ_DENIED_DIRS: + with suppress(Exception): + if _is_under(resolved, os.path.realpath(os.path.join(str(base), sub))): + return "credential" safe_roots = get_safe_write_roots() if safe_roots and not any(_is_under(resolved, root) for root in safe_roots): diff --git a/tests/agent/test_file_safety_write_credentials.py b/tests/agent/test_file_safety_write_credentials.py new file mode 100644 index 0000000000..b83e4ce4f3 --- /dev/null +++ b/tests/agent/test_file_safety_write_credentials.py @@ -0,0 +1,110 @@ +"""Write denylist must cover the same HERMES_HOME credential stores as reads. + +Reads already refuse auth.json, webhook HMAC secrets, google_oauth.json, +the Bitwarden plaintext cache, vault/, and browser-profile/. Writes only +blocked .env, the Anthropic PKCE store, and bws_cache.enc.json, so +write_file/patch could replace the rest. + +The invariant: every name in ``_CREDENTIAL_FILE_NAMES`` and every directory +in ``_READ_DENIED_DIRS`` is write-denied under both the active home and the +global root. Nested same-basename files (skill mocks) stay writable. +""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +import agent.file_safety as fs + + +@pytest.fixture() +def hermes_layout(tmp_path, monkeypatch): + """Profile HERMES_HOME plus a distinct global root, both patched.""" + root = tmp_path / "hermes_root" + profile = root / "profiles" / "coder" + profile.mkdir(parents=True) + monkeypatch.setattr(fs, "_hermes_home_path", lambda: profile) + monkeypatch.setattr(fs, "_hermes_root_path", lambda: root) + return root, profile + + +def _touch(base: Path, rel: str) -> Path: + p = base / rel + p.parent.mkdir(parents=True, exist_ok=True) + p.write_text("dummy", encoding="utf-8") + return p + + +def test_every_read_credential_file_is_write_denied(hermes_layout): + root, profile = hermes_layout + for name in fs._CREDENTIAL_FILE_NAMES: + for base in (profile, root): + path = _touch(base, name) + assert fs.is_write_denied(str(path)), f"write allowed: {path}" + + +def test_every_read_denied_dir_is_write_denied(hermes_layout): + root, profile = hermes_layout + for sub, _, _ in fs._READ_DENIED_DIRS: + for base in (profile, root): + path = _touch(base, f"{sub}/inside.bin") + assert fs.is_write_denied(str(path)), f"write allowed: {path}" + + +def test_encrypted_bitwarden_cache_stays_write_denied(hermes_layout): + root, profile = hermes_layout + for base in (profile, root): + path = _touch(base, "cache/bws_cache.enc.json") + assert fs.is_write_denied(str(path)) + + +def test_config_yaml_stays_writable(hermes_layout): + _, profile = hermes_layout + path = _touch(profile, "config.yaml") + assert fs.is_write_denied(str(path)) is False + + +def test_nested_auth_json_stays_writable(hermes_layout): + _, profile = hermes_layout + path = _touch(profile, "skills/my-skill/auth.json") + assert fs.is_write_denied(str(path)) is False + + +def test_auth_json_outside_hermes_home_stays_writable(hermes_layout, tmp_path): + project = tmp_path / "myproject" + path = _touch(project, "auth.json") + assert fs.is_write_denied(str(path)) is False + + +def test_write_file_does_not_replace_auth_json(hermes_layout): + _, profile = hermes_layout + from tools.environments.local import LocalEnvironment + from tools.file_operations import ShellFileOperations + + target = _touch(profile, "auth.json") + target.write_text("{\"ok\": true}\n", encoding="utf-8") + ops = ShellFileOperations( + LocalEnvironment(cwd=str(profile)), cwd=str(profile) + ) + res = ops.write_file(str(target), "{\"pwned\": true}\n") + assert res.error is not None + assert "protected system/credential file" in res.error + assert target.read_text(encoding="utf-8") == "{\"ok\": true}\n" + + +def test_write_file_does_not_replace_vault_key(hermes_layout): + _, profile = hermes_layout + from tools.environments.local import LocalEnvironment + from tools.file_operations import ShellFileOperations + + target = _touch(profile, "vault/vault.key") + target.write_text("not-a-real-key\n", encoding="utf-8") + ops = ShellFileOperations( + LocalEnvironment(cwd=str(profile)), cwd=str(profile) + ) + res = ops.write_file(str(target), "stolen\n") + assert res.error is not None + assert "protected system/credential file" in res.error + assert target.read_text(encoding="utf-8") == "not-a-real-key\n" diff --git a/tests/tools/test_write_deny.py b/tests/tools/test_write_deny.py index c71a509692..d31903048f 100644 --- a/tests/tools/test_write_deny.py +++ b/tests/tools/test_write_deny.py @@ -87,9 +87,8 @@ class TestWriteAllowed: assert _is_write_denied("/tmp/safe_file.txt") is False - def test_hermes_control_files_requested_writable(self): + def test_hermes_config_yaml_requested_writable(self): from hermes_constants import get_hermes_home home = get_hermes_home() - for name in ["auth.json", "config.yaml", "webhook_subscriptions.json"]: - assert _is_write_denied(str(home / name)) is False, f"{name} should be writable" + assert _is_write_denied(str(home / "config.yaml")) is False, "config.yaml should be writable" diff --git a/website/docs/user-guide/security.md b/website/docs/user-guide/security.md index d41e495a08..77b3ef1935 100644 --- a/website/docs/user-guide/security.md +++ b/website/docs/user-guide/security.md @@ -347,7 +347,7 @@ These categories are always denied, even when `HERMES_WRITE_SAFE_ROOT` is unset: | Category | Examples | |----------|----------| | OS credential stores | `~/.ssh/` (keys, `authorized_keys`), `~/.aws/`, `~/.kube/`, `/etc/sudoers`, `~/.netrc` | -| Hermes credential stores | `auth.json`, `.env`, `.anthropic_oauth.json`, `mcp-tokens/`, `pairing/` under HERMES_HOME (active profile and global root) | +| Hermes credential stores | `auth.json`, `.env`, `.anthropic_oauth.json`, `webhook_subscriptions.json`, `auth/google_oauth.json`, Bitwarden cache (`cache/bws_cache.json`, `cache/bws_cache.enc.json`), `vault/`, `browser-profile/`, `mcp-tokens/`, `pairing/` under HERMES_HOME (active profile and global root) | | Project secret files | `.env`, `.env.local`, `.env.production`, `.envrc` anywhere on disk | | Windows NT/device-namespace paths | `\??\...`, `\\.\...`, `\\?\UNC\...`, `\\?\GLOBALROOT...` — rejected for both reads and writes on every platform. On Windows, merely *resolving* such a path (e.g. `\??\UNC\host\share`) triggers outbound SMB authentication and can leak the user's NTLM hash; the prefixes also bypass normal path normalization. Ordinary extended-length local paths (`\\?\C:\...`) and plain UNC shares (`\\server\share`) are unaffected. |