From 244cf3ab75e59b1582b0de4ce9bdc7cd0f9724da Mon Sep 17 00:00:00 2001 From: Satyanarayana Vuppala Date: Fri, 20 Nov 2026 13:49:06 +0000 Subject: [PATCH] security: dashboard refuses cross-site writes and foreign Host headers secfoo serve has no login and binds 127.0.0.1, so the threat is the user's own browser. Verified against a live server before this change: a cross-site form POST created an assessment (the same works for launching a paid agent run or deleting a case file), and a request with Host: rebind.evil.example was served the full dashboard (DNS rebinding). - Unsafe methods must come from the dashboard's own origin: Origin, Sec-Fetch-Site or (failing both) Referer naming another origin -> 403. Clients that send none (curl, scripts) are unaffected; the key- authenticated runtime ingest API is exempt from this check. - Host must be a loopback name or the --host; anything else -> 400. --allow-host NAME adds a forwarding/proxy name; a wildcard bind accepts any. - Security headers on every response, including a CSP with script-src 'self' and frame-ancestors 'none'. Checked in headless Chrome: no violations on six pages and Mermaid still renders (a deliberately broken policy produces 28 violations, so the check works). - serve warns when --host is not loopback. Part of #118. Co-Authored-By: Claude Opus 5.5 --- SECURITY.md | 21 ++- src/secfoo/cli.py | 16 ++- src/secfoo/web/app.py | 11 +- src/secfoo/web/security.py | 122 +++++++++++++++++ tests/conftest.py | 7 + tests/test_web/test_dashboard_security.py | 153 ++++++++++++++++++++++ 6 files changed, 327 insertions(+), 3 deletions(-) create mode 100644 src/secfoo/web/security.py create mode 100644 tests/test_web/test_dashboard_security.py diff --git a/SECURITY.md b/SECURITY.md index b65d603..d5727d0 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -24,7 +24,26 @@ way you'd treat opening it in your agent CLI in the first place. `secfoo serve` binds to `127.0.0.1` by default and has no authentication — it is meant for local use. Do not expose it on a network interface without -putting your own authenticating reverse proxy in front of it. +putting your own authenticating reverse proxy in front of it (`secfoo serve` +prints a warning when `--host` is not loopback). + +Because the dashboard runs on your machine while you browse other sites, it +defends against your own browser being used against it: + +- **Cross-site writes are refused.** Every state-changing request (start an + agent run, delete a case file, review a finding) must come from the + dashboard's own origin; a request whose `Origin`, `Sec-Fetch-Site` or + `Referer` names another site gets `403`. Clients that send none of these + (curl, scripts) are unaffected. The runtime ingest API is authenticated by + its API key instead. +- **DNS rebinding is refused.** Requests must name the dashboard by + `localhost`, `127.0.0.1`, `[::1]` or its `--host`; any other `Host` gets + `400`. Reaching it through a port-forwarding or proxy name? Add that name + with `secfoo serve --allow-host NAME`. A wildcard `--host 0.0.0.0` accepts + any name. +- **Security headers** on every response: a Content-Security-Policy with + `script-src 'self'` and `frame-ancestors 'none'`, `X-Frame-Options: DENY`, + `X-Content-Type-Options: nosniff`, `Referrer-Policy: same-origin`. `secfoo cloud login` stores an API key in `~/.secfoo/cloud.toml` (mode `0600`). This key is scoped to push completed run data to your diff --git a/src/secfoo/cli.py b/src/secfoo/cli.py index 7436601..b9b4011 100644 --- a/src/secfoo/cli.py +++ b/src/secfoo/cli.py @@ -873,6 +873,11 @@ def serve( "--runtime-recheck-hours", help="Re-check running package versions against OSV this often (#68). 0 disables.", )] = 6.0, + allow_host: Annotated[Optional[list[str]], typer.Option( + "--allow-host", + help="Also answer to this host name (repeatable), e.g. a port-forwarding or reverse-proxy name. " + "Requests naming any other host are refused, which blocks DNS rebinding.", + )] = None, ) -> None: """Launch the local web dashboard.""" if ctx.invoked_subcommand is not None: @@ -885,7 +890,16 @@ def serve( if runtime_recheck_hours > 0: _start_runtime_recheck(runtime_recheck_hours) console.print(f"Serving dashboard at [bold]http://{host}:{port}[/]") - uvicorn.run(create_app(bind_host=host, allow_insecure_runtime=allow_insecure_runtime), host=host, port=port) + from secfoo.web.security import is_loopback + + if not is_loopback(host): + err_console.print( + f"[yellow]Warning:[/] --host {host} is reachable from other machines. The dashboard has no login: " + "anyone who can reach it can read every report and start agent runs. Prefer 127.0.0.1 " + "(or an SSH tunnel / authenticating reverse proxy)." + ) + app = create_app(bind_host=host, allow_insecure_runtime=allow_insecure_runtime, allow_hosts=allow_host or ()) + uvicorn.run(app, host=host, port=port) def _start_runtime_recheck(hours: float) -> None: diff --git a/src/secfoo/web/app.py b/src/secfoo/web/app.py index eb7f67f..bb0a2f3 100644 --- a/src/secfoo/web/app.py +++ b/src/secfoo/web/app.py @@ -1,5 +1,6 @@ from __future__ import annotations +from collections.abc import Iterable from pathlib import Path from fastapi import FastAPI @@ -8,6 +9,7 @@ from secfoo.config import STORE_DIR from secfoo.runtime import api as runtime_api +from secfoo.web import security from secfoo.web.routes import ( activities, agents, @@ -35,7 +37,9 @@ def mermaid_bundle_available() -> bool: return MERMAID_BUNDLE.is_file() -def create_app(*, bind_host: str = "127.0.0.1", allow_insecure_runtime: bool = False) -> FastAPI: +def create_app( + *, bind_host: str = "127.0.0.1", allow_insecure_runtime: bool = False, allow_hosts: Iterable[str] = () +) -> FastAPI: app = FastAPI(title="secfoo dashboard") # Read by the runtime ingest API (#66): on a non-loopback bind it refuses # API keys over plain HTTP unless the operator opted in. @@ -52,6 +56,11 @@ async def revalidate_static(request, call_next): if request.url.path.startswith("/static/"): response.headers["Cache-Control"] = "no-cache" return response + + # Added last so it is the outermost layer: Host/Origin checks run before + # any route, and security headers land on every response, rejections included. + security.install(app, bind_host=bind_host, allow_hosts=allow_hosts) + # Mounted BEFORE /static: Starlette matches mounts in registration # order, so the broader /static would otherwise swallow /static/vendor. VENDOR_DIR.mkdir(parents=True, exist_ok=True) diff --git a/src/secfoo/web/security.py b/src/secfoo/web/security.py new file mode 100644 index 0000000..b574213 --- /dev/null +++ b/src/secfoo/web/security.py @@ -0,0 +1,122 @@ +"""Browser-facing protections for the local dashboard (#118). + +`secfoo serve` binds 127.0.0.1 and has no login, so the threat is the user's +own browser: any page they visit can send requests to http://127.0.0.1:8787. + +- Cross-site writes: a page can POST a form (or `fetch(..., {mode: "no-cors"})`) + to start a paid agent run, delete a case file or flip a finding review. + Unsafe methods must come from the dashboard's own origin. Browsers always + send `Origin` (and modern ones `Sec-Fetch-Site`) on such requests, so a + request carrying neither is a non-browser client (curl, the CLI, tests). +- DNS rebinding: an attacker's hostname that resolves to 127.0.0.1 makes the + dashboard same-origin with the attacker's page, which could then read every + report. Requests must name the dashboard by a loopback host or the --host + it was started with. +- Framing and content sniffing: security headers on every response. + +The runtime ingest API (/api/runtime/) authenticates with an API key in the +Authorization header, which a cross-site page cannot set without a CORS +preflight the app never grants, so it is exempt from the origin check (sensors +are not browsers) but not from the Host check. +""" +from __future__ import annotations + +from urllib.parse import urlsplit + +from starlette.responses import PlainTextResponse + +LOOPBACK_HOSTS = {"localhost", "127.0.0.1", "::1"} +WILDCARD_BINDS = {"", "0.0.0.0", "::"} +SAFE_METHODS = {"GET", "HEAD", "OPTIONS"} +MACHINE_API_PREFIX = "/api/runtime/" + +CONTENT_SECURITY_POLICY = ( + "default-src 'self'; " + "script-src 'self'; " + # Mermaid writes