rpc: require a hex string for OP_RETURN data #36978

pull l0rinc wants to merge 1 commits into bitcoin:master from l0rinc:l0rinc/rpc-data-requires-hex-string changing 2 files +3 −2
  1. l0rinc commented at 12:25 PM on November 25, 2026: contributor

    Problem: createrawtransaction accepts a JSON number as data when its digits form valid hex. For example, it treats 99 as the string "99" and writes the byte 0x99 to the output.

    Fix: Pass the original JSON value to the hex parser so it can enforce the documented string type.

  2. rpc: require a hex string for OP_RETURN data
    Pass the original JSON value to ParseHexV so it can check the type before decoding the data. Extend the existing RPC test to reject numeric 99 alongside the existing coverage for the string "99".
    f0518a30af
  3. DrahtBot added the label RPC/REST/ZMQ on Nov 25, 2026
  4. DrahtBot commented at 12:25 PM on November 25, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK maflcko, stickies-v

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. maflcko commented at 12:45 PM on November 25, 2026: member

    lgtm ACK f0518a30afb2b5a6c25525f5b624b203374f9eb9

    This typo also caused a full copy of the hex string again (not that it matters)

  6. in src/rpc/rawtransaction_util.cpp:128 in f0518a30af
     124 | @@ -125,7 +125,7 @@ std::vector<std::pair<CTxDestination, CAmount>> ParseOutputs(const UniValue& out
     125 |                  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
     126 |              }
     127 |              has_data = true;
     128 | -            std::vector<unsigned char> data = ParseHexV(value.getValStr(), "Data");
     129 | +            std::vector<unsigned char> data = ParseHexV(value, "Data");
    


    stickies-v commented at 4:23 PM on November 25, 2026:

    For a non-string type, this now fails with Data must be hexadecimal string (not ''). Seems better to align ParseHexV with ParseHashV and more explicitly fail when an incorrect type is passed?

    <details> <summary>git diff on f0518a30af</summary>

    diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
    index e2cf363a97..b4c8da9dd9 100644
    --- a/src/rpc/util.cpp
    +++ b/src/rpc/util.cpp
    @@ -135,9 +135,7 @@ uint256 ParseHashO(const UniValue& o, std::string_view strKey)
     }
     std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name)
     {
    -    std::string strHex;
    -    if (v.isStr())
    -        strHex = v.get_str();
    +    const std::string& strHex{v.get_str()};
         if (!IsHex(strHex))
             throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
         return ParseHex(strHex);
    diff --git a/src/test/fuzz/parse_univalue.cpp b/src/test/fuzz/parse_univalue.cpp
    index 1f26f903a8..6f8667dc4e 100644
    --- a/src/test/fuzz/parse_univalue.cpp
    +++ b/src/test/fuzz/parse_univalue.cpp
    @@ -54,10 +54,12 @@ FUZZ_TARGET(parse_univalue, .init = initialize_parse_univalue)
         try {
             (void)ParseHexO(univalue, "A");
         } catch (const UniValue&) {
    +    } catch (const std::runtime_error&) {
         }
         try {
             (void)ParseHexO(univalue, random_string);
         } catch (const UniValue&) {
    +    } catch (const std::runtime_error&) {
         }
         try {
             (void)ParseHexV(univalue, "A");
    diff --git a/test/functional/rpc_rawtransaction.py b/test/functional/rpc_rawtransaction.py
    index a6e5a90bd1..d88729c62b 100755
    --- a/test/functional/rpc_rawtransaction.py
    +++ b/test/functional/rpc_rawtransaction.py
    @@ -314,8 +314,8 @@ class RawTransactionsTest(BitcoinTestFramework):
             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=[])
    -        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(-8, "Data must be hexadecimal string (not 'foo')", self.nodes[0].createrawtransaction, [], {'data': 'foo'})
    +        assert_raises_rpc_error(-3, "JSON value of type number is not of expected type string", self.nodes[0].createrawtransaction, [], {'data': 99})
             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})
    
    

    </details>


    maflcko commented at 4:33 PM on November 25, 2026:

    I wondered about this too. I guess there is no caller that let's None->"" (empty string) -> b'' (empty hex)

    Maybe in a follow-up?

  7. stickies-v approved
  8. stickies-v commented at 4:27 PM on November 25, 2026: contributor

    ACK f0518a30afb2b5a6c25525f5b624b203374f9eb9


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-25 17:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me