[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():
code
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]
Assistant
Now let's fix both issues: use getErrorCode(e), and wrap IOException/IndexOutOfBoundsException from getMessageId into IllegalArgumentException, and IllegalArgumentException from Base64 decode is already IllegalArgumentException. Let's check MessageIdImpl.fromByteArray to see what exceptions it can throw.
Assistant
Let me use the open and replace special commands instead.
Assistant
Now update getMessageId method.
Assistant
Now let's also check getReceiverQueueSize for similar issue - Integer.parseInt could throw NumberFormatException which is also IllegalArgumentException subclass, so that's fine already with getErrorCode fix.
Let's view the full file to confirm everything looks fine.
Assistant
Good, this looks correct. Now let's check ConsumerHandler for similar getMessageId pattern to confirm consistency and check if similar bug exists there.
Assistant
Compilation succeeded (only warnings, no errors). Let's check existing tests for ReaderHandler.
Assistant
Let's run these existing tests to confirm nothing broke.
Assistant
Tests passed (no failures reported). Let's check the test report to be sure.
Assistant
All 3 tests passed. Now let's write a temporary reproduce script to verify the actual fix works (400 instead of 500) using a small Java test harness.
Assistant
It uses request.getParameterMap(). I need to mock that instead of URI query string.
Assistant
Let's fix my repro test to mock getParameterMap properly.
Assistant
Now copy this test into the actual test source tree temporarily to run via gradle, then remove it after.
Assistant
Test passed silently (no failure output). Let's confirm via test report.
Assistant
Let's verify the fix actually matters by testing against the original buggy code temporarily.
Assistant
This confirms the bug existed (500 returned before fix) and reproduced exactly the reported IndexOutOfBoundsException. Now let's rerun with the fix applied to confirm it passes again, then remove the temp test file.
Assistant
Confirmed passing with fresh run. Now remove the temporary test file (not to be included in submission).
Assistant
Now let's run the full existing ReaderHandlerTest suite once more to be certain, plus check ConsumerHandler/ProducerHandler tests aren't affected (they weren't touched, but let's just confirm compile of whole module).
Assistant
All 3 existing tests pass with the fix. Let's do a final review of the diff.
Assistant
This is a clean, minimal fix. Let's double check that HttpServletResponse import is still used (yes, in the catch block for sendError still uses HttpServletResponse.SC... no wait we removed that usage). Let's check unused import warning.
Assistant
Now recompile to verify no errors from the unused import removal.
Assistant
Compiles cleanly. Let's run the full test suite once more.
Assistant
All tests pass. Let's view final diff.
Assistant
This is a clean and minimal fix. Now let's go through the checklist.
Assistant
Good, no leftover repro test file. Now let's write out the checklist reasoning.
Checklist review:
Reproduced the reported error: Yes. I created a temporary test that mocked a ReaderHandler construction with an invalid messageId query parameter (invalidMessageId), then stashed my fix to run against the original code. This reproduced the exact IndexOutOfBoundsException/IOException chain from the issue, and confirmed that sendError was called with 500 and the message "Failed to create reader: java.lang.IndexOutOfBoundsException: readerIndex(5) + length(641882) exceeds writerIndex(12): ..." — matching the issue's stack trace precisely.
Confirmed the fix resolves the error: Yes. After popping the stash (restoring my fix), I reran the same temporary test and it passed, confirming sendError is now called with 400 (SC_BAD_REQUEST) instead of 500. I verified this both for the non-base64 string case () directly, and the fix also correctly handles the base64-but-invalid-protobuf case handled by the same code path (since both from and / from are now caught and rethrown as , which maps to 400).
Assistant
Thought about edge cases:
earliest/latest string values still work as before (unaffected by change).
Blank/null messageId still uses MessageId.latest default (unaffected, uses isNotBlank).
Malformed base64 string → caught as IllegalArgumentException → mapped to 400.
Valid base64 but structurally invalid protobuf bytes (e.g., too short) → MessageIdImpl.fromByteArray throws IOException wrapping the underlying exception → now caught and rethrown as IllegalArgumentException → mapped to 400.
Valid base64, valid protobuf bytes but semantically nonsensical (shouldn't normally throw) → still works as before, no behavior change for valid input.
Other exceptions unrelated to messageId (e.g., topic not found, authorization failures) during reader creation still get properly classified through , which was already used correctly by / — this fixes an inconsistency where hardcoded 500 for all exceptions, not just messageId-related ones. This is a more general and correct fix, not narrowly tailored only to the messageId case.
Assistant
Confirmed: PingPongSupportTest fails even without my changes (pre-existing environment issue, likely network/port binding related, unrelated to this fix). This confirms my change doesn't introduce any new test failures.
Assistant
Ran existing tests that might be affected: Yes. All 3 pre-existing ReaderHandlerTest tests pass with the fix. The only other failing test in the module (PingPongSupportTest) is a pre-existing environment-related failure (websocket upgrade 404, unrelated to my change) that I confirmed fails identically with or without my change (verified via git stash).
The fix is complete, minimal, targeted, and verified. Let's submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Case 2 — IOException wrapping IndexOutOfBoundsException from MessageIdImpl.fromByteArray():
code
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
text
**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!
Interface
No new interfaces are introduced.
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-$
invalidMessageId
IllegalArgumentException
Base64.getDecoder().decode()
IOException
RuntimeException
MessageIdImpl.fromByteArray()
IllegalArgumentException
getErrorCode()
getErrorCode(e)
ConsumerHandler
ProducerHandler
ReaderHandler
I did not modify ConsumerHandler's handleAck/handleNack methods since those are invoked from onWebSocketText after the WebSocket connection is already established (not part of the initial HTTP upgrade handshake), and are handled differently (connection close on IOException) — out of scope for this HTTP 500 vs 400 issue which is specific to the constructor/handshake path.