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():codeWARN 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
I’ll locate the WebSocket reader implementation and its related tests, compare error handling with producer/consumer handlers, then reproduce the behavior through the smallest existing test path before making a focused fix.
- Assistant
The relevant module and test class are present. I’ll inspect the constructor catch block, message-ID parser, shared status mapping, and current reader tests to determine whether malformed decoded bytes need explicit normalization in addition to replacing the hardcoded 500.
- Assistant
The parser can fail in two distinct ways: invalid Base64 already yields
IllegalArgumentException, while structurally invalid decoded bytes yieldIOException. Since the shared mapper likely treats only client argument errors as 400, I need the complete constructor and mapper semantics before choosing whether to wrap parser failures asIllegalArgumentException. - Assistant
Simply calling
getErrorCode(e)would fix malformed Base64 but not decoded garbage, becauseMessageIdImpl.fromByteArraywraps structural parse failures inIOException. The robust fix is to classify all failures while decoding the user-suppliedmessageIdasIllegalArgumentException, then use the shared error-code/message mapping in the reader catch block. I’ll check style and APIs, then implement only production changes as required (without modifying tests). - Assistant
I’ll now normalize both Base64 syntax failures and message-ID binary parsing failures into an argument error, and delegate the response status/text to the same shared helpers used by the other WebSocket handlers. This preserves 500 responses for genuine reader/client failures while returning 400 specifically for invalid query input.
- Assistant
Python is unavailable in this environment, so no file was changed. I’ll apply the same focused edit using the repository editor commands and immediately inspect the diff.
- Assistant
The editor found both blocks but requires them to be in its displayed window. I’ll navigate to each occurrence and apply the replacements there.
- Assistant
The diff is clean and retains
HttpServletResponseusage elsewhere in the class. Before running the module tests, I’ll inspect request parsing and the module’s Maven configuration so I can exercise both reported inputs throughReaderHandleritself with a temporary, non-committed test harness if practical. - Assistant
HttpServletResponsebecame unused after removing the hardcoded status and must be deleted. The module is likely nested under a packaging directory despite source paths being rooted here; I’ll locate its POM, remove the import, and then run the targeted test class. - Assistant
No standalone POM was found, indicating Maven builds this source through an aggregator module. I’ll remove the import from its displayed location and identify the owning module via root POM/source references.
- Assistant
This checkout appears to be a sparse task workspace without POM files at the current level. I’ll inventory the root and available build metadata/scripts to determine the supported validation command rather than assuming Maven.
- Assistant
The project has migrated to Gradle. I’ll identify the exact WebSocket project path and available test tasks, then run the targeted
ReaderHandlerTestrather than the full multi-module suite. - Assistant
The owning task is
:pulsar-websocket:test. I’ll run its existing reader-handler tests first; this validates compilation, static checks attached to the test lifecycle, and unaffected reader behavior. - Assistant
The targeted tests pass and the modified class compiles. I’ll now run the complete WebSocket module test suite to cover servlet/upgrade interactions and shared error mapping, then inspect formatting/check tasks and the final diff.
- Assistant
The complete module suite reached all compilation successfully but had one unrelated integration failure in
PingPongSupportTest(producer WebSocket endpoint returned 404); the focused reader tests remain green. To verify the two reported malformed values directly without adding or changing repository tests, I’ll build a temporary/tmpJava harness against the module’s test runtime classpath. It will instantiateReaderHandlerwith Mockito and assert that the upgrade response receives status 400 for each value. - Assistant
The temporary Gradle task referenced
sourceSetsfrom task scope rather than project scope, so it failed without touching the repository. I’ll correct that helper and proceed with the external harness. - Assistant
Configuration-cache restrictions blocked project access at task execution. I’ll disable the cache for this one diagnostic task (or capture the classpath at configuration time) and continue; this is unrelated to production code.
- Assistant
With the runtime classpath available, I’ll compile and run the external harness for both examples. It only creates files under
/tmp, and checks the actualReaderHandlerresponse call rather than testing the parser in isolation. - Assistant
The external end-to-end harness verified HTTP 400 for both the space-corrupted Base64 value and the arbitrary decoded garbage, including the exact underlying exceptions from the report. I’ll now run the module’s available style/static validation, review the final diff and status, and ensure no temporary files exist in the repository.
- Assistant
The final diff is limited to one production file and is whitespace-clean. I’ll run
spotlessJavaCheckandcheckstyleMainfor the module; these avoid rerunning the unrelated flaky integration test while fully validating the changed source. - Assistant
Implementation and verification are complete.
Checklist:
- If an error was reported, I successfully reproduced it. An external
/tmpharness exercisedReaderHandlerwith both reported values and observed the same underlying Base64IllegalArgumentExceptionand message-ID parsingIOExceptionpaths. - If an error was reported, I confirmed the fix resolves it. The harness verified
JettyServerUpgradeResponse.sendError(400, ...)for bothCJCj6QEQ UQwAA==andinvalidMessageId. - I thought about edge cases. Empty/blank IDs and
lateststill selectMessageId.latest;earliestremains unchanged; valid encoded IDs still parse normally; both invalid Base64 and valid-Base64-but-invalid-binary inputs are now classified as bad query parameters. Genuine reader creation failures continue through the shared status mapper and can remain 500.
- If an error was reported, I successfully reproduced it. An external
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.