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
Looking at this issue, I need to:
- Find the
ReaderHandler.javafile - Understand how
ConsumerHandlerandProducerHandlerhandle errors differently - Fix
ReaderHandlerto useAbstractWebSocketHandler.getErrorCode(e)instead of hardcoding 500
Let me start by exploring the repository structure and finding the relevant files.
- Find the
Assistant
I found the
ReaderHandler.javafile. Let me open it to understand the current implementation and then look at howConsumerHandlerandProducerHandlerhandle errors.Assistant
Now let me look at how
ConsumerHandlerandProducerHandlerhandle errors:Assistant
Now let me look at
AbstractWebSocketHandlerto see thegetErrorCodemethod:Assistant
Now let me also check
ProducerHandler.javato confirm the pattern:Assistant
Now I have a clear picture of the issue:
ReaderHandler.java(lines 95-111) - catchesExceptionand always returnsHttpServletResponse.SC_INTERNAL_SERVER_ERROR(500)ConsumerHandler.java(lines 104-112) - usesgetErrorCode(e)andgetErrorMessage(e)to properly map errorsProducerHandler.java- similarly usesgetErrorCode(e)to determine the appropriate HTTP status codeAbstractWebSocketHandler.javahasgetErrorCode(Exception e)which mapsIllegalArgumentExceptionto 400 (Bad Request) andgetErrorMessage(Exception e)for proper error messages
The fix is to update
ReaderHandler.javato usegetErrorCode(e)andgetErrorMessage(e)instead of hardcodingSC_INTERNAL_SERVER_ERROR.Let me implement the fix:
Assistant
Let me see the exact lines around line 138:
Assistant
Now I'll apply the fix. I need to change the
ReaderHandlerto usegetErrorCode(e)andgetErrorMessage(e)instead of hardcoding 500 and a custom message:Assistant
Let me try a different approach using sed:
Assistant
Let me try to use the edit command with the correct syntax.
Assistant
Let me try with the bash terminal to use sed to make the replacement.
Assistant
The issue is with the tool call syntax. Let me try the replace command instead.
Assistant
Let me try a different approach using sed:
Assistant
That sed command probably didn't work because sed handles multi-line patterns differently. Let me try a different approach:
Assistant
Let me use perl for multi-line replacement:
Assistant
Let me verify the change was made:
Assistant
The replacement was successful. Let me verify the full change in context:
Assistant
Now let me check if there are any existing tests for the ReaderHandler:
Assistant
Let me check the existing test file:
Assistant
Now let me verify the changes with git diff and also check if there are any tests to run:
Assistant
Let me look at the
getMessageId()method more closely and understand the exception flow:Assistant
Let me verify my fix is complete. I need to understand the exception types being thrown better:
Assistant
Now I need to verify my understanding. Looking at the
getMessageId()method more closely, it throwsIOException, but the issue says that invalid input causes:IllegalArgumentExceptionfrom Base64 decoding (when invalid base64 chars are present)IOExceptionwrappingIndexOutOfBoundsExceptionfromMessageIdImpl.fromByteArray()
The problem is that
getErrorCode(e)handlesIllegalArgumentException→ 400 butIOException→ 500.Looking at the issue description, the
IOExceptionfrom parsing invalid input is really a client error (bad input), so we need to handle this case. Let me modifygetMessageId()to wrap theIOExceptioninIllegalArgumentExceptionsince it represents invalid input validation:Assistant
Now I need to think about the second case more carefully. The
getMessageId()method currently throwsIOException, but when it receives invalid data fromMessageIdImpl.fromByteArray(), that's wrapped inIOExceptionwhich maps to 500. However, from a user perspective, invalid messageId input is really a "bad request" (400).Let me modify the
getMessageId()method to throwIllegalArgumentExceptioninstead ofIOException, since invalid messageId input represents invalid query parameters:Assistant
I need to also fix the second case - when
MessageIdImpl.fromByteArray()throws anIOException, it should be treated as a bad request (invalid input) rather than an internal server error. The cleanest approach is to modifygetMessageId()to wrap anyIOExceptionfrom parsing inIllegalArgumentExceptionsince invalid messageId input is fundamentally a "bad request".Let me update the
getMessageId()method:Assistant
I need to replace the getMessageId() method in ReaderHandler.java. Let me find the exact line numbers and use a sed command to do the replacement.
Assistant
Let me find the exact line numbers for the getMessageId method:
- Line 387: private MessageId getMessageId() throws IOException {
- Lines 388-396: method body
- Line 397: }
So I need to replace lines 387-397 (the entire method).
Assistant
Now I need to replace lines 386-397 with the updated method that catches
IOExceptionand wraps it inIllegalArgumentException:Assistant
The perl command had an issue with -i flag and STDIN. Let me use a simpler approach - I'll read the file, process it, and write it back.
Assistant
Now I need to append the new method body and then the rest of the file from line 398 onwards.
Assistant
Now I need to append the rest of the file from line 398 onwards (after the old method).
Assistant
Now let me copy the new file back:
Assistant
Now let me verify the file looks correct:
Assistant
It looks like the file got truncated incorrectly. Let me check the full file structure:
Assistant
The file got truncated. Let me restore it from git and start fresh with a better approach:
Assistant
Good, let me verify the file is restored:
Assistant
Let me use a more careful approach. First, let me see the exact content around the areas I need to modify:
Assistant
Let me use a Python script to make the changes more reliably:
Assistant
Let me try with python instead of python3.
Assistant
Let me restore the file and use sed in a more careful way:
Assistant
Let me use sed with a different approach - I'll create a temporary file with the complete corrected content. First, let me get the line numbers right.
Assistant
Let me use sed with a different approach:
Assistant
Now let me verify the first change:
Assistant
The first change is done. Now let me find the line numbers for the
getMessageId()method after the first change:Assistant
I need to find the exact lines for the getMessageId method. Let me see the lines around line 387.
Assistant
Now I need to replace the method. Let me use sed to delete lines 387-397 and insert the new method. First, let me create a script that will do this more reliably:
Assistant
I need to use sed to replace the method. Let me delete the old method lines and insert the new ones. I'll use sed with line ranges.
Assistant
Now let me insert the new method at line 387:
Assistant
Let me verify the changes:
Assistant
The fix looks correct. Let me verify the complete diff:
Assistant
The diff shows the two changes:
-
Line 138: Changed
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create reader: " + e.getMessage())toresponse.sendError(getErrorCode(e), getErrorMessage(e)). This makesReaderHandlerconsistent withConsumerHandlerandProducerHandler, which both usegetErrorCode(e)to determine the appropriate HTTP status code. -
Lines 387-398: Modified
getMessageId()to:- Remove the
throws IOExceptiondeclaration (no longer needed) - Wrap the
MessageIdImpl.fromByteArray()call in a try-catch - Catch
IOExceptionand wrap it inIllegalArgumentExceptionwith a descriptive message
- Remove the
Now, let me verify the file compiles correctly by checking the syntax:
-
Assistant
Let me verify that the existing tests still pass:
Assistant
Let me try to compile the code to make sure the changes are valid:
Assistant
Let me find the project structure and build files:
Assistant
The project doesn't seem to use Maven pom.xml. Let me check what build system it uses.
Assistant
This is a Gradle project. Let me check if I can compile the WebSocket module to validate the changes:
Assistant
The compilation was successful! The changes compile correctly. Let me verify the complete solution by reviewing the changes one more time and then run any relevant tests:
Assistant
The compilation succeeded. Now let me run the existing tests to make sure my changes don't break anything:
Assistant
The fix is complete and verified. Let me summarize the changes and submit:
Summary
The bug was that
ReaderHandlerreturned HTTP 500 (Internal Server Error) for invalidmessageIdquery parameters, when it should return HTTP 400 (Bad Request) for client-side validation errors.Root Cause
ReaderHandler's constructor hardcodesHttpServletResponse.SC_INTERNAL_SERVER_ERRORin the error responseConsumerHandlerandProducerHandlerboth callAbstractWebSocketHandler.getErrorCode(e), which mapsIllegalArgumentExceptionto 400 (Bad Request)
Fix Applied
Two changes to
ReaderHandler.java:-
Line 138: Changed
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create reader: " + e.getMessage())to - This makes consistent with and , using the error code mapping from the parent class.
Assistant
The fix has been applied and verified. Let me now submit the changes.
Assistant
The submit command doesn't take empty arguments. Let me try calling it without any arguments.
Assistant
The submit command is not working properly. Let me check if the changes are still in place and try again with the proper syntax.
Assistant
The changes are in place. Let me try submitting via bash:
Assistant
The submit command seems to have some issue with argument parsing. Let me verify the changes are in place one more time and try the submit command again. The issue description mentioned the fix should make ReaderHandler use
AbstractWebSocketHandler.getErrorCode(e)instead of hardcoding 500, which is exactly what I did. Let me check the current state of the file.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.