-
Notifications
You must be signed in to change notification settings - Fork 130
[1.x] Add Message Payload Validation and Improve Channel Data Handling #303
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
[1.x] Add Message Payload Validation and Improve Channel Data Handling #303
Conversation
Adds type checking for channel_data to prevent JSON decode errors when the data is not a string format, improving the robustness of message handling in the CLI logger.
Add comprehensive validation for incoming WebSocket messages using Laravel Validator to: - Ensure correct message structure and data types - Prevent malformed data from causing server errors - Validate required fields (event, data.channel) - Enforce JSON format for channel_data - Improve overall server stability and security This change helps prevent server crashes and unexpected behavior caused by invalid message formats.
Add comprehensive test coverage for message payload validation including: - Event type validation - Data structure validation - Channel format validation - Auth data type checking - Channel data format verification Each test case ensures proper error responses for invalid message formats, maintaining consistency with Pusher protocol specifications.
Thanks @MahdiBagheri71 - I think this looks pretty sensible, though looks like it introduces some failing tests. Have your changes caused a regression elsewhere? |
I've carried out some testing on this pull request and the validation doesn't account for evetns such as client events and control messages which have a completely different format. We may have to move the validation to each event handler, but I want to think on this to see if there is a better solution. |
Thanks for reviewing it @joedixon I tried to cover all the possible cases in the functions I wrote and considered the input typescript of all the functions in my validation to avoid any errors and ensure the socket doesn’t go out of access. |
@MahdiBagheri71 sounds good, but there are some breaking changes so leave this with me for a bit and I'll see about addressing them. |
@MahdiBagheri71 can you give these updates a try with the tests you have configured in your video? |
I tested everything based on the new changes, and the issues I had were completely resolved. |
Prerequisites
Description
This pull request introduces improved validation for WebSocket message payloads and enhances the handling of
channel_data
. These changes ensure better stability, prevent server crashes, and align with the latest Laravel best practices.Key Changes
channel_data
JSON parsing to avoid crashes.Current Behavior
Currently, the server accepts messages without proper validation. This can lead to:
channel_data
Example of Problematic Payload:
New Behavior
With this PR:
channel_data
is safely parsed and handled.Validation Rules:
Improved
channel_data
Handling:Test Coverage
Comprehensive tests have been added in
tests/Unit/Protocols/Pusher/ServerTest.php
to cover:channel_data
Example Test Case:
Benefits
Breaking Changes
None. Invalid messages that previously caused crashes will now receive proper error responses instead.
Related Issues
Fixes #298