fix(hub): allow supervisor owner to invoke /:id/scan (CSRF bypass for legacy-JWT)#66
Closed
finedesignz wants to merge 1 commit into
Closed
fix(hub): allow supervisor owner to invoke /:id/scan (CSRF bypass for legacy-JWT)#66finedesignz wants to merge 1 commit into
finedesignz wants to merge 1 commit into
Conversation
Root cause: csrfGuard rejected every mutating /api/* request that didn't carry a csrf_token cookie + matching X-CSRF-Token header. Users on the legacy-JWT soak path (authMethod=legacy_jwt) never get the csrf_token cookie issued, so POST /api/supervisors/:id/scan (and every other mutating REST call) returned 403 csrf_failed. Fix: skip CSRF enforcement when the request authenticates via Authorization: Bearer and has no session cookie. Bearer-only requests are not CSRF-vulnerable because browsers never auto-attach Authorization headers cross-origin (unlike cookies) — JS must opt-in, which a malicious cross-origin page cannot do without the token. When BOTH a session cookie AND a bearer token are present, CSRF is still enforced (cookie wins — matches authMiddleware precedence). Added 2 csrf.test.ts cases covering both branches; full suite 20/20.
Owner
Author
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.
Summary
POST /api/supervisors/:id/scan(and every other mutating REST call) returned 403csrf_failedfor users on the legacy-JWT auth path. Hub logs confirmed the affected user (233c6d63-…) was authenticating viamethod=legacy_jwt.hub/src/csrf.tscsrfGuard()enforces double-submitcsrf_tokencookie +X-CSRF-Tokenheader on every mutating/api/*request. Legacy-JWT sessions never get thecsrf_tokencookie issued, so the web client (web/src/lib/api.ts) cannot echo it back, and every POST/PUT/PATCH/DELETE fails.Authorization: Bearer …and carries no session cookie. Bearer-only requests are not CSRF-vulnerable — browsers never auto-attachAuthorizationheaders cross-origin (unlike cookies), so a malicious page cannot forge them.authMiddlewarecookie-wins precedence).Why this is the right fix (not a security loosening)
CSRF only matters when the browser auto-attaches credentials. The threat model is: attacker.com loads
<form action="hub/api/supervisors/.../scan" method=POST>, browser auto-attaches the session cookie, request succeeds. With Bearer tokens this is impossible — JS must explicitly read the token and attach the header, and same-origin-policy blocks attacker.com from reading the legitimate site's storage. The standard treatment ofAuthorization: Beareris exactly this bypass.Test plan
bun test hub/test/csrf.test.ts— 20/20 pass (added 2 new cases: bearer-only bypass, bearer+cookie still enforces)/api/supervisors/9a6a325e-.../scanfrom the web UI returns 200, hub logs show nocsrf_failed🤖 Generated with Claude Code