Skip to content

Require a per-launch secret and trusted hosts on the API - #1074

Open
Creylay wants to merge 5 commits into
fix/api-upload-path-hardeningfrom
fix/1070-trusted-host-launch-secret
Open

Creylay wants to merge 5 commits into
fix/api-upload-path-hardeningfrom
fix/1070-trusted-host-launch-secret

Conversation

@Creylay

@Creylay Creylay commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Closes #1070. Builds on #1063 (base branch), which restricted CORS.

CORS only hides responses from other sites. This PR closes the remaining ways a foreign page could reach the local API:

  • Trusted hosts (DNS rebinding): TrustedHostMiddleware refuses requests whose Host is not localhost / 127.0.0.1. New ALLOWED_HOSTS setting for Docker and server deployments.
  • Per-launch secret (simple requests): each launch generates a random secret. The backend hands it to the browser in an HttpOnly, SameSite=Strict cookie when it serves /app/, or through GET /api/v1/auth/launch (used by the Vite dev frontend). Every /api/ request and websocket without it gets 401 / close 1008. A cookie was chosen because <img>, video and audio tags that point at the API cannot send headers.
  • Frontend: axios sends credentials, the app gets the cookie before rendering, and a 401 launch_secret_required after a backend restart renews the cookie and retries once.
  • Docker: README "Security" section (publish on 127.0.0.1, ALLOWED_HOSTS, reverse proxy with authentication, API_TOKEN), comments in both Dockerfiles, and docker-compose.demo.yml reads DASHAI_ALLOWED_HOSTS.

Behavior changes

  • Demo deployment: whoever runs docker-compose.demo.yml must set DASHAI_ALLOWED_HOSTS to the name or IP users reach the server with, otherwise requests get 400 Invalid host header.
  • Scripts calling the API (curl, Python) need API_TOKEN and an Authorization: Bearer header.
  • The desktop app and the dev frontend need no changes.

Known limitation

SameSite treats every localhost port as the same site, so a malicious page served from another port on the same machine would still get the cookie attached. This is far narrower than "any website", which is what this PR closes.

Tests

  • New: test_trusted_host_security.py, test_launch_secret_security.py, test_api_token_setting.py. The rejection tests were confirmed to fail without the middleware.
  • Test clients now fetch the cookie on start, like the frontend does.
  • Backend suite: API 1407 passed. The rest passes except two test_bart_regression cases that fail locally on a pytensor C compile error unrelated to this change.
  • Frontend: lint, type check and build pass. Manually tested the dev frontend, the desktop window and recovery after a backend restart.
  • Smoke-tested against a real server: unknown host 400, missing secret 401, form post without secret 401, /app/ sets the cookie, API_TOKEN bearer accepted.

This branch has not been deployed

No deployments
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