Skip to content

rpc: validate output key and value counts - #36086

Closed
l0rinc wants to merge 1 commit into
bitcoin:masterfrom
l0rinc:l0rinc/fix-parseoutputs-fuzz-regression
Closed

l0rinc wants to merge 1 commit into
bitcoin:masterfrom
l0rinc:l0rinc/fix-parseoutputs-fuzz-regression

Conversation

@l0rinc

@l0rinc l0rinc commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

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 rpc fuzz 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.

`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.
@DrahtBot

DrahtBot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36086.

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since the line is long and the style in the function is to use multiple lines for other errors:

Suggested change
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");
}

Comment thread src/test/rpc_tests.cpp
BOOST_AUTO_TEST_CASE(parse_outputs_invalid)
{
UniValue mismatched;
BOOST_REQUIRE(!mismatched.read(R"({"":)"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the actual RPC subsystem does not allow invalid JSON through:

bitcoin/src/httprpc.cpp

Lines 233 to 237 in 0311751

if (request.read(req->ReadBody())) {
reply = ExecuteHTTPRPC(request, jreq, status);
} else {
reply = JSONErrorReply(JSONRPCError(RPC_PARSE_ERROR, "Parse error"), jreq, status);
}

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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fanquake

Copy link
Copy Markdown
Member

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.

@fanquake fanquake closed this Aug 26, 2026
@fanquake fanquake removed this from the 32.0 milestone Aug 26, 2026
@l0rinc
l0rinc deleted the l0rinc/fix-parseoutputs-fuzz-regression branch August 26, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants