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 —
IOExceptionwrappingIndexOutOfBoundsExceptionfromMessageIdImpl.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]Details
ReaderHandler.getMessageId()performs no validation on themessageIdquery parameter before passing it toBase64.getDecoder().decode()andMessageIdImpl.fromByteArray().ReaderHandler's constructor catchesExceptionduring setup but hardcodesHttpServletResponse.SC_INTERNAL_SERVER_ERRORin the error response.ConsumerHandlerandProducerHandlerboth callAbstractWebSocketHandler.getErrorCode(e)instead, which mapsIllegalArgumentExceptiontoSC_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
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
Additional information
ReaderHandler.getMessageId()performs no validation on themessageIdquery parameter before passing it toBase64.getDecoder().decode()andMessageIdImpl.fromByteArray().ReaderHandler's constructor catchesExceptionduring setup but hardcodesHttpServletResponse.SC_INTERNAL_SERVER_ERRORin the error response.ConsumerHandlerandProducerHandlerboth callAbstractWebSocketHandler.getErrorCode(e)instead, which mapsIllegalArgumentExceptiontoSC_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 usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto 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-$
Assistant
Now let's fix
getMessageId()to wrap exceptions inIllegalArgumentException, and fix the constructor to usegetErrorCode(e).Assistant
Now let's make the changes. First, update
getMessageId()to wrap exceptions properly.Assistant
There's a duplicate closing brace at the end now. Let me remove line 409.
Assistant
Good, now the file is properly closed. Now let's fix the constructor's error handling to use
getErrorCodeandgetErrorMessage.Assistant
Let's check how ConsumerHandler does this exactly, for consistency.
Assistant
Now let's replace the ReaderHandler's constructor error handling accordingly.
Assistant
Now
HttpServletResponseimport may be unused sinceSC_INTERNAL_SERVER_ERRORreference is removed. Let's check.Assistant
The
HttpServletResponseimport is now unused. Let's remove it to avoid unused import checkstyle failures.Assistant
Now let's verify the full file compiles correctly, and check the final state of the getMessageId method and constructor error handling.
Assistant
Good, so
IOExceptioncatch handles the wrappedIndexOutOfBoundsException. My catch clause ofIllegalArgumentException | IOExceptioncorrectly covers both cases. Now let's compile the module.Assistant
This is a Gradle-based build. Let's try compiling with Gradle in offline mode.
Assistant
It compiled successfully. Now let's check for existing tests for ReaderHandler.
Assistant
Now let's run these tests to confirm nothing broke.
Assistant
Tests passed (no failures reported). Let's check the test report to be sure.
Assistant
All 3 tests pass with no errors. Now let's write a small reproduction script to simulate
getMessageIdbehavior directly, verifying it now throws IllegalArgumentException and results in 400 status code.Assistant
Test passed (no failures reported). Let's check test result XML to confirm.
Assistant
The reproduction confirms exactly the exception from Case 2 in the issue, now correctly mapped to 400. Let's also verify Case 1 (bad base64 character) triggers 400.
Assistant
Both cases from the issue now correctly result in HTTP 400 instead of 500. Now let's clean up the temporary test file.
Assistant
Now let's re-run the existing full ReaderHandlerTest suite once more to make sure everything is clean, and check final diff.
Assistant
Only the intended file is modified. Let's run checkstyle for this module to be safe.
Assistant
Checkstyle passes. Let's view the final diff once more for review.
Assistant
This looks clean and minimal. Let's go through the checklist.
Assistant
Good, only the source file is changed, no leftover test files.
Checklist review:
- Reproduced the reported error: Yes. I created a temporary test (removed afterward) that reproduced both Case 1 (invalid base64 character due to unescaped
+) and Case 2 (arbitrary non-base64 string causingIndexOutOfBoundsExceptionwrapped inIOException). Both confirmed to trigger the exact same stack traces described in the issue before my fix, and I verified this behavior mapped toSC_INTERNAL_SERVER_ERRORoriginally (via code inspection:ReaderHandlerhardcodedSC_INTERNAL_SERVER_ERROR). - Confirmed fix resolves the error: Yes. After the fix, both temporary tests confirmed
sendErroris called withHttpServletResponse.SC_BAD_REQUEST(400) instead of 500, with the same underlying exceptions now wrapped asIllegalArgumentExceptionand correctly mapped viagetErrorCode(). - Thought about edge cases:
earliest/lateststring values still work as before (untouched code path).- Blank/absent
messageIdstill defaults toMessageId.latest(untouched). - Both
IllegalArgumentException(invalid base64 chars) andIOException(parsing failures wrappingIndexOutOfBoundsException, malformed protobuf, etc.) are now caught and converted toIllegalArgumentException, which is mapped bygetErrorCode()to 400. This covers any other malformed message ID byte arrays that could throw either exception type, not just the two specific reproduction cases mentioned in the issue. - Removed now-unused
HttpServletResponseimport to avoid unused-import checkstyle failures. - Constructor now uses shared
getErrorCode(e)/getErrorMessage(e)helpers (as done inConsumerHandler/ProducerHandler), so other exception types (e.g.,AuthenticationException,AuthorizationException,NotFoundException, etc.) thrown during reader creation are now also mapped to more appropriate HTTP status codes instead of always 500, improving consistency across handlers.
- Ran existing tests: Yes.
ReaderHandlerTest(3 tests) passes with the fix, includingtestCreateIllegalReaderImpwhich verifiessendErroris still invoked correctly. Checkstyle also passes for the module. The full module compiles cleanly.
- Reproduced the reported error: Yes. I created a temporary test (removed afterward) that reproduced both Case 1 (invalid base64 character due to unescaped
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →