Merge bitcoin/bitcoin#36978: rpc: require a hex string for OP_RETURN data
What changed, and why it matters
This commit fixes a small but real type-checking bug in Bitcoin Core's `createrawtransaction` RPC command. Previously, if a user accidentally passed a JSON number where a hex string was expected for OP_RETURN data, the command could silently accept it and write unintended bytes into the transaction. The fix forces the RPC to reject non-string values, matching the documented behavior. It is not a critical money-stealing bug, but it removes a foot-gun that could surprise developers or tools building transactions.
No urgent action required beyond normal patching. Node operators and developers using `createrawtransaction` with `data` outputs should ensure they pass hex strings, not numbers, after upgrading. Review any tooling that may have relied on the accidental numeric acceptance.
Security signals we found
Type confusion between JSON number and hex string in RPC input parsing
Silent acceptance of unintended byte values in OP_RETURN outputs
Input validation strengthened to match documented API contract
Regression test added for numeric input rejection
Evidence from the diff
In ParseOutputs inside src/rpc/rawtransaction_util.cpp, the code previously called value.getValStr() to obtain the OP_RETURN data field, then passed that string to ParseHexV. Because UniValue::getValStr() returns the raw textual representation of whatever JSON value was present, a JSON number such as 99 would be treated as the two-character string "99" and parsed as the single byte 0x99. The patch passes the original UniValue object value directly to ParseHexV, which enforces that the JSON type is a string. A regression test is added covering both the existing invalid hex string 'foo' and the new invalid JSON number 99.
Changed components
src/rpc/rawtransaction_util.cpptest/functional/rpc_rawtransaction.pycreaterawtransaction RPCInspect captured patch +3 / −2
### src/rpc/rawtransaction_util.cpp
@@ -125,7 +125,7 @@ std::vector<std::pair<CTxDestination, CAmount>> ParseOutputs(const UniValue& out
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
}
has_data = true;
- std::vector<unsigned char> data = ParseHexV(value.getValStr(), "Data");
+ std::vector<unsigned char> data = ParseHexV(value, "Data");
CTxDestination destination{CNoDestination{CScript() << OP_RETURN << data}};
CAmount amount{0};
parsed_outputs.emplace_back(destination, amount);
### test/functional/rpc_rawtransaction.py
@@ -314,7 +314,8 @@ def createrawtransaction_tests(self):
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type array", self.nodes[0].createrawtransaction, [], 'foo')
self.nodes[0].createrawtransaction(inputs=[], outputs={}) # Should not throw for backwards compatibility
self.nodes[0].createrawtransaction(inputs=[], outputs=[])
- assert_raises_rpc_error(-8, "Data must be hexadecimal string", self.nodes[0].createrawtransaction, [], {'data': 'foo'})
+ for invalid_data in ('foo', 99):
+ assert_raises_rpc_error(-8, "Data must be hexadecimal string", self.nodes[0].createrawtransaction, [], {'data': invalid_data})
assert_raises_rpc_error(-5, "Invalid Bitcoin address", self.nodes[0].createrawtransaction, [], {'foo': 0})
assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].createrawtransaction, [], {address: 'foo'})
assert_raises_rpc_error(-3, "Amount out of range", self.nodes[0].createrawtransaction, [], {address: -1})Why this scored 34/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.