[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 IndexOutOfBoundsException from :
Assistant
Now let me compile and check for existing tests:
Assistant
This repo uses Gradle. Let me compile:
Assistant
While compiling, let me check the existing websocket proxy tests:
Assistant
The compile is still running. Meanwhile, let me add a test to ProxyPublishConsumeTest following the existing pattern:
Assistant
Compile is still in progress (Gradle daemon startup on first run takes a while). Waiting for the background task to complete.
Assistant
While waiting, let me verify the test file has all needed imports:
Assistant
All imports are already present. Waiting on the compile to finish, then I'll run the new test.
Assistant
Compile succeeded (only pre-existing warnings). Now running the new test:
Assistant
Still compiling/running — I'll continue when the background task notifies completion.
Assistant
Still building. I'll wait for the completion notification rather than polling further.
Assistant
Build outputs confirm pulsar-websocket compiled cleanly; pulsar-broker is still working through its dependency chain. Waiting for the background notifications.
Assistant
I'll stop polling and wait for the background task notifications to continue.
Assistant
All waiters are armed. Ending this turn — the background task notification for the gradle test run will re-invoke me to verify results.
Assistant
Waiting on background tasks. I'll report the test result when the run finishes.
Assistant
Build still in progress. Ending my turn — the completion notification will bring me back to verify the test outcome.
Assistant
Waiting for the build. I'll continue once notified.
Assistant
Background build still running — ending turn until a task notification arrives.
Assistant
Build is actively compiling (load ~4). Waiting for completion notification.
Assistant
I'll now genuinely idle until a background notification arrives.
Assistant
Idle — awaiting the background build's completion notification to verify the test result.
Assistant
Awaiting the background test run's completion notification. No further action needed until then.
Assistant
Awaiting build completion.
Assistant
Standing by for the test run to finish.
Assistant
All code changes are complete and compiled. The test verification run is in progress in the background with completion notifications armed — I'll confirm the test outcome when it finishes.
Assistant
Awaiting background test completion notification.
Assistant
Monitor armed on the gradle process exit. I'll verify the test result when it fires.
Assistant
Dependencies are progressing (pulsar-client, pulsar-broker-common done; pulsar-broker itself still to compile). Waiting for the monitor event.
Assistant
Awaiting the monitor event.
Assistant
CPU is idle — the daemon may be downloading dependencies. Let me check its log:
Assistant
The build is actively downloading dependencies (network-bound, hence idle CPU). It's progressing normally. Waiting for the gradle exit monitor to fire.
Assistant
Awaiting the monitor event.
Assistant
Awaiting the monitor event.
Assistant
Awaiting the monitor event — no action needed until gradle exits.
Assistant
Progress: 9 modules built now (up from 4). Continuing to wait for the run to complete.
Assistant
Still compiling mid-chain modules. Waiting.
Assistant
Awaiting the monitor event.
Assistant
Awaiting the monitor event.
Assistant
Downloads continuing. Waiting for the exit monitor.
Assistant
Awaiting the monitor event.
Assistant
Still in configuration phase. Waiting.
Assistant
Awaiting the monitor event.
Assistant
Still hashing dependencies in configuration. Waiting.
Assistant
Awaiting the monitor event.
Assistant
The gradle run completed successfully. Let me verify the test actually ran and passed:
Assistant
The fix is complete and verified.
Summary
Bug: WebSocket ReaderHandler returned HTTP 500 for an invalid messageId query parameter instead of 400.
Root causes (both fixed in pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java):
getMessageId() passed the raw query param straight to Base64.getDecoder().decode() and MessageIdImpl.fromByteArray(). It now catches IllegalArgumentException (bad base64, e.g. a + decoded as a space) and IOException (base64-valid but not a serialized MessageId) and rethrows both as IllegalArgumentException("Invalid messageId query param: …") — see ReaderHandler.java:388.
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!
Interface
No new interfaces are introduced.
The constructor's error path hardcoded SC_INTERNAL_SERVER_ERROR. It now uses getErrorCode(e) / getErrorMessage(e) from AbstractWebSocketHandler (ReaderHandler.java:137), matching ConsumerHandler and ProducerHandler — so IllegalArgumentException maps to 400 Bad Request, and other known exceptions (auth, timeout, not-found, etc.) also get their correct codes instead of a blanket 500.
Test: added invalidMessageIdReaderTest to pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyPublishConsumeTest.java, covering both reproduction cases from the issue:
messageId=invalidMessageId (decodes as base64, fails MessageIdImpl.fromByteArray)
messageId=CJCj6QEQ%20UQwAA== (space from a mis-URL-encoded +, fails base64 decode)
Both assert the WebSocket upgrade is rejected with SC_BAD_REQUEST.
Verification:./gradlew :pulsar-broker:test --tests "…ProxyPublishConsumeTest.invalidMessageIdReaderTest" — BUILD SUCCESSFUL; result XML shows tests="1" failures="0" errors="0".