aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/app.py
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-09-01 20:03:03 +0200
committerChristophe Besson <cbesson@gmail.com>2026-09-01 20:03:03 +0200
commitf2915bc7b1550b80cadfa36f5910223106cb3bfe (patch)
tree34fa6760db608968057feac31b5f38bcca28bfa4 /packages/meshbay-hub/src/meshbay_hub/app.py
parent24d29e910c8d1649a1cd51969f27b921b94d97e3 (diff)
downloadmeshbay-f2915bc7b1550b80cadfa36f5910223106cb3bfe.tar.gz
fix(hub): send a CSP and protective headers on every response
The SPA shell and its assets went out with no Content-Security-Policy and no X-Content-Type-Options / Referrer-Policy / X-Frame-Options — so an injection that reached the SPA (rendered third-party OpenGraph data, a federated group name, chat content) had nothing stopping it from loading more code or exfiltrating to any host, and the page could be framed by any site. A middleware in `create_app` now adds all four to every response. `webapp.CSP` is deliberately the *same* policy the desktop client's protocol handler already enforces on these exact UI files, plus the two reCAPTCHA hosts the sign-up widget needs: `default-src 'none'`, `script-src 'self' 'wasm-unsafe-eval' <recaptcha>` (the hub's own origin is not a script source — T3), `style-src 'self' 'unsafe-inline'` (htm/preact inline `style=` only, nothing executes), `connect-src 'self' https: wss:`, `frame-ancestors 'none'`, `base-uri 'none'`, `form-action 'none'`. The shell's dead `<script>window.__MB_ASSET_V = ...</script>` is removed (nothing has ever read it) so `script-src` needs no inline allowance. Needs verification against the running SPA — a mis-tuned CSP shows as a blank page — but it matches a policy already proven with these files under Electron. Second-review L5 / third-review M5. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011pG75yGK3NthNfyjH74omG
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/app.py')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/app.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/app.py b/packages/meshbay-hub/src/meshbay_hub/app.py
index 76d7ec0..2daa55b 100644
--- a/packages/meshbay-hub/src/meshbay_hub/app.py
+++ b/packages/meshbay-hub/src/meshbay_hub/app.py
@@ -35,7 +35,7 @@ from meshbay_hub.api.relay import router as relay_router
from meshbay_hub.api.signaling import router as signaling_router
from meshbay_hub.api.admin import router as admin_router
from meshbay_hub.api.notifications import router as notifications_router
-from meshbay_hub.api.webapp import router as webapp_router, STATIC_DIR, ASSET_V
+from meshbay_hub.api.webapp import router as webapp_router, STATIC_DIR, ASSET_V, CSP
from meshbay_hub.api.middleware import limiter
@@ -142,6 +142,21 @@ def create_app(cfg: HubConfig | None = None) -> FastAPI:
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
+ @app.middleware("http")
+ async def _security_headers(request, call_next):
+ """
+ Second-review L5, third-review M5: the SPA shell and its assets went out
+ with no CSP and no other protective headers. This adds them everywhere —
+ `webapp.CSP` is the same policy the desktop client already enforces on
+ these exact files. `setdefault` so a route that sets its own wins.
+ """
+ response = await call_next(request)
+ response.headers.setdefault("Content-Security-Policy", CSP)
+ response.headers.setdefault("X-Content-Type-Options", "nosniff")
+ response.headers.setdefault("Referrer-Policy", "strict-origin-when-cross-origin")
+ response.headers.setdefault("X-Frame-Options", "DENY")
+ return response
+
# Routers (webapp last — catches / before API routes)
app.include_router(hub_router)
app.include_router(users_router)