Skip to content

sec(web): harden local dashboard against CSRF, DNS rebinding, and XSS - #139

Closed
kushaldave002 wants to merge 3 commits into
secfoo-com:mainfrom
kushaldave002:fix/dashboard-security-hardening
Closed

kushaldave002 wants to merge 3 commits into
secfoo-com:mainfrom
kushaldave002:fix/dashboard-security-hardening

Conversation

@kushaldave002

Copy link
Copy Markdown
Contributor

Summary

Hardens the local secfoo serve dashboard against CSRF, DNS rebinding, XSS, and related web-based attack vectors targeting local services (as tracked in #118).

Key Hardening Measures

  1. CSRF & Cross-Site Origin Validation (CrossSiteProtectionMiddleware):

    • Validates incoming Origin, Sec-Fetch-Site: cross-site, and Referer headers on all state-changing endpoints (POST, PUT, PATCH, DELETE).
    • Rejects cross-origin browser requests with 403 Forbidden while allowing local CLI/curl non-browser requests that lack cross-site browser headers.
    • Covers all 6 state-changing dashboard routes:
      • POST /assessments
      • POST /assessments/bulk-status
      • POST /assessments/{assessment_id}/runs
      • POST /assessments/{assessment_id}/attachments
      • POST /assessments/{assessment_id}/delete
      • POST /activities/sast/findings/{fingerprint}/review
  2. DNS Rebinding Defense (TrustedHostMiddleware):

    • Restricts accepted Host headers to loopback hostnames (localhost, 127.0.0.1, [::1], testserver, and the explicitly configured bind_host). Unrecognized hosts are rejected with 400 Bad Request.
  3. Security Headers (SecurityHeadersMiddleware):

    • Adds CSP (script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net ..., style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net ..., frame-ancestors 'none', object-src 'none').
    • Adds X-Frame-Options: DENY, X-Content-Type-Options: nosniff, and Referrer-Policy: strict-origin-when-cross-origin.
  4. File Upload Restrictions & Streamed Limits:

    • Enforces extension allowlist (.pdf, .docx, .pptx, .md, .html, .htm, .txt, .json, .yaml, .yml, .csv).
    • Limits file uploads chunk-by-chunk during streaming against MAX_UPLOAD_BYTES (50MB), preventing memory/disk exhaustion and cleaning up partial files on breach.
  5. Safe Attachment Downloads:

    • Adds GET /assessments/{assessment_id}/attachments/{attachment_id}/download endpoint.
    • Enforces directory confinement and serves uploads with Content-Type: application/octet-stream, Content-Disposition: attachment; filename="...", and X-Content-Type-Options: nosniff.
  6. Constant-Time Runtime API Key Verification:

    • Uses key_prefix candidate query and hmac.compare_digest to prevent timing side channels.
    • Protects runtime API keys over plaintext HTTP when bound to non-loopback interfaces unless --allow-insecure-runtime is passed.
  7. Fuzzing & Threat Model Documentation:

    • Added Atheris fuzz target fuzz/fuzz_docext.py and Hypothesis property-based fuzz tests in tests/test_docext.py.
    • Updated SECURITY.md with a detailed "Local dashboard threat model" section.
    • Added comprehensive test suite tests/test_web/test_dashboard_security.py (26 tests).

Closes #118

- Protect state-changing dashboard routes with CrossSiteProtectionMiddleware validating Origin, Sec-Fetch-Site, and Referer
- Restrict accepted hostnames to loopback addresses using TrustedHostMiddleware to prevent DNS rebinding
- Add security hardening headers including Content-Security-Policy (CSP), X-Frame-Options: DENY, X-Content-Type-Options: nosniff, and Referrer-Policy: strict-origin-when-cross-origin
- Validate file uploads against permitted document extensions and enforce maximum upload size chunk-by-chunk during streaming
- Serve attachment downloads as application/octet-stream with Content-Disposition: attachment
- Harden runtime API key verification with candidate lookup and constant-time hmac.compare_digest
- Add fuzzing tests and property tests with Atheris and Hypothesis for document text extraction
- Document local dashboard threat model in SECURITY.md
@vsn411

vsn411 commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks for taking on the dashboard hardening, @kushaldave002. The middleware structure is good, --host 0.0.0.0 with Docker -p 8787:8787 still works from localhost, and the web/docext/runtime suites pass. I'm closing this PR because it breaks documented deployments and the CSRF check can still be bypassed, so it shouldn't land as-is. #118 stays open (it's the security review tracking item), and you're welcome to continue it in a new PR or reopen this one once these are addressed. Reviewed at 16a7690.

Blocking

  1. Host allowlist breaks non-loopback use with no override. TrustedHostMiddleware only allows loopback names, testserver and the literal --host value, and create_app(allowed_hosts=...) is never wired to the CLI. With create_app(bind_host='0.0.0.0'), Host: 192.168.1.5:8787, secfoo.internal, secfoo:8787 (compose service) and host.docker.internal:8787 all return 400, including POST /api/runtime/v1/heartbeat. That breaks the documented SECFOO_SERVER=https://secfoo.internal sensor setup. Suggest an --allowed-host option (repeatable) and/or skipping the host check when bound to all interfaces with an explicit opt-in.
  2. Origin/Referer check ignores scheme and port, and accepts Sec-Fetch-Site: same-site. POST /assessments/bulk-status with Origin: http://localhost:3000 or http://127.0.0.1:5173 and Sec-Fetch-Site: same-site returns 303. Any dev server, notebook or preview server on another local port can still fetch() /assessments/{id}/runs (paid agent runs) or /delete, which is the attack Security review of the local dashboard: CSRF on state-changing routes, DNS rebinding, and an XSS regression suite #118 asks to stop. Compare scheme + host + port against the request, and reject Sec-Fetch-Site values other than same-origin/none.
  3. Non-Latin attachment names return 500. Upload 报告.pdf, then GET …/attachments/{id}/download raises UnicodeEncodeError. The hand-built Content-Disposition overrides FileResponse's own filename* handling; drop it and pass only filename=.
  4. IPv6 --host rejects every request. The bind address is added without brackets, but Starlette keeps them on IPv6 literals, so create_app(bind_host='::') with Host: [::]:8787 returns 400.

Should fix

  • CSP vs Mermaid (not reproduced, needs a browser): style-src 'self' without 'unsafe-inline' blocks the <style> Mermaid injects into each SVG, so diagrams likely lose their styling. Please add a browser check, or use a nonce/hash for those pages.
  • Upload size limit runs too late: Starlette has already spooled the whole multipart body to disk before the chunked check, so a huge POST still fills the temp disk. Check Content-Length and/or cap the body in middleware.
  • Windows 413 path: the partial file is unlinked while still open inside with open(...), which raises PermissionError on Windows and leaves the file behind. Close first, then unlink, on any exception.
  • Headers on error responses: the 400/403 responses from the new middlewares carry no CSP/X-Frame-Options; register the headers middleware outermost.
  • testserver in the production allowlist, and a second LOOPBACK_HOSTS that differs from runtime/api.py's. Keep one shared set and pass base_url/allowed_hosts in tests.
  • XSS corpus only calls render_report_html(). Security review of the local dashboard: CSRF on state-changing routes, DNS rebinding, and an XSS regression suite #118 asks for the finding detail pages too; please render the actual templates that show agent output.
  • SECURITY.md doesn't match the code: it says origins must match host and port (port is ignored), that the CSP allows 'unsafe-inline' and CDNs (it doesn't), and a 50 MB upload limit (docext.MAX_UPLOAD_BYTES is 25 MB). It should also state exactly what --allow-insecure-runtime disables.

Happy to review the next version.

@vsn411 vsn411 closed this Nov 17, 2026
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.

Security review of the local dashboard: CSRF on state-changing routes, DNS rebinding, and an XSS regression suite

3 participants