Repository navigation
Conversation
`ParseOutputs()` reads transaction outputs from the parallel key and value vectors stored by `UniValue`. A failed parse can leave an object partially populated, which the RPC fuzz target passes to this code. Reject objects whose key and value counts differ before iterating over them. Add unit coverage for the mismatch and the existing duplicate-output errors.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36086. ReviewsSee the guideline and AI policy for information on the review process. |
| bool has_data{false}; | ||
| const auto& keys{outputs.getKeys()}; | ||
| const auto& values{outputs.getValues()}; | ||
| if (keys.size() != values.size()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output key and value counts do not match"); |
There was a problem hiding this comment.
nit: Since the line is long and the style in the function is to use multiple lines for other errors:
| if (keys.size() != values.size()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output key and value counts do not match"); | |
| if (keys.size() != values.size()) { | |
| throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output key and value counts do not match"); | |
| } |
| BOOST_AUTO_TEST_CASE(parse_outputs_invalid) | ||
| { | ||
| UniValue mismatched; | ||
| BOOST_REQUIRE(!mismatched.read(R"({"":)")); |
There was a problem hiding this comment.
It seems like the actual RPC subsystem does not allow invalid JSON through:
Lines 233 to 237 in 0311751
This has me think that it is the rpc fuzz target which should be patched once instead of having to harden all RPC implementations against invalid JSON just to serve the fuzz target rather than the real use-case.
| bool has_data{false}; | ||
| const auto& keys{outputs.getKeys()}; | ||
| const auto& values{outputs.getValues()}; | ||
| if (keys.size() != values.size()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output key and value counts do not match"); |
There was a problem hiding this comment.
Seems fine to add this, but I am generally not a fan of having to fix classes of bugs one-by-one in the code manually in every instance. Those instances are basically impossible to enumerate or review.
It would be better to just avoid the whole class of bugs wholesale in one go.
Also, this is a fuzz-test-only code path, so it seems a bit odd to mix test-only code with real code.
Alternative in #36088
jeanpablojp
left a comment
There was a problem hiding this comment.
To me #36088 fixes this at the right level, in UniValue, rather than hardening each caller.
Ran this one with #36088 on top. The failed read() now leaves null rather than a mismatched object, so ParseOutputs throws type_error from getKeys() before the new check, and the first assertion in parse_outputs_invalid dies on it.
The other two pass with the first one dropped, and they add unit coverage for paths only the functional tests reach today.
|
Consensus here seems to be that #36088 is a better direction/fix, so I'm going to close this for now, in favour of that PR. |
Problem: Before #36032, a partially parsed
UniValue(with a valid output key but no value) reached a checked lookup and threw an exception.We changed this to unchecked indexing there, which aborted the
rpcfuzz target instead of throwing.The triggering qa-assets input landed after the PR's CI so it only failed post-merge.
Fix: Reject mismatched output key and value counts before indexing them, with additional unit coverage for malformed and duplicate outputs.