ISSUE DESCRIPTION [Bug] WebSocket ReaderHandler returns 500 for invalid messageId query…
- User
ISSUE DESCRIPTION
[Bug] WebSocket ReaderHandler returns 500 for invalid messageId query parameter
Search before reporting
- I searched in the issues and found nothing similar.
Read release policy
- I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.
User environment
Broker Version 4.0.7 Any broker OS Any broker Java version Any WebSocket client
Issue Description
Minimal reproduction steps
Connect to the WebSocket reader endpoint with an invalid
messageIdquery parameter.Two examples that both trigger the bug:
-
An improperly URL-encoded base64 message ID that includes a
+which the server interprets as a space (e.g.CJCj6QEQ+UQwAA==becomesCJCj6QEQ UQwAA==). -
An arbitrary non-base64 string:
?messageId=invalidMessageId
What did you expect to see?
HTTP 400 Bad Request.
What did you see instead?
HTTP 500 Internal Server Error in both cases.
Case 1 —
IllegalArgumentExceptionfromBase64.getDecoder().decode():codeWARN org.apache.pulsar.websocket.ReaderHandler - [REDACTED] Failed in creating reader on topic persistent://REDACTED/REDACTED/REDACTED java.lang.IllegalArgumentException: Illegal base64 character 20 at java.base/java.util.Base64$Decoder.decode0(Unknown Source) ~[?:?] at java.base/java.util.Base64$Decoder.decode(Unknown Source) ~[?:?] at java.base/java.util.Base64$Decoder.decode(Unknown Source) ~[?:?] at org.apache.pulsar.websocket.ReaderHandler.getMessageId(ReaderHandler.java:348) ~[org.apache.pulsar-pulsar-websocket-4.0.7.jar:4.0.7] at org.apache.pulsar.websocket.ReaderHandler.<init>(ReaderHandler.java:95) ~[org.apache.pulsar-pulsar-websocket-4.0.7.jar:4.0.7] at org.apache.pulsar.websocket.WebSocketReaderServlet.lambda$configure$0(WebSocketReaderServlet.java:44) ~[org.apache.pulsar-pulsar-websocket-4.0.7.jar:4.0.7] Assistant
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me look at the WebSocket module structure.
Assistant
Now let me look at the
AbstractWebSocketHandlerto understand thegetErrorCodemethod used byConsumerHandlerandProducerHandler.Assistant
Let me look at how
ConsumerHandlerandProducerHandlerusegetErrorCode:Assistant
Let me also check the ProducerHandler and look at the existing test file:
Assistant
Let me look at the existing ReaderHandlerTest:
Assistant
Let me look at the
AbstractWebSocketHandlerTest:Assistant
Now I understand the issue. Let me first check the
WebSocketReaderServlet.javato understand the context:Assistant
Let me first reproduce the issue with a simple test. Let me first look at the existing tests and try to understand the structure:
Assistant
Let me first verify the current behavior by running an existing test, then write a test that reproduces the issue:
Assistant
Now I understand the issue. Let me write a reproduction script to verify the current behavior. First, let me understand what
getErrorCodeandgetErrorMessagereturn:Assistant
Now let me look at the relevant section in ReaderHandler.java again to understand what needs to be changed:
Assistant
Now I'll apply the fix. Looking at the code, I need to:
- Replace the hardcoded
HttpServletResponse.SC_INTERNAL_SERVER_ERRORwith a call togetErrorCode(e). - Use
getErrorMessage(e)for the error message.
- Replace the hardcoded
Assistant
Let me try with the heredoc style:
Assistant
Let me verify the change was applied:
Assistant
Let me look at the
MessageIdImpl.fromByteArray()to understand the minimum size for a validMessageIdData:Assistant
Now let me think about this. The fix for the constructor handles case 1 (invalid base64), but for case 2 (valid base64 but invalid bytes for MessageIdData), it would still return 500 because
getErrorCodeonly mapsIllegalArgumentExceptionto 400, notIOException. Let me check what the issue's expected behavior is and look more carefully:Assistant
Let me write a quick test to verify my understanding:
Assistant
Now I understand. The constructor fix alone handles case 1, but case 2 (where the base64 is valid but the bytes don't form a valid MessageIdData) would still return 500 because
getErrorCodedoesn't mapIOExceptionto 400. I need to also updategetMessageId()to throwIllegalArgumentExceptionfor invalid input. Let me look at the currentgetMessageIdmethod:Assistant
Now let me update the
getMessageId()method to validate the input and throwIllegalArgumentExceptionfor invalid input:Assistant
Let me use perl to do the edit:
Assistant
Now let me try to compile and run the existing tests to see if my changes don't break anything:
Assistant
Let me try compiling the code to make sure my changes are syntactically correct:
Assistant
The code compiled successfully. Let me run the existing tests:
Assistant
The existing tests pass. Now let me write a temporary test to verify the fix works for the bug cases:
Assistant
Let me take a different approach and use the existing test infrastructure. Let me look at the existing
ReaderHandlerTestand write a test using the same pattern:Assistant
Let me create a temporary test file in the test directory to verify my fix works:
Assistant
The test passed. Let me check the test report to see all 3 test cases:
Assistant
All 3 test cases pass! The fix correctly returns 400 (Bad Request) for all invalid messageId cases. Now let me also run all the existing websocket tests to make sure I didn't break anything:
Assistant
Let me check which test failed:
Assistant
This failure is unrelated to my changes - it's an integration test (PingPongSupportTest) that requires a running Pulsar broker (404 Not Found). Let me check if the test would have failed even before my changes:
Assistant
The test failure is pre-existing and not related to my changes (it's an integration test requiring a running Pulsar broker). Let me restore my changes:
Assistant
Let me run the unit tests excluding the integration test:
Assistant
All tests pass. Now let me clean up the temporary test file and look at my final changes:
Assistant
Assistant
Let me run the existing tests to ensure I didn't break anything:
Assistant
Let me also run the existing tests one more time to ensure my changes don't break anything:
Assistant
All existing tests pass. Let me view the final diff once more:
Assistant
The changes look good. Let me verify all the changes are in place and run the relevant tests once more:
Assistant
The changes look correct. Let me submit the changes:
Assistant
The changes are minimal and well-targeted. Let me submit the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.