-
-
Notifications
You must be signed in to change notification settings - Fork 14
fix: exclude CSRF verification from broadcast auth routes #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: exclude CSRF verification from broadcast auth routes #294
Conversation
|
Hi @albertcht. I suggest taking a look at #295 first. If that gets merged, then we can remove the middleware from the broadcast routes using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a TokenMismatchException that occurs when broadcasting authentication routes are accessed by Pusher/Soketi JavaScript clients. The clients cannot include CSRF tokens in their authentication requests, so these routes must be explicitly excluded from CSRF verification.
Key Changes:
- Exclude
broadcasting/authandbroadcasting/user-authroutes from CSRF verification usingVerifyCsrfToken::except() - Add comprehensive tests to verify the CSRF exclusion behavior
- Add cleanup in test tearDown to flush static CSRF state
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/broadcasting/src/BroadcastManager.php | Added CSRF exclusions for broadcasting authentication routes in both routes() and userRoutes() methods with explanatory comments |
| tests/Broadcasting/BroadcastManagerTest.php | Added two new test cases to verify CSRF exclusions work correctly, plus static state cleanup in tearDown |
After thoroughly reviewing the code changes, implementation, and tests, I found no issues with this pull request. The implementation is well-designed, properly tested, and correctly handles the static state management of the CSRF exclusion list. The solution appropriately addresses the problem described in the PR description.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Hi @binaryfire, I think PR #295 looks promising and it will be merged to the main branch soon. (there's one comment left). Then you can apply it via |
…ware Use route-level without_middleware option instead of VerifyCsrfToken::except() to exclude CSRF verification from broadcasting authentication routes. This approach is cleaner as the exclusion is declared alongside the middleware assignment.
|
@albertcht Updated. It's now using |
Problem
Broadcasting authentication routes (
/broadcasting/authand/broadcasting/user-auth) fail withTokenMismatchExceptionwhenVerifyCsrfTokenmiddleware is included in the web middleware group.These routes receive POST requests from Pusher/Soketi JavaScript clients during private/presence channel authentication. The JavaScript client has no awareness of Hypervel's CSRF protection system and cannot include CSRF tokens in its requests.
Solution
Exclude broadcasting routes from CSRF verification by calling
VerifyCsrfToken::except()when routes are registered.Laravel's implementation:
Laravel uses
->withoutMiddleware(VerifyCsrfToken::class)on both routes. However, this can't be ported to Hypervel because thewithout_middlewareroute option operates at a different stage in the middleware resolution process:Since Hypervel's route-level exclusion can't remove middleware from inside groups, we use
VerifyCsrfToken::except()which adds paths to the middleware's internal exclusion list.Why This Matters
For CSRF protection to work properly,
VerifyCsrfTokenmust be in the web middleware group. However, this causes broadcasting authentication to fail:The fix ensures broadcasting routes can continue to use the web middleware stack (for session/auth) while being excluded from CSRF verification.
Changes