[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 messageId query 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== becomes CJCj6QEQ 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 — IllegalArgumentException from Base64.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 — IOException wrapping from :
Assistant
I’ll locate the WebSocket handlers and relevant tests, compare ReaderHandler’s exception handling with consumer/producer behavior, then reproduce through the narrowest existing test path before making a focused fix. Afterward I’ll run the affected module tests and inspect the final diff.
Assistant
The search confirms the inconsistency described: ReaderHandler has its own setup catch path, while the shared error mapper already exists. I’ll inspect constructor ordering, mapper semantics (especially wrapped exceptions), and current ReaderHandler test scaffolding to determine whether merely using the mapper is sufficient or malformed decoded protobuf bytes also need normalization to an argument error.
Assistant
getErrorCode only classifies IllegalArgumentException; malformed Base64 already has that type, but syntactically valid Base64 containing invalid MessageId protobuf is surfaced as IOException, so changing only the response call would still return 500 for the second reported case. The robust boundary is getMessageId: treat both decoding and deserialization failures as invalid query input by wrapping them in IllegalArgumentException, then let the constructor use the shared mapper. I’ll verify the full catch/cleanup logic and query parsing used by mocks before editing.
Assistant
The constructor currently hardcodes both status and message, confirming both changes are needed. I’ll make getMessageId a validation boundary: preserve earliest/latest and valid IDs, but convert either Base64 or MessageId parsing failures into one IllegalArgumentException with a stable parameter-specific message. Then ReaderHandler will call the same getErrorCode/getErrorMessage helpers as the other handlers. No test files will be changed.
Assistant
Python is unavailable in this environment, so no file was changed. I’ll apply the same focused replacements using the repository editor command and immediately inspect the diff for correctness.
Assistant
The editor requires the target region to be in its active displayed window. I’ll open around each occurrence and apply the replacements there.
Assistant
The diff now implements both required paths. I’ll check imports and style, then run the focused ReaderHandler test. Since existing tests do not cover malformed query IDs and repository tests cannot be altered, I’ll also use a temporary standalone Java/JShell-style probe only if needed; preferably the existing mocked constructor can be exercised through the module test infrastructure with command-line selection.
Assistant
This checkout does not include a Maven wrapper, and pulsar-websocket is an aggregate directory rather than a module root. The status check also reveals HttpServletResponse became unused after removing the hardcoded constant. I’ll remove that import, identify the actual module/build entry point, and run its focused test with the available Maven installation.
Assistant
The repository uses Gradle in this branch. I’ll locate the project path and available test task, then invoke only ReaderHandlerTest to keep validation targeted.
Assistant
The module is :pulsar-websocket. I’ll run its test task filtered to ReaderHandlerTest; this also compiles the changed production class and catches import/type/style issues relevant to the fix.
Assistant
The focused test suite passes, including compilation. To directly confirm both reported inputs rather than relying only on reasoning, I’ll create a temporary probe under /tmp (not in the repository and not a test modification). It will instantiate ReaderHandler with Mockito-backed request/service objects for (1) a decoded space in Base64 and (2) invalidMessageId, and assert that each upgrade response receives HTTP 400. I’ll obtain the existing test runtime classpath from Gradle, compile/run the probe, and delete temporary files afterward.
Assistant
The temporary classpath task was blocked by Gradle’s configuration-cache restrictions; it did not affect repository files. I’ll rerun this diagnostic task with configuration caching disabled, which permits the one-off project lookup.
Assistant
The runtime classpath is available. I’ll now run the out-of-tree probe against the compiled changed class. It checks the exact two examples and fails with an assertion if the handler does not send status 400.
Assistant
The temporary probe reproduced both original exception chains and confirmed each now calls sendError(400, ...); temporary files were removed. The focused tests passed. I’ll now run the complete pulsar-websocket test task to cover other handler behavior, then inspect the final diff/status and ensure no generated or temporary files are tracked.
Assistant
The complete module suite reached 11 tests but failed in unrelated PingPongSupportTest.testPingPong[producer] because its embedded endpoint returned 404; the changed ReaderHandler tests had already passed, and this failure does not traverse messageId parsing. I’ll run the module’s Java formatting/static check if available, then review the exact diff. I’ll also rerun the focused test after the full suite to provide a clean final test result unaffected by that endpoint test.
Assistant
Formatting passes. I’ll run Checkstyle on production sources and rerun ReaderHandlerTest, then perform final status/diff checks.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
WARN org.apache.pulsar.websocket.ReaderHandler - [REDACTED] Failed in creating reader on topic persistent://REDACTED/REDACTED/REDACTED
java.io.IOException: java.lang.IndexOutOfBoundsException: readerIndex(5) + length(641882) exceeds writerIndex(12): UnpooledHeapByteBuf(ridx: 5, widx: 12, cap: 12/12)
at org.apache.pulsar.client.impl.MessageIdImpl.fromByteArray(MessageIdImpl.java:93) ~[org.apache.pulsar-pulsar-client-original-4.0.7.jar:4.0.7]
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]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(5) + length(641882) exceeds writerIndex(12): UnpooledHeapByteBuf(ridx: 5, widx: 12, cap: 12/12)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1442) ~[io.netty-netty-buffer-4.1.127.Final.jar:4.1.127.Final]
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1428) ~[io.netty-netty-buffer-4.1.127.Final.jar:4.1.127.Final]
at io.netty.buffer.AbstractByteBuf.skipBytes(AbstractByteBuf.java:971) ~[io.netty-netty-buffer-4.1.127.Final.jar:4.1.127.Final]
at org.apache.pulsar.common.api.proto.LightProtoCodec.skipUnknownField(LightProtoCodec.java:264) ~[org.apache.pulsar-pulsar-common-4.0.7.jar:4.0.7]
at org.apache.pulsar.common.api.proto.MessageIdData.parseFrom(MessageIdData.java:304) ~[org.apache.pulsar-pulsar-common-4.0.7.jar:4.0.7]
at org.apache.pulsar.client.impl.MessageIdImpl.fromByteArray(MessageIdImpl.java:91) ~[org.apache.pulsar-pulsar-client-original-4.0.7.jar:4.0.7]
Details
ReaderHandler.getMessageId() performs no validation on the messageId query parameter before passing it to Base64.getDecoder().decode() and MessageIdImpl.fromByteArray().
ReaderHandler's constructor catches Exception during setup but hardcodes HttpServletResponse.SC_INTERNAL_SERVER_ERROR in the error response. ConsumerHandler and ProducerHandler both call AbstractWebSocketHandler.getErrorCode(e) instead, which maps IllegalArgumentException to SC_BAD_REQUEST (400).
Error messages
**Case 1** — `IllegalArgumentException` from `Base64.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** — `IOException` wrapping `IndexOutOfBoundsException` from `MessageIdImpl.fromByteArray()`:
WARN org.apache.pulsar.websocket.ReaderHandler - [REDACTED] Failed in creating reader on topic persistent://REDACTED/REDACTED/REDACTED
java.io.IOException: java.lang.IndexOutOfBoundsException: readerIndex(5) + length(641882) exceeds writerIndex(12): UnpooledHeapByteBuf(ridx: 5, widx: 12, cap: 12/12)
at org.apache.pulsar.client.impl.MessageIdImpl.fromByteArray(MessageIdImpl.java:93) ~[org.apache.pulsar-pulsar-client-original-4.0.7.jar:4.0.7]
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]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(5) + length(641882) exceeds writerIndex(12): UnpooledHeapByteBuf(ridx: 5, widx: 12, cap: 12/12)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1442) ~[io.netty-netty-buffer-4.1.127.Final.jar:4.1.127.Final]
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1428) ~[io.netty-netty-buffer-4.1.127.Final.jar:4.1.127.Final]
at io.netty.buffer.AbstractByteBuf.skipBytes(AbstractByteBuf.java:971) ~[io.netty-netty-buffer-4.1.127.Final.jar:4.1.127.Final]
at org.apache.pulsar.common.api.proto.LightProtoCodec.skipUnknownField(LightProtoCodec.java:264) ~[org.apache.pulsar-pulsar-common-4.0.7.jar:4.0.7]
at org.apache.pulsar.common.api.proto.MessageIdData.parseFrom(MessageIdData.java:304) ~[org.apache.pulsar-pulsar-common-4.0.7.jar:4.0.7]
at org.apache.pulsar.client.impl.MessageIdImpl.fromByteArray(MessageIdImpl.java:91) ~[org.apache.pulsar-pulsar-client-original-4.0.7.jar:4.0.7]
Reproducing the issue
Connect to the WebSocket reader endpoint with an invalid messageId query 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== becomes CJCj6QEQ UQwAA==).
An arbitrary non-base64 string: ?messageId=invalidMessageId
Additional information
ReaderHandler.getMessageId() performs no validation on the messageId query parameter before passing it to Base64.getDecoder().decode() and MessageIdImpl.fromByteArray().
ReaderHandler's constructor catches Exception during setup but hardcodes HttpServletResponse.SC_INTERNAL_SERVER_ERROR in the error response. ConsumerHandler and ProducerHandler both call AbstractWebSocketHandler.getErrorCode(e) instead, which maps IllegalArgumentException to SC_BAD_REQUEST (400).
Are you willing to submit a PR?
I'm willing to submit a PR!
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work.
For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state.
You can also reset all changes in the repository using git reset --hard command.
Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later.
Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands.
However you don't need to use git add or git commit before submitting your changes. In fact,
submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.
CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
If an error was reported in the issue description, I have successfully reproduced it.
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
I have thought about edge cases and made sure my fix handles them as well.
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pulsar, current file: none) bash-$