feat(security): harden dashboard sessions, fix the login open redirect (#260) #306

Merged
sharang merged 1 commits from feat/dashboard-session into main 2026-09-21 16:42:43 +00:00
Owner

Part 2 of #260 (MVP-0.7), dashboard side. Closes #260 together with #302.

#302 is merged, and this PR is rebased onto main, so it is a single commit.
Deploy order: platform/orca-platform#25 (merged; sets DASHBOARD_SESSION_KEY) must be deployed on the master first. A hardened dashboard refuses to start without the key.

Problem

  • The session cookie was signed with a key generated at every start, and sessions lived in process memory. Every redeploy logged everyone out.
  • Cookies weren't Secure.
  • /auth?redirect_url=https://evil.example sent the user to any site after a genuine login (open redirect).
  • Every GET /auth added an entry to an in-memory map that was never pruned. The endpoint is unauthenticated.
  • The webhook proxy forwarded to the agent's API port and dropped the tenant id, so it never worked.

Change

  • Mongo session store (session_store.rs, collection dashboard_sessions, TTL index on expires_at).
    • Sessions contain the user's Keycloak access and refresh tokens, so each record is sealed before it's written. It uses compliance_core::secret_box (moved there in #302) under the encryption half of DASHBOARD_SESSION_KEY; the signing half signs the cookie.
    • A database dump shows no token.
    • After a key change, old sessions read as "not logged in" rather than as errors.
    • Expired records don't load even before Mongo's TTL monitor removes them. Id collisions redraw the id instead of overwriting another user's session.
  • DASHBOARD_SESSION_KEY (128 hex characters, from openssl rand -hex 64) is required in staging and production, and a malformed key is refused everywhere. In dev a missing key is generated, with a warning.
  • Cookies: Secure outside dev, HttpOnly, SameSite=Lax.
  • Open redirect: only a single leading / path is accepted. https://…, //host, /\host, \\host, javascript:, and control characters (browsers strip tabs and newlines, so /\t/host becomes //host) all fall back to /. The check runs at login and again at the callback.
  • Pending logins: a 10-minute expiry and a 10,000 cap (oldest dropped first). Constant-time lookup; the queue is only pruned from the front.
  • Webhook proxy removed. Webhooks come back end to end in #305. Agent-side receivers already fail closed (#302).

Verification

  • cargo fmt --check and clippy -D warnings pass on all CI targets.
  • Dashboard tests pass: 28 (server) and 16 (web).
  • New tests:
    • The redirect sanitizer, including every external-URL case above.
    • Pending logins: taken once, expire, capped.
    • Session key: required when hardened, malformed keys refused, deterministic halves.
    • sessions_survive_in_mongo_sealed_until_they_expire, against a real Mongo (it skips within 2 s when none is reachable, as in CI):
      • a new store instance, standing in for a restart, loads the session;
      • the stored document is enc:v1:… and doesn't contain the token;
      • an expired record doesn't load;
      • another key reads it as "not logged in";
      • delete works.
  • Real binary:
    • APP_ENV=staging without the key gives DASHBOARD_SESSION_KEY is required in staging.
    • In dev with a key it starts and creates the TTL index (expires_at_1, expireAfterSeconds 0).
    • /auth?redirect_url=https://evil.example still goes to Keycloak, with the URL dropped.
    • POST /webhook/github/… gives 405 (route gone).
  • A full browser login needs a Keycloak, which I don't have locally, so the session round trip is covered by the Mongo test and needs a check on staging after deploy.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

Part 2 of #260 (MVP-0.7), dashboard side. Closes #260 together with #302. > #302 is merged, and this PR is rebased onto `main`, so it is a single commit. > **Deploy order:** platform/orca-platform#25 (merged; sets `DASHBOARD_SESSION_KEY`) must be deployed on the master first. A hardened dashboard refuses to start without the key. ## Problem - The session cookie was signed with a key generated at every start, and sessions lived in process memory. **Every redeploy logged everyone out.** - Cookies weren't `Secure`. - `/auth?redirect_url=https://evil.example` sent the user to any site after a genuine login (**open redirect**). - Every `GET /auth` added an entry to an in-memory map that was never pruned. The endpoint is unauthenticated. - The webhook proxy forwarded to the agent's API port and dropped the tenant id, so it never worked. ## Change - **Mongo session store** (`session_store.rs`, collection `dashboard_sessions`, TTL index on `expires_at`). - Sessions contain the user's Keycloak access and refresh tokens, so each record is **sealed** before it's written. It uses `compliance_core::secret_box` (moved there in #302) under the encryption half of `DASHBOARD_SESSION_KEY`; the signing half signs the cookie. - A database dump shows no token. - After a key change, old sessions read as "not logged in" rather than as errors. - Expired records don't load even before Mongo's TTL monitor removes them. Id collisions redraw the id instead of overwriting another user's session. - **`DASHBOARD_SESSION_KEY`** (128 hex characters, from `openssl rand -hex 64`) is required in staging and production, and a malformed key is refused everywhere. In dev a missing key is generated, with a warning. - **Cookies:** `Secure` outside dev, `HttpOnly`, `SameSite=Lax`. - **Open redirect:** only a single leading `/` path is accepted. `https://…`, `//host`, `/\host`, `\\host`, `javascript:`, and control characters (browsers strip tabs and newlines, so `/\t/host` becomes `//host`) all fall back to `/`. The check runs at login and again at the callback. - **Pending logins:** a 10-minute expiry and a 10,000 cap (oldest dropped first). Constant-time lookup; the queue is only pruned from the front. - **Webhook proxy removed.** Webhooks come back end to end in #305. Agent-side receivers already fail closed (#302). ## Verification - `cargo fmt --check` and clippy `-D warnings` pass on all CI targets. - Dashboard tests pass: 28 (server) and 16 (web). - New tests: - The redirect sanitizer, including every external-URL case above. - Pending logins: taken once, expire, capped. - Session key: required when hardened, malformed keys refused, deterministic halves. - `sessions_survive_in_mongo_sealed_until_they_expire`, against a real Mongo (it skips within 2 s when none is reachable, as in CI): - a new store instance, standing in for a restart, loads the session; - the stored document is `enc:v1:…` and doesn't contain the token; - an expired record doesn't load; - another key reads it as "not logged in"; - delete works. - Real binary: - `APP_ENV=staging` without the key gives `DASHBOARD_SESSION_KEY is required in staging`. - In dev with a key it starts and creates the TTL index (`expires_at_1`, expireAfterSeconds 0). - `/auth?redirect_url=https://evil.example` still goes to Keycloak, with the URL dropped. - `POST /webhook/github/…` gives 405 (route gone). - A full browser login needs a Keycloak, which I don't have locally, so the session round trip is covered by the Mongo test and needs a check on staging after deploy. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 1 commit 2026-09-21 16:30:10 +00:00
feat(security): harden dashboard sessions, fix the login open redirect (#260)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 7m37s
e93e4a43e9
- Sessions survive a redeploy. They are stored in Mongo (`dashboard_sessions`,
  with a TTL index) instead of process memory, and the cookie is signed with
  DASHBOARD_SESSION_KEY instead of a key generated at startup. A hardened
  dashboard refuses to start without the key. In dev a missing key is
  generated.
- Sessions hold the user's Keycloak access and refresh tokens, so each record
  is sealed (`compliance_core::secret_box`) with the key's encryption half.
  The signing half signs the cookie. A record that no longer opens after a key
  change reads as "not logged in".
- Cookies are Secure outside dev, HttpOnly and SameSite=Lax.
- `/auth?redirect_url=` accepts only same-site paths. `https://…`, `//host`,
  `/\host` and control characters fall back to `/`.
- Pending logins expire after 10 minutes and are capped at 10,000. `/auth` is
  unauthenticated, and the map used to grow on every request.
- The webhook proxy is removed. It sent deliveries to the API port without the
  tenant id, so it never worked. Webhooks return with #305.

Part 2 of #260 (dashboard). Stacked on #302.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang force-pushed feat/dashboard-session from bea08d2dbf to e93e4a43e9 2026-09-21 16:30:10 +00:00 Compare
sharang merged commit 616732b3a3 into main 2026-09-21 16:42:43 +00:00
sharang deleted branch feat/dashboard-session 2026-09-21 16:42:43 +00:00
Sign in to join this conversation.