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 update
getMessageId()to validate the parameter: - Assistant
The
HttpServletResponseimport is now unused — remove it so checkstyle passes: - Assistant
Now let's check existing tests to see where to add coverage:
- Assistant
The
queryParamsare populated fromrequest.getParameterMap(), so I can drive both failure cases from mocks. Adding tests for both invalid messageId cases: - Assistant
This checkout builds with Gradle, not Maven:
- Assistant
The Gradle build is still starting up. I'll wait for it to finish.
- Assistant
The diff is complete. Now waiting for the Gradle test run to finish — the monitor will report when the build completes.
- Assistant
The build output is buffered through
tail -30, so it will appear when the run finishes. Waiting on the background task and monitor notifications. - Assistant
All 5 tests passed (0 failures, 0 errors), including the two new ones covering both failure modes from the bug report.
Summary
Fixed the WebSocket
ReaderHandlerreturning HTTP 500 for an invalidmessageIdquery parameter — it now returns HTTP 400 Bad Request.Changes in
pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java:-
getMessageId()— now validates themessageIdparameter:Base64.getDecoder().decode()failures (IllegalArgumentException, bug case 1) andMessageIdImpl.fromByteArray()failures (IOException, bug case 2) are both rethrown asIllegalArgumentExceptionwith a message identifying the invalid param value. -
Constructor catch block — replaced the hardcoded
SC_INTERNAL_SERVER_ERRORwithresponse.sendError(getErrorCode(e), getErrorMessage(e)), matching what and already do. maps → 400, and this also fixes the status code for other error types during reader creation (auth → 401/403, topic not found → 404, consumer busy → 409, etc.).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.