Repository navigation
fix(server): require a bearer token on /api and /v1, close share traversal - #479
Merged
Merged
Conversation
…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]>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
whycodes servebound to loopback but had no authentication andCorsLayer::permissive(). Loopback is not a boundary — any web page the user opens can reach127.0.0.1:POST /api/session/newthen/api/session/{id}/chatwith "runcurl … | sh";/apichat 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.jsonjoined 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
servemakes a random 64-hex token per start (two v4 UUIDs) or takesWHYCODES_SERVE_TOKEN. It lives inAppState::auth_tokenand in.whycodes/serve.lock(now written0600on Unix)./apiand/v1route requiresAuthorization: Bearer <token>(constant-time compare; an empty token refuses everything) →401otherwise. The CORS layer is removed (clients are the TUI and SDKs, never a page);tower-httpdrops out./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.whycodes connect: token fromWHYCODES_SERVE_TOKEN, else the project'sserve.lockwhen its port matches.connect_with_token;connectreads the env var;launchhands the child a fresh token.connect(base, token = env);launchhands the child a fresh token.Breaking for external clients of a running daemon: they now need the token (in the lock file, or fix it with the env var).
launchusers are unaffected.Tests
/api/health,/api/shares,/v1/health,/v1/sessions→ 401 with no header, a wrong/short token, or noBearerprefix; 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.connect_tokenenv > lock (same port only).connect_with_tokensends the bearer, blank sends none (mock server that 401s otherwise).set_tokenadds/removes the header on the wire.cli/tests/sdk_v1.rsagainst a real spawned daemon: token accepted, wrong token refused;launchend-to-end with its generated token.cargo deny,check_sdk_protocol.py, TS build + tests.🤖 Generated with Claude Code