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():WARN 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]Case 2 —
IOExceptionwrapping from : Assistant
I'll start by exploring the repository structure to understand the codebase and find the relevant files mentioned in the issue.
Assistant
The open command doesn't take a file parameter. Let me use the correct syntax.
Assistant
Now let me check the AbstractWebSocketHandler, ConsumerHandler, and ProducerHandler to understand how they handle errors:
Assistant
Now let me check how ConsumerHandler and ProducerHandler handle errors:
Assistant
Now I understand the issue. The problem is in
ReaderHandler.javaline 132 where it uses:response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ...);But
ConsumerHandlerandProducerHandleruse:response.sendError(getErrorCode(e), getErrorMessage(e));Let me look at the exact line numbers in ReaderHandler.java to understand what needs to be changed:
Assistant
Now let me see the context around that line:
Assistant
Now I have a clear understanding of the issue. Let me create a test script to verify the bug exists before fixing it. First, let me explore the test structure:
Assistant
Now I understand the codebase structure. Let me write a simple test to verify the bug and then fix it. Let me first create a test script to reproduce the issue:
Assistant
Now let me check how the error code logic works in
AbstractWebSocketHandler.getErrorCode(). The issue description mentions that:IllegalArgumentExceptionshould be mapped toSC_BAD_REQUEST(400)- The
ReaderHandlershould usegetErrorCode(e)instead of hardcodingSC_INTERNAL_SERVER_ERROR
Looking at the code:
Current ReaderHandler behavior (line 138):
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create reader: " + e.getMessage());Expected behavior (like ConsumerHandler and ProducerHandler):
response.sendError(getErrorCode(e), getErrorMessage(e));The
getErrorCode(e)method inAbstractWebSocketHandlerreturnsSC_BAD_REQUESTforIllegalArgumentException.However, there's also
IOExceptionwithIndexOutOfBoundsExceptioncause. Looking at the issue:- Case 1:
IllegalArgumentException→ should return 400 - Case 2: wrapping → this would return 500 since is not specifically handled
Assistant
Now I understand the complete picture. The fix is straightforward:
- Change
ReaderHandlerto usegetErrorCode(e)andgetErrorMessage(e)instead of hardcodingHttpServletResponse.SC_INTERNAL_SERVER_ERRORand the error message.
For Case 2, the
IOExceptionwrappingIndexOutOfBoundsExceptionwould still return 500, but the issue description suggests that the primary fix is to usegetErrorCode()like the other handlers do. TheIllegalArgumentExceptionfrom invalid base64 would then correctly return 400.However, looking more carefully at the issue, the second case is an
IOExceptionwhich wouldn't be caught bygetErrorCode(). But looking at the stack trace, the root cause is actually anIndexOutOfBoundsExceptionin the byte buffer, which is still a user input error (bad messageId format).Let me check if
getErrorCodeshould also handleIOException:- Change
Assistant
Let me look at the
getMessageId()method more carefully to understand what exceptions it can throw:Assistant
Looking at the issue description more carefully:
- Case 1:
IllegalArgumentExceptionfromBase64.getDecoder().decode()- This should be caught bygetErrorCode()and return 400 - Case 2:
IOExceptionwrappingIndexOutOfBoundsExceptionfromMessageIdImpl.fromByteArray()- This is still an invalid input error
The second case is tricky because
IOExceptionis a checked exception thrown byfromByteArray(). However, the root cause is user error (bad input), so it should also return 400.Looking at the issue description again, it says:
ConsumerHandlerandProducerHandlerboth callAbstractWebSocketHandler.getErrorCode(e)instead, which mapsIllegalArgumentExceptiontoSC_BAD_REQUEST(400).So the fix is to use
getErrorCode(e)like the other handlers do. This will:- Fix Case 1 (IllegalArgumentException → 400)
- Case 2 (IOException) would still return 500, but looking at the error message, it says the IOException is wrapping an IndexOutOfBoundsException from bad user input.
Let me check if we need to also handle IOException. Let me look at what the exception hierarchy looks like:
- Case 1:
Assistant
The
IndexOutOfBoundsExceptionis not handled ingetErrorCode(). However, based on the issue description, the main fix is to usegetErrorCode(e)instead of hardcodingSC_INTERNAL_SERVER_ERROR. This is a clear fix that makesReaderHandlerconsistent withConsumerHandlerandProducerHandler.For the
IOExceptioncase (Case 2), the issue description shows it's wrapping anIndexOutOfBoundsException. We should also check if we need to handleIOExceptionor unwrap the cause. Let me look more carefully:Assistant
Let me think about this more carefully. The issue description states:
- Case 1 (
IllegalArgumentException): Should return 400 - usinggetErrorCode(e)will fix this - Case 2 (
IOExceptionwrappingIndexOutOfBoundsException): Should also return 400
For Case 2, we need to check if
getErrorCodeshould also handleIOExceptionthat wraps bad input exceptions. Looking at the method signature,getMessageId()throwsIOException, andMessageIdImpl.fromByteArray()throwsIOException.The simplest fix that aligns with ConsumerHandler and ProducerHandler is to:
- Use
getErrorCode(e)instead of hardcodedSC_INTERNAL_SERVER_ERROR - Use
getErrorMessage(e)instead of hardcoded message
For the IOException case, we may need to also enhance
getErrorCode()to check the cause of IOException. Let me check if there's already logic for this:- Case 1 (
Assistant
Now I need to decide on the complete fix. Based on the issue description:
-
The primary issue is that
ReaderHandlerusesHttpServletResponse.SC_INTERNAL_SERVER_ERRORhardcoded instead of callinggetErrorCode(e). -
For Case 1 (
IllegalArgumentException), usinggetErrorCode(e)will correctly return 400. -
For Case 2 (
IOExceptionwrappingIndexOutOfBoundsException), we need to either:- a) Add
IOExceptionhandling togetErrorCode()that checks the cause - b) Or just use
getErrorCode(e)and accept that IOException returns 500
- a) Add
Looking at the issue more carefully, the user says both cases should return 400. Let me add handling for
IOExceptionwith bad input causes ingetErrorCode().Let me implement the fix:
-
Assistant
Now let me make the fix. First, fix
ReaderHandler.javato usegetErrorCode(e)andgetErrorMessage(e):Assistant
Now I need to also fix the error message to use
getErrorMessage(e):Assistant
Now I need to also handle the IOException case (Case 2). The issue says that an
IOExceptionwrappingIndexOutOfBoundsExceptionshould also return 400. Let me updategetErrorCode()to handle this:Assistant
Let me see the full
getErrorCodemethod:Assistant
I need to modify the
getErrorCodemethod to also check forIOExceptionwith bad input causes. Let me add this check. I'll add it right after checking forIllegalArgumentException:Assistant
Long
Assistant
I need to use the proper tool call format. Let me continue with the fix using sed commands instead.
Assistant
Perl is available. Let me use perl to make the changes to the AbstractWebSocketHandler.java file. I need to:
- Add IOException handling in getErrorCode method
- Add IOException handling in getErrorMessage method
- Add a helper method isBadFormatException
Let me create a perl script to do this.
Assistant
Now let me verify the changes were applied correctly:
Assistant
The changes to AbstractWebSocketHandler.java look good. Now let me verify the changes to ReaderHandler.java:
Assistant
The changes look good. Now let me verify the complete diff of the changes:
Assistant
The changes look good. Now let me verify the code compiles and run the existing tests. First, let me check the build system:
Assistant
The user hasn't said anything yet. I need to continue my work. Let me check if Maven is available and try to compile and run the tests to verify my changes work correctly.
Assistant
The project uses Gradle. Let me try to compile and run the tests for the websocket module to verify my changes.
Assistant
The compilation was successful. Now let me run the existing tests for the ReaderHandler to make sure I haven't broken anything.
Assistant
The tests passed successfully. Now I should also run tests for AbstractWebSocketHandler if there are any, to make sure my changes to getErrorCode and getErrorMessage didn't break anything.
Assistant
Good, I can see there are existing tests. Let me run them to make sure my changes don't break anything:
Assistant
The tests passed. Now let me create a test script to verify that my fix actually works for both cases mentioned in the issue. I'll create a simple test that verifies:
- Case 1: IllegalArgumentException returns 400
- Case 2: IOException with IndexOutOfBoundsException cause returns 400
Let me create a temporary test file to validate my fix.
Assistant
Let me simplify the test to just use the actual constants instead:
Assistant
The test confirms the logic is working correctly. Now let me run a broader set of tests for the websocket module to ensure my changes don't break anything else.
Assistant
Let me check which test failed to see if it's related to my changes.
Assistant
Let me check if this was a pre-existing failure by looking at which test failed:
Assistant
The failing test is
PingPongSupportTest. Let me check if this test was passing before my changes by looking at what the test does and if it's related to my changes:Assistant
Let me check if this test was already failing before my changes by reverting temporarily and running the test:
Assistant
The test was already failing before my changes. Let me restore my changes and verify that my changes don't introduce any new failures.
Assistant
Good, my changes are restored. The PingPongSupportTest failure is a pre-existing issue, not caused by my changes. Let me run the tests that are most relevant to my changes - specifically the ReaderHandler tests and AbstractWebSocketHandler tests:
Assistant
The tests pass. Let me also run the ProducerHandler and ConsumerHandler tests to make sure my changes to getErrorCode and getErrorMessage in AbstractWebSocketHandler didn't break anything:
Assistant
Let me now review my changes one more time to make sure everything is correct and complete.
Assistant
The changes look good. Let me remove the backup files and verify my fix one more time:
Assistant
Now let me verify my changes are correct by reviewing the modified files one more time:
Assistant
I made a mistake with an invalid JSON response. Let me continue with a proper tool call to verify my changes.
Assistant
Now let me run a final verification of the relevant tests to make sure everything is working:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.