Repository navigation
fix(jsonrpc): reject invalid UTF-8 and trailing request content - #7103
Open
waynercheung wants to merge 2 commits into
Open
waynercheung wants to merge 2 commits into
waynercheung wants to merge 2 commits into
Conversation
Check that the request body is well-formed UTF-8 before parsing it. Jackson accepts overlong encodings, UTF-16 surrogates and code points above U+10FFFF, none of which are valid UTF-8 (RFC 3629). A body that is not well-formed UTF-8 now gets a -32700 parse error with a null id, and nothing is dispatched. This applies to single, batch and notification requests alike. Compatibility: well-formed UTF-8, including a byte order mark and non-ASCII text in parameters, is unchanged. Encoding detection is kept only for bodies that are also well-formed UTF-8, so an ASCII body sent as UTF-16 without a byte order mark is still accepted. A UTF-16 or UTF-32 body whose raw bytes are not well-formed UTF-8, such as one that starts with a byte order mark, now gets -32700.
Parse the request body with FAIL_ON_TRAILING_TOKENS so that it must
hold exactly one JSON value. Previously the node answered the first
value and silently dropped anything after it, so a client that sent
two requests received one response with no error.
Anything other than whitespace after the first value now gets a -32700
parse error with a null id, and nothing is dispatched. Trailing
whitespace is still accepted. Only the HTTP JSON-RPC servlet is
changed; the shared mapper is not, and the admin endpoints already
reject trailing content.
BREAKING CHANGE: a body such as `{...} {...}` or `{...}}` used to
return the result of the first request and now returns -32700.
Clients that send several requests in one body must send them as a
batch array instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Tightens how the JSON-RPC HTTP servlet (
JsonRpcServlet, also used by the solidity and PBFT JSON-RPC services) accepts a request body. Before any request is dispatched, it now rejects two kinds of body with HTTP 200 and{"jsonrpc":"2.0","error":{"code":-32700,"message":"JSON parse error"},"id":null}:Utf8.isWellFormedbefore parsing. Jackson accepts overlong encodings, UTF-16 surrogates and code points above U+10FFFF, none of which are valid UTF-8 (RFC 3629).ObjectReaderthat enablesDeserializationFeature.FAIL_ON_TRAILING_TOKENS.Both checks apply to single, batch and notification requests. The feature is enabled only on the servlet's own reader, so the shared
JsonRpcMapperand the admin endpoints do not change. The admin endpoints already apply both checks.The PR has two commits, one per check, and each commit has its own tests.
Why are these changes required?
{...} {...}got one response and no error. go-ethereum (v1.17.6 and later) and Besu both reject such a body with -32700.This PR has been tested by:
JsonRpcServletTestwith a realJsonRpcServer. Each rejected body asserts the full -32700 envelope with a null id, and asserts that neither the server nor the service method was called.}or], a notification pair, and1 2.\uescapes, a UTF-8 BOM, trailing space/tab/CR/LF, and BOM-less UTF-16 whose raw bytes are also well-formed UTF-8 (ASCII text, and U+4E2D).JsonRpcServletJettyTest: posts raw bytes through embedded Jetty and the real filter chain, one invalid UTF-8 body and one trailing-content body../gradlew test: the only failures were 7 inInetUtilTest(DNS resolution timeouts), and they fail the same way without this change.-x :framework:generateGitProperties, because that task fails in a linked git worktree (JGit cannot resolve the repository)../gradlew clean build -x test, the Checkstyle tasks frompr-check.ymlandcheck_math_usage.shpassed.Follow up
None.
Extra details
Behavior changes (release note)
{...} {...},{...} xyz,{...}},[...] {...}1 2,"x" {Suggested release note: The JSON-RPC HTTP endpoint now rejects, with a -32700 parse error, a request body that contains anything other than whitespace after the first JSON value; clients that send several requests in one body must send a batch array instead. It also rejects, with a -32700 parse error, request bodies that are not well-formed UTF-8.
Out of scope: the HTTP status code (stays 200, as for every other -32700), NUL bytes, and Jackson's encoding detection for bodies that are also well-formed UTF-8.