Put nosniff, X-Frame-Options: DENY and Referrer-Policy: same-origin on every HTTP answer through the boilerplate's Hardening middleware, outermost and leaving the socket alone, name the API tokens in the Account page's lede, and reach the open test app as 127.0.0.1, since auth now answers 421 to any other Host
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
244e57ac29
commit
14793f4707
4 files changed
+144
-6
No files matched your search
+47
-4
@@ -4,8 +4,9 @@
|
||||
uv run uvicorn app.main:app --reload
|
||||
|
||||
`create_app()` is the whole of the wiring, and the order in it is the point:
|
||||
config and store, the door, the queue, the pages. Everything before the queue
|
||||
is something the queue needs; everything after it is something that enqueues.
|
||||
config and store, the door, the queue, the pages, and around all of them the
|
||||
hardening headers. Everything before the queue is something the queue needs;
|
||||
everything after it is something that enqueues.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -17,7 +18,9 @@ from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from starlette.datastructures import MutableHeaders
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
||||
|
||||
import auth
|
||||
from app import jobs
|
||||
@@ -33,6 +36,39 @@ log = logging.getLogger(__name__)
|
||||
#: data gets `{"detail": ...}`; a browser that asked for a page gets one.
|
||||
API_PREFIXES = ("/api", "/ws", "/_jobs", "/_auth", "/static", "/health")
|
||||
|
||||
#: On every HTTP answer, the door's refusals and the error pages included. No
|
||||
#: CSP yet: the templates carry inline scripts and styles, which would need a
|
||||
#: nonce or a hash each.
|
||||
HARDENING = {
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"X-Frame-Options": "DENY",
|
||||
"Referrer-Policy": "same-origin",
|
||||
}
|
||||
|
||||
|
||||
class Hardening:
|
||||
"""`HARDENING` on every response that does not set the header itself.
|
||||
|
||||
Pure ASGI rather than `BaseHTTPMiddleware`, so a streamed answer and the
|
||||
socket 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 harden(message: Message) -> None:
|
||||
if message["type"] == "http.response.start":
|
||||
headers = MutableHeaders(scope=message)
|
||||
for name, value in HARDENING.items():
|
||||
headers.setdefault(name, value)
|
||||
await send(message)
|
||||
|
||||
await self.app(scope, receive, harden)
|
||||
|
||||
|
||||
def create_app(config: Config | None = None) -> FastAPI:
|
||||
"""Build the application. A test passes its own config; nobody else does."""
|
||||
@@ -59,8 +95,12 @@ def create_app(config: Config | None = None) -> FastAPI:
|
||||
|
||||
# The door, first, because it is the door. The middleware covers every
|
||||
# route whenever it was added, `/ws/jobs` included; written first, nobody
|
||||
# has to work that out. `auto` locks unless the bind address is loopback.
|
||||
# Auth's tables are the `auth_*` ones in the same file.
|
||||
# has to work that out. `auto` locks unless the bind address is loopback,
|
||||
# and an app it leaves open answers 421 to a request that names any host
|
||||
# but a loopback one or comes through a proxy. Auth's tables are the
|
||||
# `auth_*` ones in the same file, `auth_tokens` for the API tokens among
|
||||
# them. No `session_only=`: no route here hands out or replaces the
|
||||
# database as a file.
|
||||
auth.connect(app, auth.AuthStore(config.database), config.auth,
|
||||
host=config.server.host, open=("/static/",),
|
||||
stylesheet="/static/app.css")
|
||||
@@ -84,6 +124,9 @@ def create_app(config: Config | None = None) -> FastAPI:
|
||||
--wait` waits on this, so it has to prove the database answers."""
|
||||
return {"ok": True, "open_roles": sum(store.open_role_counts().values())}
|
||||
|
||||
# Last, so outermost: around the door and the error pages.
|
||||
app.add_middleware(Hardening)
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def errors(request: Request, exc: StarletteHTTPException) -> Any:
|
||||
"""JSON for a caller, a page for a browser. One shape for every API
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
{% block content %}
|
||||
<h1>Account</h1>
|
||||
<p class="lede">The one password, the devices signed in with it, and the passkeys that sign in without it.</p>
|
||||
<p class="lede">The one password, the devices signed in with it, the passkeys that sign in without it, and the API tokens a script sends instead.</p>
|
||||
|
||||
<section class="card">
|
||||
{# As in Sources' Runs: the element and its script belong to
|
||||
|
||||
Reference in new issue
Block a user