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
Let me understand the issue:
- The bug is in
ReaderHandlerin the Apache Pulsar WebSocket implementation - When an invalid
messageIdquery parameter is provided, it returns HTTP 500 instead of HTTP 400 - The issue is that
ReaderHandler.getMessageId()doesn't validate themessageIdbefore decoding - The constructor catches exceptions but hardcodes
HttpServletResponse.SC_INTERNAL_SERVER_ERRORinstead of usingAbstractWebSocketHandler.getErrorCode(e)likeConsumerHandlerandProducerHandlerdo
Let me start by exploring the repository structure and finding the relevant files.
- The bug is in
Assistant
I made an error in my function call. Let me use the correct syntax for opening a file. I need to use
openwith the proper arguments format.Assistant
Let me try without the file parameter and just use the path directly.
Assistant
Now let me look at how
ConsumerHandlerandProducerHandlerhandle errors to understand the expected pattern:Assistant
Now let me check the
AbstractWebSocketHandlerto see thegetErrorCodemethod:Assistant
Now let me check the
ProducerHandlerto confirm it also usesgetErrorCode:Assistant
Now I understand the issue clearly:
- In
ReaderHandler.java, line ~130, when an exception occurs, it usesHttpServletResponse.SC_INTERNAL_SERVER_ERRORhardcoded - In
ConsumerHandler.javaandProducerHandler.java, they usegetErrorCode(e)which properly mapsIllegalArgumentExceptiontoSC_BAD_REQUEST(400)
The fix is to change the
ReaderHandlerto usegetErrorCode(e)andgetErrorMessage(e)instead of the hardcoded 500 error code.Let me look at the exact lines in
ReaderHandler.java:- In
Assistant
Let me see the exact context of the error handling block:
Assistant
Now I'll make the fix. I need to replace the hardcoded
HttpServletResponse.SC_INTERNAL_SERVER_ERRORwithgetErrorCode(e)and the error message withgetErrorMessage(e):Assistant
Now I also need to update the error message to use
getErrorMessage(e):Assistant
Let me verify the changes:
Assistant
The change looks good. Now let me check if
HttpServletResponseis still needed in the imports (it might not be used anymore in the file):Assistant
Good, the import is still there but now it's unused. Let me remove it to keep the code clean:
Assistant
Now let me verify the file compiles correctly by running a quick build:
Assistant
Let me find the correct module name:
Assistant
I need to find the correct module name. Let me search more broadly.
Assistant
The project uses Gradle, not Maven. Let me try with Gradle:
Assistant
The build succeeded. The code compiles correctly. Now let me look for existing tests related to
ReaderHandlerto see if I can run them:Assistant
Let me look at the existing test to understand the test structure:
Assistant
Let me run the existing tests to make sure my changes don't break anything:
Assistant
The tests pass. Now let me verify the exact diff of my changes:
Assistant
The diff looks correct. The change is:
- Removed the unused
HttpServletResponseimport - Changed
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create reader: " + e.getMessage())toresponse.sendError(getErrorCode(e), getErrorMessage(e))
This aligns with how
ConsumerHandlerandProducerHandlerhandle errors - they usegetErrorCode(e)which returnsSC_BAD_REQUEST(400) forIllegalArgumentExceptionand other appropriate status codes for other exceptions.Now let me verify my fix handles the edge cases mentioned in the issue:
-
Case 1:
IllegalArgumentExceptionfrom invalid base64 -getErrorCode(e)will returnSC_BAD_REQUEST(400) sinceIllegalArgumentExceptionis handled. -
Case 2:
IOExceptionwrappingIndexOutOfBoundsException- This will still return 500 sinceIOExceptionis not specifically handled. However, looking at the issue description, the root cause is an invalidmessageIdthat leads to these exceptions. Let me check if there's a way to handle this better.
- Removed the unused
Assistant
Looking at Case 2 from the issue:
- An
IOExceptionis thrown fromMessageIdImpl.fromByteArray()when the decoded bytes are not a valid message ID format - This
IOExceptionis not wrapped in anIllegalArgumentException
The
getErrorCode()method doesn't have a specific handler forIOExceptionwrappingIndexOutOfBoundsException. However, looking at the issue more carefully:-
Case 1:
IllegalArgumentExceptionfromBase64.getDecoder().decode()- This is now properly handled bygetErrorCode()which returns 400. -
Case 2:
IOExceptionwrappingIndexOutOfBoundsException- The cause is still an invalid user input. We should catch this earlier and wrap it in anIllegalArgumentException.
Looking at the issue description, it says:
ReaderHandler.getMessageId()performs no validation on themessageIdquery parameter before passing it toBase64.getDecoder().decode()and .- An
Assistant
Let me look at the
AbstractWebSocketHandler.getErrorCode()method again to understand how it handlesIOException:Assistant
The
getErrorCode()method only checks the top-level exception type, not the cause. For Case 2, the exception is anIOException, which would still return 500.The proper fix is to catch the exceptions in
getMessageId()and rethrow them asIllegalArgumentException. This is the more robust approach since it validates the user input properly. Let me update thegetMessageId()method:Assistant
Now I need to modify the
getMessageId()method to catch and rethrow exceptions asIllegalArgumentException. However, since the method signature declaresthrows IOException, I should be careful. Looking at the code, the issue is that when an invalidmessageIdis provided, either:Base64.getDecoder().decode()throwsIllegalArgumentException(Case 1)MessageIdImpl.fromByteArray()throwsIOException(Case 2)
For Case 1, the fix I already made (using
getErrorCode()) will properly return 400 becauseIllegalArgumentExceptionis mapped toSC_BAD_REQUEST.For Case 2, the
IOExceptionneeds to be wrapped asIllegalArgumentExceptionto get the proper 400 status code. Let me update thegetMessageId()method:Assistant
Now let me apply this change to the file. I need to replace the
getMessageId()method:Assistant
Let me verify the file is correct:
Assistant
Now let me verify the file compiles correctly:
Assistant
The code compiles. Now let me run the tests again to make sure everything still works:
Assistant
All tests pass. Let me verify the complete diff of my changes:
Assistant
The changes look good. Let me summarize the changes made:
-
Removed unused import:
javax.servlet.http.HttpServletResponseis no longer needed since we're usinggetErrorCode(e)instead ofHttpServletResponse.SC_INTERNAL_SERVER_ERROR. -
Changed error handling in constructor (lines 137-138): Changed from hardcoded
HttpServletResponse.SC_INTERNAL_SERVER_ERRORto usinggetErrorCode(e)andgetErrorMessage(e)from the parent classAbstractWebSocketHandler. This aligns with howConsumerHandlerandProducerHandlerhandle errors. -
Added validation in
getMessageId()method:- Removed
throws IOExceptionfrom the method signature - Added try-catch block around the Base64 decode and
MessageIdImpl.fromByteArray()call - Both
IllegalArgumentException(from invalid base64) andIOException(from invalid message ID bytes) are caught and re-thrown as with a descriptive message
- Removed
-
Assistant
The failing test seems to be related to a different issue (HTTP 404 Not Found when upgrading to WebSocket), which is unrelated to my changes. Let me check what test is failing:
Assistant
The test
consumerBuilderTestwas skipped due to connection refused (no broker running). This is not related to my changes. Let me also check what test failed:Assistant
Let me check which test actually failed (1 failure was reported).
Assistant
Now I need to check if this test was already failing before my changes or if my changes broke it. Let me look at the test details to understand what's happening.
Assistant
This test failure (
PingPongSupportTest.testPingPong) is a WebSocket upgrade error getting a 404 Not Found. This is not related to my changes - it's an infrastructure issue (the WebSocket server endpoint is not available). This test requires a running Pulsar broker which is not available in this test environment.Let me verify by resetting my changes temporarily and running the test:
Assistant
The test also fails without my changes, confirming that this is a pre-existing test failure due to missing infrastructure (no running broker). Let me restore my changes:
Assistant
Now let me run only the unit tests that don't require a running broker:
Assistant
All the unit tests that don't require a running broker pass successfully. </tool_call>
Assistant
All the relevant unit tests pass. Let me do a final review of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.