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 analyze the issue:
- The bug is in
ReaderHandlerclass in the WebSocket module - When an invalid
messageIdquery parameter is provided, it returns HTTP 500 instead of HTTP 400 - The issue is that
ReaderHandlercatchesExceptionand hardcodesSC_INTERNAL_SERVER_ERROR(500) instead of usingAbstractWebSocketHandler.getErrorCode(e)likeConsumerHandlerandProducerHandlerdo
Let me first explore the repository structure to find the relevant files.
- The bug is in
Assistant
Let me look at the ReaderHandler.java file to understand the current implementation.
Assistant
Now let me look at the
AbstractWebSocketHandlerto understand howgetErrorCodeworks:Assistant
Let me look at the AbstractWebSocketHandler to see the getErrorCode method:
Assistant
Now let me look at how ConsumerHandler and ProducerHandler handle errors:
Assistant
Let me look at how ConsumerHandler handles errors in its constructor to see how it uses
getErrorCode:Assistant
Let me see the exact error handling code in ConsumerHandler for reference:
Assistant
Let me also check the ProducerHandler error handling:
Assistant
Now I understand the issue. The
ReaderHandlercatches exceptions and usesSC_INTERNAL_SERVER_ERROR(500) hardcoded, whileConsumerHandlerandProducerHandlerusegetErrorCode(e)which properly mapsIllegalArgumentExceptiontoSC_BAD_REQUEST(400).The fix is simple: change
ReaderHandlerto usegetErrorCode(e)andgetErrorMessage(e)from the parent class, just likeConsumerHandlerdoes.Let me look at the exact lines that need to be changed in ReaderHandler:
Assistant
Let me find the exact lines for the error handling code that needs to be changed:
Assistant
Now I understand the issue clearly. The fix is to replace the hardcoded
HttpServletResponse.SC_INTERNAL_SERVER_ERRORwithgetErrorCode(e)from the parent class, and usegetErrorMessage(e)as well, just likeConsumerHandlerdoes.Let me make the fix:
Assistant
Now I'll fix the
ReaderHandler.javato usegetErrorCode(e)andgetErrorMessage(e)instead of the hardcoded values:Assistant
Now I'll make the edit. The fix is to replace:
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create reader: " + e.getMessage());with:
response.sendError(getErrorCode(e), getErrorMessage(e));This is consistent with how ConsumerHandler does it.
Assistant
The edit command format seems to require a different syntax. Let me try using the bash command to make the edit with sed or by using the proper edit tool format.
Assistant
The lines to replace are 138-139 (the two lines with the sendError call). Let me try the edit command with the correct format.
Assistant
The edit command isn't working. Let me try using the special edit command format properly - it seems like I need to use the proper tool name.
Assistant
Let me verify the change was made correctly:
Assistant
The fix is in place. Let me verify the full context of the file to make sure everything is correct:
Assistant
Now the import for
HttpServletResponseis no longer needed since we're not directly usingSC_INTERNAL_SERVER_ERRORanymore. Let me check if it's used elsewhere in the file:Assistant
The
HttpServletResponseimport is no longer used in the file since we replaced the direct reference. Let me remove the unused import:Assistant
The HttpServletResponse import is on line 31. Let me remove it since it's no longer used.
Assistant
Let me verify the changes are correct by checking the final state of the file:
Assistant
Let me now verify the complete error handling block looks correct:
Assistant
The fix looks correct. Let me verify the file compiles by checking the imports section and the whole class:
Assistant
The changes look good. Let me verify by trying to compile the module:
Assistant
Let me find the pom.xml in the pulsar-websocket module to compile just that module.
Assistant
Let me explore the repository structure to understand how it's built.
Assistant
Let me check the repository structure differently:
Assistant
This is a Gradle project. Let me look for existing tests related to ReaderHandler to verify the fix works correctly:
Assistant
Let me look at the existing tests for ReaderHandler:
Assistant
Let me verify my changes are complete by looking at the git diff:
Assistant
Looking at the diff, the fix is in place. Now let me analyze the edge cases more carefully:
- Case 1 (
IllegalArgumentExceptionfrom Base64 decode) - Will be fixed by usinggetErrorCode(e)since it returns 400 forIllegalArgumentException - Case 2 (
IOExceptionwrappingIndexOutOfBoundsException) - This is still anIOExceptionwrapping anIndexOutOfBoundsException, not anIllegalArgumentException, so it would still return 500
Let me verify by checking the
getErrorCodemethod and see if it also handlesIOException:- Case 1 (
Assistant
Looking more carefully at the issue, I need to consider both cases:
-
Case 1:
IllegalArgumentExceptionfrom Base64 decode - my fix handles this becausegetErrorCode()returns 400 forIllegalArgumentException. -
Case 2:
IOExceptionwrappingIndexOutOfBoundsExceptionfromMessageIdImpl.fromByteArray()- this is NOT handled by the currentgetErrorCode()since it's anIOException.
The best fix is to make
getMessageId()wrap all parsing/validation exceptions inIllegalArgumentExceptionsince they all represent invalid user input. Let me check thegetMessageId()method:-
Assistant
Let me look at the getMessageId() method location:
Assistant
Now I need to look at the
getMessageId()method more carefully. The issue is that:Base64.getDecoder().decode()throwsIllegalArgumentExceptionMessageIdImpl.fromByteArray()throwsIOExceptionwrappingIndexOutOfBoundsException
Since these are both validation errors for user input (invalid messageId parameter), they should all be wrapped in
IllegalArgumentExceptionso thatgetErrorCode()properly returns 400.Let me look at the full method.
Assistant
Now I need to also address Case 2 - the
IOExceptioncase. Looking at thegetMessageId()method, whenMessageIdImpl.fromByteArray()throws anIOException, thegetErrorCode()method returns 500 because it's not anIllegalArgumentException.The cleanest fix is to make
getMessageId()wrap all exceptions (bothIllegalArgumentExceptionandIOException) into a single exception type that indicates invalid user input. Since the constructor already catches these and passes them togetErrorCode(), I should modifygetMessageId()to wrap exceptions appropriately.Let me update the
getMessageId()method:Assistant
I need to update
getMessageId()to wrap bothIllegalArgumentException(from base64 decode failure) andIOException(from MessageId parsing failure) intoIllegalArgumentException. This way, when the exception propagates to the constructor's catch block,getErrorCode()will return 400.Assistant
I need to update the
getMessageId()method to wrap bothIllegalArgumentException(from base64 decode failure) andIOException(from MessageId parsing failure) intoIllegalArgumentException. This way, when the exception propagates to the constructor's catch block,getErrorCode()will return 400.Assistant
Let me apply the fix to the getMessageId() method. I need to replace lines 386-396 with the new version that wraps exceptions in IllegalArgumentException.
Assistant
Let me verify the changes were applied correctly by looking at the file.
Assistant
Let me verify the final state of my changes:
Assistant
Let me verify the entire file looks syntactically correct by checking its structure:
Assistant
The file looks syntactically correct. Now let me run the existing tests to verify the changes don't break anything:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.