Untitled conversation
- User
[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
Now I'll check the existing tests for the websocket module:
- Assistant
Now I'll make the fix in
ReaderHandler.java: - Assistant
Now add tests. I'll add a test covering both invalid
messageIdcases: - Assistant
Now run the tests:
- Assistant
The build is still starting up (compiling module dependencies takes a while in this repo). I'll wait for the monitor to report.
- Assistant
The monitor is already armed on the build output — I'll report as soon as the test run completes.
- Assistant
Gradle is running (the daemon is compiling the dependency modules before it can run the test). I'll wait for the completion notification from the background task and report the results then.
- Assistant
The diff is complete. Waiting for the checkstyle run to confirm no style violations, then I'll summarize.
- Assistant
Everything passes. The fix is complete:
Summary
Problem: The WebSocket
ReaderHandlerreturned HTTP 500 for an invalidmessageIdquery parameter, because:getMessageId()passed the raw parameter toBase64.getDecoder().decode()/MessageIdImpl.fromByteArray()with no validation — throwing rawIllegalArgumentExceptionorIOException.- The constructor's catch block hardcoded
SC_INTERNAL_SERVER_ERROR, unlikeConsumerHandler/ProducerHandlerwhich usegetErrorCode(e).
Changes (
pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java):
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.