Skip to content

fix(server): require a bearer token on /api and /v1, close share traversal - #479

Merged
mrnasil merged 1 commit into
mainfrom
fix/server-auth-token
Nov 25, 2026
Merged

mrnasil merged 1 commit into
mainfrom
fix/server-auth-token

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Problem

whycodes serve bound to loopback but had no authentication and CorsLayer::permissive(). Loopback is not a boundary — any web page the user opens can reach 127.0.0.1:

  • a page could POST /api/session/new then /api/session/{id}/chat with "run curl … | sh"; /api chat runs tools auto-approved, and the permissive preflight let the page read the SSE stream too (DNS rebinding worked as well, no Host check);
  • GET /s/..%2Fauth.json joined a percent-decoded id onto the share dir and returned the OAuth token store (an absolute id worked on Windows); the 404 page also reflected the id unescaped (XSS on the localhost origin).

Fix

  • Token: serve makes a random 64-hex token per start (two v4 UUIDs) or takes WHYCODES_SERVE_TOKEN. It lives in AppState::auth_token and in .whycodes/serve.lock (now written 0600 on Unix).
  • Middleware: every /api and /v1 route requires Authorization: Bearer <token> (constant-time compare; an empty token refuses everything) → 401 otherwise. The CORS layer is removed (clients are the TUI and SDKs, never a page); tower-http drops out.
  • Shares: /s/<id> stays open for the browser, but the id must be [A-Za-z0-9_-]+ (session ids), and the 404 page HTML-escapes it.
  • Clients:
    • whycodes connect: token from WHYCODES_SERVE_TOKEN, else the project's serve.lock when its port matches.
    • Rust SDK: new connect_with_token; connect reads the env var; launch hands the child a fresh token.
    • TS SDK: connect(base, token = env); launch hands the child a fresh token.
  • Docs: guide (serve section) and TS README.

Breaking for external clients of a running daemon: they now need the token (in the lock file, or fix it with the env var). launch users are unaffected.

Tests

  • server: /api/health, /api/shares, /v1/health, /v1/sessions → 401 with no header, a wrong/short token, or no Bearer prefix; 200 with the token; an evil-origin preflight gets no CORS grant; an empty configured token refuses everything; traversal ids (..%2Fauth.json, absolute, ..\) find nothing even when the target exists; the 404 page escapes <script>; token from env / random.
  • cli: lock carries the token, mode 0600 on Unix; connect_token env > lock (same port only).
  • sdk: connect_with_token sends the bearer, blank sends none (mock server that 401s otherwise).
  • tui: set_token adds/removes the header on the wire.
  • cli/tests/sdk_v1.rs against a real spawned daemon: token accepted, wrong token refused; launch end-to-end with its generated token.
  • server and sdk line coverage 100% locally; fmt, clippy, budgets, cargo deny, check_sdk_protocol.py, TS build + tests.

🤖 Generated with Claude Code

…ersal

The serve daemon had no authentication and a permissive CORS layer.
Loopback is not a boundary: any web page the user opens can reach
127.0.0.1, preflight passed, and `/api/session/{id}/chat` runs tools
auto-approved. `/s/..%2Fauth.json` also read the OAuth token store.

- serve makes a random 64-hex token per start (or takes
  WHYCODES_SERVE_TOKEN), keeps it in AppState and in
  .whycodes/serve.lock (written 0600 on Unix). Every /api and /v1
  route requires `Authorization: Bearer <token>` (constant-time
  compare; an empty token refuses everything). The CORS layer is gone.
- Share pages (/s/<id>) stay open for the browser, but the id must be
  [A-Za-z0-9_-]+ and the 404 page escapes it (reflected XSS).
- whycodes connect sends the token from WHYCODES_SERVE_TOKEN or the
  project's lock for the same port. The Rust SDK gains
  connect_with_token (connect reads the env var); both SDKs' launch
  hand the child a fresh token.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 55e105d into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/server-auth-token branch November 25, 2026 13:03
@mrnasil mrnasil mentioned this pull request Nov 26, 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.

1 participant