fix(web): let the browser extension WS handshake past the Origin gate - #151
Merged
Conversation
PR #141 hardened corsMiddleware to 403 any untrusted Origin before it reaches a handler. But a Chrome extension always sends 'Origin: chrome-extension://<id>' on the WebSocket handshake, which is never same-origin, so /api/browser/ext/ws was being rejected before the bridge's own pairing-token auth could run — the extension could no longer connect. Exempt the extension WS endpoint (GET only, exact path) from the Origin gate, mirroring the existing isAuthExempt carve-out. The bridge still authenticates via its pairing token (verified: an evil-origin socket that completes the upgrade is rejected with 'authentication required'), so the drive-by-POST vector PR #141 closed stays closed. Adds TestCORSMiddlewareExtensionWSHandshake to pin the regression.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe CORS middleware now exempts only GET requests to the browser extension WebSocket handshake endpoint from origin rejection. Tests cover extension and untrusted origins on the WebSocket path, plus blocked methods and non-WebSocket endpoints. ChangesBrowser extension WebSocket CORS exception
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
The browser plugin (jcode Browser Bridge extension) could no longer connect.
PR #141 (commit
f71f5a8) hardenedcorsMiddlewareto return 403 for any untrustedOriginbefore the request reaches a handler — closing a drive-by-POST vector. But a Chrome extension always sendsOrigin: chrome-extension://<id>on its WebSocket handshake, which is never same-origin. As a resultGET /api/browser/ext/wswas 403'd at the Origin gate before the bridge's own pairing-token auth could run, so the extension could never connect.Fix
Exempt the extension WS endpoint (GET only, exact path) from the Origin gate, mirroring the existing
isAuthExemptcarve-out for the same route. AddedisBrowserExtensionWS()and a comment explaining the trade-off.Security is unchanged: the bridge still authenticates via its pairing token (
bridge.go:83). The exemption is scoped to a GET upgrade endpoint that performs no cross-origin simple-request side effects, so the drive-by-POST vector PR #141 closed stays closed.Verification (live server, real WS handshakes)
Started a local
jcode webserver and drove real handshakes from both the plugin's and an attacker's perspective:/api/config)authentication required✅The last two rows confirm the pairing token remains the real authorization boundary — an untrusted origin gains nothing from the exemption.
Tests
TestCORSMiddlewareExtensionWSHandshake(5 subcases, pins the regression)go test ./internal/web/ ./internal/browser/greengo vet+golangci-lint0 issues; full pre-push suite greenSummary by CodeRabbit