[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 start by exploring the repository structure to understand the codebase and locate the relevant files. Let me first look at the overall structure.
Assistant
The issue is about ReaderHandler.java in the websocket module. Let me find that file.
Assistant
Let me open the ReaderHandler.java file to understand the issue.
Assistant
Let me scroll down to see the getMessageId method and the error handling in the constructor.
Assistant
Let me see the constructor part more clearly, particularly the catch block:
Assistant
Now let me look at AbstractWebSocketHandler to understand getErrorCode:
Assistant
Let me see what that method does.
Assistant
Let me check how ConsumerHandler and ProducerHandler handle the error to see the pattern they use:
Assistant
Let me now look at the ConsumerHandler more fully:
Assistant
Let me verify by looking at ProducerHandler to confirm the pattern:
Assistant
Let me see more of the ProducerHandler's error handling:
Assistant
Now let me run existing tests to make sure they pass before making changes:
Assistant
Let me look at the existing ReaderHandlerTest to understand how tests are structured:
Assistant
Let me continue reading the test file to see if there are any tests related to error handling:
Assistant
Now I understand the issue clearly. Let me implement the fix. The issue is that ReaderHandler's constructor catches exceptions but hardcodes HttpServletResponse.SC_INTERNAL_SERVER_ERROR in the error response, while ConsumerHandler and ProducerHandler both call AbstractWebSocketHandler.getErrorCode(e) instead, which maps IllegalArgumentException to SC_BAD_REQUEST (400).
Let me make the fix:
Assistant
The file seems to have gotten corrupted. Let me restore it first:
Assistant
Let me re-read the file to see its current state:
Assistant
Now I have a clear picture. Let me apply the fix using the proper syntax. First, let me fix the catch block in the constructor:
Assistant
Good, the catch block is now updated. Now I need to also look at the getMessageId() method to see if it needs to wrap exceptions better. Looking at the issue again:
IllegalArgumentException from Base64.getDecoder().decode() - this is already IllegalArgumentException and will be correctly mapped to SC_BAD_REQUEST by getErrorCode().
wrapping from - this is , not . The method doesn't handle specially, so it would fall through to .
Assistant
Now I need to update the getMessageId() method to wrap exceptions into IllegalArgumentException so that they are properly mapped to 400. Let me update it:
Assistant
Now let me verify the full file to make sure everything looks correct:
Assistant
Let me check that imports are correct (IOException should already be imported):
Assistant
Good, the imports look correct. IOException is already imported (line 24). The getErrorCode and getErrorMessage methods are inherited from AbstractWebSocketHandler.
Now let me verify that the getErrorMessage method exists in AbstractWebSocketHandler (I saw it earlier at line 206). Let me double-check.
Assistant
Let me check if HttpServletResponse is still used elsewhere (it might be unused now, but let's verify):
Assistant
Let me check if HttpServletResponse is used anywhere else in the file:
Assistant
The HttpServletResponse import is no longer used since we replaced the direct reference. Let me remove it:
Assistant
Now let me verify the final state of the file with git diff:
Assistant
The changes look good. Let me now run the existing tests to make sure they pass:
Assistant
Let me check the correct module name.
Assistant
Let me check the module structure.
Assistant
Let me check the project structure.
Assistant
It's a Gradle project. Let me check the build system.
Assistant
This is a Gradle project. Let me try running the ReaderHandlerTest:
Assistant
The tests pass. Let me also verify by looking at the getErrorMessage method to make sure it will produce a reasonable message for IllegalArgumentException:
Assistant
Good. The getErrorMessage method for IllegalArgumentException returns "Invalid query params: " + e.getMessage(). Since our wrapped IllegalArgumentException has the message "Invalid messageId query parameter: ...", the final error message will be "Invalid query params: Invalid messageId query parameter: ..." which is reasonable.
Let me now create a simple test script to verify the behavior:
Assistant
Let me check which test failed:
Assistant
This failure appears to be in PingPongSupportTest - a pre-existing test that requires a running server. Let me check if this was failing before my changes:
Assistant
Good, the PingPongSupportTest failure is pre-existing. Let me restore my changes and run only the ReaderHandlerTest:
Assistant
All the existing ReaderHandler tests pass. Let me also run the AbstractWebSocketHandlerTest:
Assistant
All tests pass. Let me now review the final diff one more time:
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-$
IOException
IndexOutOfBoundsException
MessageIdImpl.fromByteArray()
IOException
IllegalArgumentException
getErrorCode()
IOException
SC_INTERNAL_SERVER_ERROR
Wait, let me look more carefully. The issue says:
Case 1: IllegalArgumentException from Base64.getDecoder().decode() → this would now be correctly mapped to 400 by getErrorCode() ✓
Case 2: IOException wrapping IndexOutOfBoundsException from MessageIdImpl.fromByteArray() → this is IOException and would fall through to 500 stillSo we should also wrap the IOException from MessageIdImpl.fromByteArray() into an IllegalArgumentException so it gets properly mapped to 400.
The issue is that Base64.getDecoder().decode() can throw IllegalArgumentException (which is correct - it's a bad request), and MessageIdImpl.fromByteArray() can throw IOException wrapping other exceptions (which should also be a bad request since the user provided an invalid messageId).
Both of these should be wrapped in an IllegalArgumentException or handled so they return 400. Let me update getMessageId() to wrap the exceptions:
Good, the catch block is updated. Now let me also look at the getMessageId() method to see if we should wrap exceptions better. Let me check it: