Skip to content

security: dashboard refuses cross-site writes and foreign Host headers (#118) - #160

Merged
vsn411 merged 2 commits into
mainfrom
fix/dashboard-cross-site
Nov 20, 2026
Merged

vsn411 merged 2 commits into
mainfrom
fix/dashboard-cross-site

Conversation

@vsn411

@vsn411 vsn411 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Part of #118 (CSRF, DNS rebinding). Found during end-to-end testing before go-live, and confirmed against a live secfoo serve built from main:

Attack Before After
A page you visit POSTs a form to /assessments (same for /assessments/{id}/runs, which starts a paid agent run, /delete, finding review) 303, assessment created 403, nothing written
DNS rebinding: Host: rebind.evil.example → 127.0.0.1 full dashboard served (an attacker's page could read every report) 400
Framing / sniffing no CSP, no X-Frame-Options headers on every response

What changed

New src/secfoo/web/security.py adds one middleware, installed as the outermost layer:

  • Cross-site writes. Unsafe methods must come from the dashboard's own origin.
    • A request whose Origin (including null) or Sec-Fetch-Site names another origin gets 403; so does one with only a Referer naming another origin.
    • "Same site" (another local port) counts as foreign.
    • Requests with none of these headers (curl, scripts, the CLI) pass.
    • /api/runtime/ is exempt: it is authenticated by an Authorization header, which a cross-site page cannot set without a CORS preflight the app never grants.
  • DNS rebinding. Host must be localhost, 127.0.0.1, [::1] or --host; anything else gets 400.
    • New secfoo serve --allow-host NAME, repeatable, for port-forwarding or proxy names. The error message says so.
    • A wildcard --host 0.0.0.0 accepts any name, but still refuses cross-site writes.
  • Headers: Content-Security-Policy (default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; … frame-ancestors 'none'), X-Frame-Options: DENY, nosniff, Referrer-Policy: same-origin, COOP/CORP same-origin.
    • style-src needs 'unsafe-inline' because Mermaid writes <style> into its SVGs. No script is inline: test_no_inline_scripts.py already enforces that.
  • secfoo serve warns when --host is not loopback (no login).
  • SECURITY.md documents all of the above.

Verification

  • tests/test_web/test_dashboard_security.py, 28 tests:
    • cross-site variants have no side effect
    • same-origin browser POSTs and header-less clients still work
    • every unsafe route the app registers is enumerated and must refuse a cross-site request, so future routes are covered automatically
    • rebinding host variants (127.0.0.1.evil.example, empty Host, …), IPv6/uppercase/trailing-dot loopback, bind host, wildcard, --allow-host
    • headers on pages, static files and rejections
  • 16 of the 28 fail with the middleware disabled.
  • Real browser: headless Chrome loads /, /assessments, /assessments/new, /agents, /activities/sast and a run report containing a Mermaid diagram.
    • Result: 0 CSP violations, and the flowchart renders.
    • Control: with script-src 'none' the same check reports 28 violations, so it does detect breakage.
  • Full suite: 1862 passed.

tests/conftest.py allows Starlette TestClient's default Host: testserver for the rest of the suite. The new tests turn that off.

Still open in #118 for contributors: the systematic XSS corpus for report content. A 22-payload spot check through render_report_html found nothing executable.

🤖 Generated with Claude Code

Satyanarayana Vuppala and others added 2 commits November 20, 2026 13:49
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 <[email protected]>
@vsn411
vsn411 merged commit f4332c2 into main Nov 20, 2026
7 checks passed
vsn411 added a commit that referenced this pull request Nov 21, 2026
…ashboard (#118) (#189)

* security: XSS corpus, bounded uploads and runtime API audit for the dashboard

The rest of #118 after #160 (cross-site writes, DNS rebinding, headers).

- XSS regression corpus: 56 payloads through render_report_html and
  through the run, SAST and secret-scanning pages that show report data;
  a page must not gain any script, frame, form, image, inline handler or
  non-http(s) URL compared with the same page built from harmless text.
- Uploads: every attachment kind is capped at MAX_UPLOAD_BYTES, the copy
  stops at the cap, and the middleware refuses a larger Content-Length
  (413) or a missing one (411) before Starlette spools the body. A
  PDF/DOCX/PPTX whose bytes are not that format gets 415 before any parser
  runs. DOCX/PPTX archives that expand past 200 MB or hold more than 5,000
  members are not parsed.
- Fuzzing docext for 60 s with the new type check found that zipfile
  raises NotImplementedError for an unsupported version; fixed, and the
  reproducer is in fuzz/corpus/docext.
- Runtime API: a test enumerates every /api/runtime route and requires 401
  without a valid key; the stored key hash is compared with
  hmac.compare_digest; "[::1]", "LOCALHOST" and "localhost." binds count
  as loopback for the TLS check; --allow-insecure-runtime is documented as
  lifting only that check.
- SECURITY.md: "Local dashboard threat model".

Closes #118

* test(web): XSS page comparison with the Mermaid bundle vendored

CI has mermaid.min.js vendored, so pages showing a diagram load the
Mermaid scripts and the poisoned Mermaid payloads gained two dashboard
script tags over a diagram-free baseline. The baseline now has a harmless
diagram, the fixture renders as if the bundle is vendored (the riskier
case), and a page fails only if it gains something over the baseline.

---------

Co-authored-by: aitechnology <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant