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 ReaderHandler: use getErrorCode(e) and wrap exceptions from getMessageId to IllegalArgumentException for base64/parse errors.
Assistant
Now update ReaderHandler: change error handling to use getErrorCode/getErrorMessage, and wrap getMessageId exceptions into IllegalArgumentException.
Assistant
I'll use the
edit/replacetool commands instead.Assistant
Now let's update getMessageId method.
Assistant
The import is now unused. Let's remove it to avoid checkstyle failures.
Assistant
Good. Now let's check the full diff and try to compile the module.
Assistant
Now let's check
getMessageIdstill throws IOException per signature - that's fine since IllegalArgumentException is unchecked. Let's compile module to make sure everything works.Assistant
Gradle-based project. Let's try compiling just the websocket module.
Assistant
Compilation succeeds. Now let's check for existing tests related to ReaderHandler to run them.
Assistant
Now let's run this test to make sure it still passes.
Assistant
Test passed silently (no failures reported). Let's check test report for confirmation.
Assistant
All 3 tests pass. Now let's write a quick reproduction test to confirm the fix works for both cases described in the issue.
Assistant
Good, it uses getParameterMap(). Let's run this test.
Assistant
This confirms exactly the bug scenario from the issue (Case 2) is now correctly caught and returns SC_BAD_REQUEST (400). Let's remove the temporary test file now.
Assistant
Now let's run the full existing test suite for the websocket module to ensure nothing broke.
Assistant
Confirmed: this test failure is pre-existing and unrelated to our change (fails even on unmodified code, likely due to environment/network limitations in this sandbox). Our change is verified to not introduce new failures.
Assistant
The diff looks clean and minimal. Let's double check checkstyle passes.
Assistant
Checkstyle passes with no issues. Let's do a final review of the complete file to ensure correctness.
Assistant
The implementation looks correct and complete. Let's go through the checklist.
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 →