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 —
IOExceptionwrapping from : Assistant
I'll analyze the issue and fix it. Let me start by exploring the repository structure and finding the relevant files.
Assistant
Let me examine the ReaderHandler.java file to understand the current implementation and the issue.
Assistant
Now let me examine the ConsumerHandler and ProducerHandler to see how they handle errors, as the issue mentions they use
AbstractWebSocketHandler.getErrorCode(e)to properly map exceptions to HTTP status codes.<tool_call> <function=bash_terminal> <parameter=command> find /pulsar -type f -name "ConsumerHandler.java" 2>/dev/null | head -5 </tool_call>
Assistant
Let me try to find the ConsumerHandler.java file to see how it handles errors.
Assistant
Let me view the ConsumerHandler.java file to see how it handles errors and uses getErrorCode.
Assistant
Now let me view the ProducerHandler.java to see how it handles errors.
<tool_call> <function=bash_terminal> <parameter=command> find /pulsar -type f -name "ProducerHandler.java" 2>/dev/null | head -5 </tool_call>
Assistant
Let me try again with the find command for ProducerHandler.
Assistant
Let me view the ProducerHandler.java file to see how it handles errors.
Assistant
Now let me view the AbstractWebSocketHandler to understand the getErrorCode and getErrorMessage methods that ConsumerHandler and ProducerHandler use.
<tool_call> <function=bash_terminal> <parameter=command> find /pulsar -type f -name "AbstractWebSocketHandler.java" 2>/dev/null | head -5 </tool_call>
Assistant
I need to find the AbstractWebSocketHandler.java file to understand the getErrorCode method that ConsumerHandler and ProducerHandler use.
Assistant
Good, I found the file. Let me examine it to see the getErrorCode and getErrorMessage methods.
<tool_call> <function=bash_terminal> <parameter=command> cat /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/AbstractWebSocketHandler.java </tool_call>
Assistant
Let me view the AbstractWebSocketHandler.java file to understand how getErrorCode and getErrorMessage work.
Assistant
Now I understand the issue. Let me summarize:
-
The
ReaderHandlerclass catchesExceptionin its constructor and always returnsHttpServletResponse.SC_INTERNAL_SERVER_ERROR(500) for any error. -
ConsumerHandlerandProducerHandleruseAbstractWebSocketHandler.getErrorCode(e)which properly maps exceptions to HTTP status codes (e.g.,IllegalArgumentException→ 400 Bad Request). -
The issue is in the
getMessageId()method inReaderHandler.javaat line 348, where:- Invalid base64 characters (like
+being interpreted as space) throwIllegalArgumentExceptionfromBase64.getDecoder().decode() - Invalid messageId strings throw
IOExceptionwrappingIndexOutOfBoundsExceptionfromMessageIdImpl.fromByteArray()
- Invalid base64 characters (like
-
The fix should:
- Use
getErrorCode(e)andgetErrorMessage(e)from (similar to ConsumerHandler and ProducerHandler)
- Use
-
Assistant
Let me look for tests related to ReaderHandler to understand how they test this functionality.
Assistant
Good, let me look at the existing ReaderHandlerTest to see if there are already tests for messageId validation.
Assistant
Now I have a good understanding of the issue. Let me fix the
ReaderHandler.javafile to:- Use
getErrorCode(e)instead of hardcodingHttpServletResponse.SC_INTERNAL_SERVER_ERROR - Use
getErrorMessage(e)instead of hardcoding the error message - This is exactly what ConsumerHandler and ProducerHandler do
Let me apply the fix now.
<tool_call> <function=bash_terminal> <parameter=command> cat /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java | grep -n "sendError" </tool_call>
- Use
Assistant
Let me search for the sendError line in ReaderHandler.java to find the exact location of the problematic code.
Assistant
Now let me view the lines around line 138 to understand the exact context for the fix.
Assistant
Now I'll fix the
ReaderHandler.javafile to usegetErrorCode(e)andgetErrorMessage(e)instead of hardcoding the error status and message, just likeConsumerHandlerandProducerHandlerdo.Assistant
Now I'll apply the fix to use
getErrorCode(e)andgetErrorMessage(e)instead of hardcoding the error status and message.Assistant
It seems the edit command introduced an extra closing brace. Let me check the file around line 147-150 to see the issue and fix it.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '145,155p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me view the lines around 145-155 to see the issue.
Assistant
I see there's an extra closing brace. Let me fix that.
Assistant
Let me fix the extra closing brace issue.
Assistant
I see the problem. I need to look at the full context of the constructor to see the right structure. Let me view the constructor from the beginning to understand it better.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '67,150p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me view the constructor from line 67 to line 150 to understand the structure.
Assistant
I can see the issue now. The constructor has extra braces at lines 146-148. The structure should be:
} catch (Exception e) { ... } } // end of constructorBut currently it's:
} catch (Exception e) { ... } } // extra brace } // another extra braceLet me fix this by removing the extra closing braces at lines 146-148.
<tool_call> <function=bash_terminal> <parameter=command> edit --file /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java 145:149 << 'EOF' } } } EOF </tool_call>
Assistant
Let me view the file around lines 140-155 to see the exact structure and fix it properly.
Assistant
I see the problem now. The structure should be:
- Line 146:
}(closes the catch block for IOException e1) - Line 147:
}(closes the catch block for Exception e) - Line 148:
}(closes the constructor)
But I have:
- Line 146:
}(closes the catch block for IOException e1) - Line 147:
}(closes the catch block for Exception e) - Line 148:
}(extra closing brace - should not be here)
Let me fix this by replacing lines 146-148.
- Line 146:
Assistant
Let me view the complete constructor to understand the structure better. Let me use sed to delete lines 147-148 which contain the extra closing braces. <tool_call> <function=bash_terminal> <parameter=command> sed -i '147,148d' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me try again with the correct format - the command should be a single string parameter.
Assistant
The sed command executed successfully. Now let me verify that the structure is correct by viewing the lines around 140-150.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '137,155p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me view the lines around 137-155 to verify the structure is correct.
Assistant
I see I removed too many lines. I need to add back the closing braces for the catch block and constructor. Let me fix this.
Assistant
I need to add the closing braces for the catch block and constructor before the
receiveMessage()method. Let me insert the missing braces.Assistant
Let me check if the structure is correct now.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '137,155p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me view the lines to verify the structure.
Assistant
The structure now looks correct:
- Line 146:
}(closes the catch block for IOException e1) - Line 147:
}(closes the catch block for Exception e) - Line 148:
}(closes the constructor)
Now let me verify that the entire file compiles correctly by checking if it has any syntax errors. Let me view the entire constructor to make sure it's correct.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '80,150p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
- Line 146:
Assistant
Let me verify the constructor by viewing it from line 80 to 150 to ensure it's syntactically correct.
Assistant
The constructor now looks correct. The fix has been applied successfully. Now let me run the existing tests to make sure they still pass.
Assistant
Let me find the correct module name or run the test differently.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ls -la | grep pom.xml </tool_call>
Assistant
Let me look for the correct way to run tests in this project. I'll explore the project structure first.
Assistant
I see this is a Gradle project. Let me run the tests using Gradle.
Assistant
The tests pass. Now let me verify that my fix works for the reported issue. Let me create a simple test to verify that the fix properly handles invalid messageId parameters.
Assistant
Actually, instead of creating a new test file, let me check the existing test file and ensure the fix is correct by looking at the code. Let me also look at the getMessageId() method to understand the flow better.
Assistant
Let me view the getMessageId() method to understand it better.
Assistant
The getMessageId() method can throw:
IllegalArgumentExceptionfromBase64.getDecoder().decode()when the base64 string is invalidIOExceptionwrappingIndexOutOfBoundsExceptionfromMessageIdImpl.fromByteArray()when the decoded bytes don't form a valid MessageId
My fix is correct - now when either of these exceptions occurs, it will be caught by the try-catch block in the constructor, and the
getErrorCode(e)method fromAbstractWebSocketHandlerwill properly mapIllegalArgumentExceptionto HTTP 400 (Bad Request) instead of HTTP 500 (Internal Server Error).Let me verify that my changes compile and the existing tests still pass. I already ran the tests and they passed. Let me now verify the change one more time by viewing the diff.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git diff pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me check the git diff to see what changes I've made to the ReaderHandler.java file.
Assistant
The change looks correct. Now let me run the tests once more to make sure everything still works.
Assistant
The tests passed. Let me also verify that the code compiles without issues by running the build.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ./gradlew :pulsar-websocket:compileJava -q 2>&1 | tail -10 </tool_call>
Assistant
The command seems to have run without errors (no tail output shown). Let me verify by running it again with more visible output.
Assistant
Assistant
Let me view the constructor's error handling code to understand how to modify it properly for handling the IOException case.
Assistant
I need to handle the
IOExceptioncase specially. Let me modify the code to check if theIOExceptionis caused by invalid input (wrappingIndexOutOfBoundsException) and return 400 for that case.Assistant
Now I need to modify the error handling to check for
IOExceptionwrappingIndexOutOfBoundsExceptionand return 400 for that case. Let me replace the error handling code.Assistant
Let me apply this patch to the file. <tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && patch -p1 < /tmp/fix.patch </tool_call>
Assistant
Let me manually edit the file using the edit command to replace the error handling code.
Assistant
I see there's an issue - the closing brace for the constructor is missing. Let me check line 159-161 to see the structure. <tool_call> <function=bash_terminal> <parameter=command> sed -n '156,165p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me view lines 156-165 to check the structure.
Assistant
I need to add the missing closing brace for the constructor. Let me fix that.
Assistant
Let me verify the structure is correct now.
Assistant
The structure looks correct now. Let me verify that the code compiles and tests pass.
Assistant
The code compiles successfully. Now let me run the tests to make sure they still pass.
Assistant
Excellent! The tests pass. Now let me view the complete diff to see all the changes I've made.
Assistant
Let me verify that the code handles all edge cases correctly. Let me review the changes:
- The fix now checks if the exception is an
IOExceptionwrappingIndexOutOfBoundsException(which happens when invalid messageId bytes are provided) and returns HTTP 400 for that case. - For other exceptions, it uses
getErrorCode(e)andgetErrorMessage(e)fromAbstractWebSocketHandler, which properly mapsIllegalArgumentExceptionto HTTP 400.
This should handle both cases mentioned in the issue:
- Case 1: Invalid base64 character (throws
IllegalArgumentException) → returns 400 viagetErrorCode(e) - Case 2: Invalid messageId string (throws
IOExceptionwrappingIndexOutOfBoundsException) → returns 400 via the special check
Let me also verify that the code handles the edge case where the messageId is "earliest" or "latest" (which are valid values).
Let me view the getMessageId() method again to make sure it handles all edge cases. <tool_call> <function=bash_terminal> <parameter=command> sed -n '387,400p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
- The fix now checks if the exception is an
Assistant
Let me view the getMessageId() method to verify it handles all edge cases correctly.
Assistant
Let me view the getMessageId() method starting from line 398.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '398,410p' /pulsar/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
The command didn't seem to run. Let me try a simpler approach and just check the line count of the file to see if it's correct.
Assistant
The file now has 410 lines (was 400 before, increased by 10 lines for the new error handling code). Let me check if there are any issues by viewing the end of the file.
Assistant
Let me run all the tests in the pulsar-websocket module to ensure the changes don't break anything.
Assistant
There's a failing test. Let me check the test report to see which test failed and why.
Assistant
The failing test is
PingPongSupportTest.testPingPong[2](producer)and it's failing due to "404 Not Found" when trying to upgrade to websocket. This test failure appears to be unrelated to my changes (it's a network/binding issue - "Failed to bind to 0.0.0.0/0.0.0.0:8080"). Let me run only the ReaderHandlerTest to verify my changes are correct.Assistant
The ReaderHandlerTest passes. The failing test
PingPongSupportTestappears to be a pre-existing issue related to network binding (port 8080 is already in use or not available). Let me verify this by checking if this test was passing before my changes by reverting the changes temporarily and running the test.Actually, let me just check the git status to see if the failing test is related to my changes.
Assistant
Let me check if the PingPongSupportTest failure is a pre-existing issue by checking if it fails without my changes.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git stash </tool_call>
Assistant
Let me stash the changes to verify if the failing test was already failing before my changes.
Assistant
Let me run the PingPongSupportTest without my changes to verify if it was already failing.
Assistant
This confirms that the
PingPongSupportTestfailure is a pre-existing issue and not related to my changes. The test was already failing before my changes due to network binding issues (port 8080 is not available). Let me restore my changes.<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git stash pop </tool_call>
Assistant
Good, I've verified that the failing test is a pre-existing issue. Now let me restore my changes.
Assistant
Good, my changes are back. Let me now verify that the ReaderHandlerTest still passes with my changes.
Assistant
Perfect! The ReaderHandlerTest passes. Now let me create a summary of my changes and verify the fix one more time by viewing the diff. Let me view the final diff to confirm the changes are correct.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git diff pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ReaderHandler.java </tool_call>
Assistant
Let me run the git diff command to see the changes I made to the ReaderHandler.java file.
Assistant
The changes look correct. Now let me submit the solution. <tool_call> <function=submit> </tool_call>
Assistant
The submit command needs to be run. Let me try again with the correct format.
Assistant
The submit command seems to have an issue. Let me try with the full command string format. <tool_call> <function=bash_terminal> <parameter=command> submit </tool_call>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.