The repo had no tests, no lint config, and a CI that went straight from push
to docker push. That is the reason F1 could ship: authorization lives in the
routers, each of 171 routes independently picks require_admin or
get_current_user, and nothing checked the choice was right.
670 tests, no Docker daemon needed. The app is driven through TestClient
without entering it as a context manager, which skips the lifespan — no
background loops, no socket — and conftest points DATA_DIR/STACKS_DIR at a
temp directory before anything is imported.
test_route_authorization.py is the load-bearing one. Rather than 171 implied
decisions it states the policy once — every route requires admin unless it is
listed in USER_READABLE or PUBLIC — and fails on any route that disagrees. A
new route defaults to admin, which is the safe direction; what it catches is a
route written with get_current_user that nobody weighed against "can this
return a credential". Writing the allowlist meant auditing all 53 user-readable
routes, which turned up one more leak: GET /api/templates/{id} returns a
template's env, and "save stack as template" snapshots the stack's real .env
into it. Now admin-only; the listing stays open.
test_agent_authorization.py pins the same invariant on the agent, where the
whole access model is one shared token declared per route and a single
forgotten Depends(verify_token) would hand over the host.
Both were checked by reintroducing the bug: re-opening /api/files/read fails
three tests with actionable messages, dropping a token guard fails two.
test_bundled_templates.py covers the 83 templates — parse, image per service,
.env.example in sync with what compose reads, every bind-mounted file actually
shipped, and no working default password. It found one on its first run:
authentik shipped PG_PASS=change-me and AUTHENTIK_SECRET_KEY=change-me against
a compose that marks both required, so the stack would have come up with a
known password instead of refusing to start. Fixed.
The rest ports the ad-hoc harnesses from 0.44.0 into permanent tests (crypto
round-trip incl. plaintext passthrough and key-loss handling, the browse
sandbox) and covers compose_service's slug/status/file handling and
secret_service's name validation.
ruff is configured as a floor, not a style bar: F, E9 and B only. Import
sorting is deliberately out — it is style, and enabling it would rewrite the
imports of nine files that have nothing else wrong. The 12 findings it did have
are fixed here (unused imports, an unused local, four raise-without-from that
were swallowing exception context).
CI now runs check (ruff, pytest, tsc) and only builds if it passes.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dk43rmEeRfYi5wsLDbmfyG
98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
"""The agent's token guard.
|
|
|
|
The agent has no users and no roles: one shared ``AGENT_TOKEN`` is the whole
|
|
access-control model, declared per route as
|
|
``dependencies=[Depends(verify_token)]``. That makes a forgotten decorator
|
|
argument the entire failure mode — one route without it hands anonymous full
|
|
Docker control of that host, and nothing in review would show it.
|
|
|
|
So the invariant is asserted here rather than assumed across 50 decorators.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
#: The only agent routes that may answer without a token — the liveness probe
|
|
#: the container's HEALTHCHECK calls, which returns nothing but a version.
|
|
UNAUTHENTICATED = {"GET /agent/health"}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def agent_app():
|
|
import agent_app as module
|
|
|
|
return module.app
|
|
|
|
|
|
def _routes(app):
|
|
from fastapi.routing import APIRoute
|
|
|
|
out = []
|
|
for route in app.routes:
|
|
if not isinstance(route, APIRoute):
|
|
continue
|
|
for method in sorted(route.methods - {"HEAD", "OPTIONS"}):
|
|
out.append((f"{method} {route.path}", route))
|
|
return sorted(out, key=lambda r: r[0])
|
|
|
|
|
|
def _has_token_guard(route) -> bool:
|
|
from agent_app import verify_token
|
|
|
|
found = [False]
|
|
|
|
def walk(dependant):
|
|
for sub in dependant.dependencies:
|
|
if sub.call is verify_token:
|
|
found[0] = True
|
|
walk(sub)
|
|
|
|
walk(route.dependant)
|
|
return found[0]
|
|
|
|
|
|
def test_every_agent_route_requires_the_token(agent_app):
|
|
unguarded = {
|
|
key
|
|
for key, route in _routes(agent_app)
|
|
if not _has_token_guard(route) and not key.startswith("GET /agent/health")
|
|
}
|
|
assert not unguarded, (
|
|
"These agent routes answer without AGENT_TOKEN, which is full Docker "
|
|
f"access to the host: {sorted(unguarded)}"
|
|
)
|
|
|
|
|
|
def test_only_the_health_probe_is_unauthenticated(agent_app):
|
|
open_routes = {key for key, route in _routes(agent_app) if not _has_token_guard(route)}
|
|
assert open_routes == UNAUTHENTICATED
|
|
|
|
|
|
def test_a_wrong_token_is_rejected(agent_app, monkeypatch):
|
|
from starlette.testclient import TestClient
|
|
|
|
from config import settings
|
|
|
|
monkeypatch.setattr(settings, "AGENT_TOKEN", "the-real-token", raising=False)
|
|
client = TestClient(agent_app, raise_server_exceptions=False)
|
|
|
|
assert client.get("/agent/ping").status_code == 401
|
|
assert client.get(
|
|
"/agent/ping", headers={"Authorization": "Bearer wrong"}
|
|
).status_code == 401
|
|
# The health probe stays open so the container's HEALTHCHECK works.
|
|
assert client.get("/agent/health").status_code == 200
|
|
|
|
|
|
def test_an_unset_token_refuses_everything(agent_app, monkeypatch):
|
|
"""An agent started without AGENT_TOKEN must not be wide open."""
|
|
from starlette.testclient import TestClient
|
|
|
|
from config import settings
|
|
|
|
monkeypatch.setattr(settings, "AGENT_TOKEN", "", raising=False)
|
|
client = TestClient(agent_app, raise_server_exceptions=False)
|
|
|
|
response = client.get("/agent/ping", headers={"Authorization": "Bearer anything"})
|
|
assert response.status_code == 503
|