File size: 5,650 Bytes
c453128 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | """Red-team tests for the host-side path-confinement boundary (`_safe_path`).
`_safe_path()` guards the three endpoints that turn a caller-supplied path into a
host filesystem read / open:
GET /api/file/preview?path=... (read file contents)
GET /api/file/tree?root=... (list a directory)
POST /api/workspace/open (open in Finder/terminal)
The allowed roots are the workspace root and the Hermes home dir. The interesting
attacks are the ones that *look* like an allowed path but resolve elsewhere:
* a symlink placed inside the workspace whose target is outside every root,
* `..` traversal that climbs out of the workspace,
* an absolute path straight at a host secret.
The fix relies on `Path(raw).resolve()` following symlinks *before* the
containment check. These tests pin that behaviour so a future "optimisation"
that drops the resolve (or checks the literal path) can't silently re-open the
hole. Existing happy-path / basic-rejection coverage lives in test_server.py and
test_workspace_open.py; this file is the adversarial layer.
"""
import os
import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from agent_gui.server import create_app
@pytest.fixture()
def roots(tmp_path):
"""(hermes_home, workspace_root) with the workspace under its own dir.
Unlike test_server.py (where home == tmp_path, so the workspace sits *inside*
home), here home and the workspace are siblings and `secrets/` is a third
sibling outside BOTH roots β a realistic escape target.
"""
home = tmp_path / "hermes_home"
ws = tmp_path / "workspace"
secrets = tmp_path / "secrets"
for d in (home, ws, secrets):
d.mkdir()
(secrets / "id_rsa").write_text("PRIVATE KEY\n")
return home, ws, secrets
@pytest.fixture()
def app(roots):
home, ws, _ = roots
return create_app(hermes_home=str(home), workspace_root=str(ws))
@pytest_asyncio.fixture()
async def client(app):
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
# ββ Symlink escape βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.mark.asyncio
async def test_preview_symlink_out_of_workspace_is_forbidden(client, roots):
"""A symlink that lives inside the workspace but points at a host secret must
not leak the secret β `.resolve()` follows the link, containment then fails."""
home, ws, secrets = roots
link = ws / "innocent.txt"
link.symlink_to(secrets / "id_rsa")
r = await client.get("/api/file/preview", params={"path": str(link)})
assert r.status_code == 403
assert "PRIVATE KEY" not in r.text
@pytest.mark.asyncio
async def test_tree_symlinked_dir_out_of_workspace_is_forbidden(client, roots):
"""A symlinked directory inside the workspace pointing outside is rejected."""
home, ws, secrets = roots
link = ws / "shared"
link.symlink_to(secrets, target_is_directory=True)
r = await client.get("/api/file/tree", params={"root": str(link)})
assert r.status_code == 403
assert "id_rsa" not in r.text
@pytest.mark.asyncio
async def test_open_symlink_out_of_workspace_is_forbidden(client, roots):
"""workspace/open must refuse a symlink that escapes the allowed roots."""
home, ws, secrets = roots
link = ws / "shortcut"
link.symlink_to(secrets, target_is_directory=True)
r = await client.post("/api/workspace/open", json={"path": str(link)})
assert r.status_code == 403
# ββ `..` traversal βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.mark.asyncio
async def test_preview_dotdot_traversal_is_forbidden(client, roots):
"""`<ws>/../secrets/id_rsa` resolves outside the roots β 403."""
home, ws, secrets = roots
sneaky = ws / ".." / "secrets" / "id_rsa"
r = await client.get("/api/file/preview", params={"path": str(sneaky)})
assert r.status_code == 403
assert "PRIVATE KEY" not in r.text
@pytest.mark.asyncio
async def test_preview_absolute_secret_path_is_forbidden(client, roots):
"""A plain absolute path at a host secret is rejected outright."""
home, ws, secrets = roots
r = await client.get("/api/file/preview", params={"path": str(secrets / "id_rsa")})
assert r.status_code == 403
# ββ The boundary must not over-block legitimate access βββββββββββββββββββββββ
@pytest.mark.asyncio
async def test_symlink_within_workspace_is_allowed(client, roots):
"""A symlink whose target is *also* inside the workspace stays allowed β the
guard blocks escapes, not indirection per se."""
home, ws, secrets = roots
real = ws / "real.txt"
real.write_text("hello from inside\n")
link = ws / "alias.txt"
link.symlink_to(real)
r = await client.get("/api/file/preview", params={"path": str(link)})
assert r.status_code == 200
assert "hello from inside" in r.json()["content"]
@pytest.mark.asyncio
async def test_file_under_hermes_home_is_allowed(client, roots):
"""Hermes home is the second allowed root (config/memory previews rely on it)."""
home, ws, secrets = roots
cfg = home / "notes.log"
cfg.write_text("log line\n")
r = await client.get("/api/file/preview", params={"path": str(cfg)})
assert r.status_code == 200
assert "log line" in r.json()["content"]
|