All verification is complete — this pass needed no code changes. Final report: **Phase 91 — Admin Theme tab: final verification pass (all 6 tasks already in `complete/`)** - Verified pre-paint theming end-to-end: `ui_settings` store + resolver, admin `GET/PUT /api/ui-settings`, `CachingMiddleware` inline-`<style id="bor-theme">` injection before `</head>` (incl. `/shared/<token>` prefix branch, unit-pinned), CSP sha256 exemption for the inline tag, Theme tab shell + `theme.js` editor, CSS-file theming fully retired. - No defects found; zero changes made — working tree left exactly as the task executors left it. - Tests: `uv run pytest --cov=app` → 1841 passed, 0 failed (TOTAL coverage **99%**; theming/ui_settings/caching all 100%); `uv run pytest tests/e2e/test_admin_theme_tab.py -v --no-cov` → **5 passed** in isolation. - Lint/types: `uv run ruff check .` → All checks passed; `uv run pyright` → 0 errors, 0 warnings. - Criteria: (1) unset deployment byte-identical, no `#bor-theme` anywhere — ✓ (unit no-op test + E2E reset byte-compare); `rg "BOR_THEME|themes/"` → single hit is the permitted doc-history comment in `frontend/index.html`. (2) admin-only gate + 403s for anonymous and token users — ✓ (E2E test 3). (3) saved theme inline before `</head>` on every page incl. `/shared/<token>`, computed `--brand` on first paint for admin + anonymous — ✓ (E2E test 2 + unit). (4) reset → byte-identical; 5 contrast pairs warn <4.5:1, non-blocking — ✓ (E2E tests 4–5). (5) suite green, >90% coverage, lint clean — ✓. (6) commit deferred to harness per rules. - Notable: `.agents/PLAN.md` is absent from the repo — the phase overview's Design section was used as the binding spec; no deviation resulted. - Next pending phase: **none** — 91 is the last phase in `todo/`.
96 lines
4.7 KiB
Python
96 lines
4.7 KiB
Python
"""Security headers on every response (phase 82 — security audit SEC-04).
|
|
|
|
Audit basis (SEC-04, severity Medium): no response in the app carried
|
|
``Content-Security-Policy``, ``X-Frame-Options``, or
|
|
``X-Content-Type-Options``. Every page — including the admin sign-in and
|
|
the admin-only views — could be embedded in a third-party page's
|
|
iframe: a malicious LAN page could overlay the admin UI and trick the
|
|
signed-in owner into clicking Sync / Revoke / Delete-source actions
|
|
(audit PoC: ``<iframe src="http://<server>:8000/git-sources.html">``
|
|
with a benign-looking overlay). ``frame-ancestors 'none'`` (in the
|
|
CSP) plus ``X-Frame-Options: DENY`` (the legacy fallback for pre-CSP
|
|
browsers) close that clickjacking vector. The CSP itself is the
|
|
standard second line of defense against any future XSS regression —
|
|
cheap here because the frontend is No-CDN and was verified for this
|
|
phase to have **no** inline ``<style>`` blocks, **no** ``style="…"``
|
|
attributes, **no** ``onclick``/``on*`` handlers, **no** ``javascript:``
|
|
URLs, and **no** external hosts (inline ``<svg>`` elements are
|
|
CSP-legal; the two ``el.style.…`` CSSOM writes in ``app.js`` are not
|
|
CSP-blocked). If inline scripts/styles are ever introduced, re-verify
|
|
that: the policy would break the affected page loudly, and that loud
|
|
failure is the feature, not the bug.
|
|
|
|
``X-Content-Type-Options: nosniff`` is the one-line belt-and-braces
|
|
against MIME confusion on the static mount.
|
|
|
|
Why pure ASGI: this is a *header-only* middleware. It wraps ``send``
|
|
and writes the three headers on ``http.response.start`` only — it
|
|
never reads or buffers a body, so the SSE chat stream and every other
|
|
response pass through byte-identical. That is the explicit contrast
|
|
with ``app/core/caching.py``'s ``CachingMiddleware``, which
|
|
*does* buffer page bodies for the ``?v=`` rewrite. No HSTS (plain
|
|
homelab HTTP — TLS is the documented non-goal), no ``Referrer-Policy``
|
|
/ ``Permissions-Policy`` (owner decision A4: the three headers above
|
|
are the scope).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from starlette.datastructures import MutableHeaders
|
|
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
|
|
#: The exact owner-approved policy (phase 82, decision A1).
|
|
#: ``default-src 'self'`` is inherited by every sub-policy that has no
|
|
#: explicit entry (``script-src``, ``style-src``, ``connect-src``, …),
|
|
#: ``base-uri 'none'`` blocks base-tag hijacking, and
|
|
#: ``frame-ancestors 'none'`` forbids any framing. No ``'unsafe-inline'``
|
|
#: (verified unnecessary — see module docstring), no ``report-uri`` /
|
|
#: ``report-to`` (no collector in the homelab — a report would just
|
|
#: vanish).
|
|
CSP = "default-src 'self'; base-uri 'none'; frame-ancestors 'none'"
|
|
|
|
|
|
class SecurityHeadersMiddleware:
|
|
"""Header-only, pure-ASGI — never reads or buffers a body (SSE-safe).
|
|
|
|
Adds exactly three headers to every HTTP response:
|
|
|
|
* ``Content-Security-Policy``: the strict same-origin policy above
|
|
(``frame-ancestors 'none'`` → clickjacking closed, SEC-04) —
|
|
EXCEPT when an inner layer has already set one: the phase-91
|
|
(task 05) pre-paint theme tag is an inline ``<style>`` that the
|
|
A1 policy would block in the browser, so the caching middleware
|
|
publishes, on themed HTML pages only, the A1 string with
|
|
``style-src 'self' 'sha256-<tag-content-hash>'`` appended (the
|
|
current theme is the only inline style ever permitted — no
|
|
``'unsafe-inline'``). A pre-existing CSP is that inner layer's
|
|
deliberate one and is preserved; every other response (including
|
|
every untagged page) gets the plain A1 string.
|
|
* ``X-Frame-Options: DENY`` — legacy no-framing fallback;
|
|
* ``X-Content-Type-Options: nosniff`` — MIME-confusion belt.
|
|
|
|
Non-HTTP scopes (websocket, lifespan) pass through untouched.
|
|
"""
|
|
|
|
def __init__(self, app: ASGIApp) -> None:
|
|
self.app = app
|
|
|
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
if scope["type"] != "http":
|
|
await self.app(scope, receive, send)
|
|
return
|
|
|
|
async def send_wrapper(message: Message) -> None:
|
|
if message["type"] == "http.response.start":
|
|
headers = MutableHeaders(scope=message)
|
|
# Phase 91 (task 05): preserve a CSP an inner layer set
|
|
# (the caching middleware's theme-extended policy — see
|
|
# the class docstring); the A1 string covers every
|
|
# response without one.
|
|
if "content-security-policy" not in headers:
|
|
headers["Content-Security-Policy"] = CSP
|
|
headers["X-Frame-Options"] = "DENY"
|
|
headers["X-Content-Type-Options"] = "nosniff"
|
|
await send(message)
|
|
|
|
await self.app(scope, receive, send_wrapper)
|