rpc: require a hex string for OP_RETURN data #36978

Manually merged
l0rinc merged 1 commit from l0rinc/rpc-data-requires-hex-string into master 2026-12-03 12:58:12 +00:00 AGit
l0rinc commented 2026-11-25 12:25:24 +00:00 (Migrated from github.com)

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.

**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.
DrahtBot commented 2026-11-25 12:25:34 +00:00 (Migrated from github.com)

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

External sites

Reviews

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

Type Reviewers
ACK maflcko, stickies-v, rkrux

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36990 (test: replace hard-coded RPC error codes by l0rinc)
  • #35302 (Silent Payments: Sending (take 2) by Eunovo)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36978 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36978.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36978#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [maflcko](https://github.com/bitcoin/bitcoin/pull/36978#issuecomment-7162803443), [stickies-v](https://github.com/bitcoin/bitcoin/pull/36978#pullrequestreview-6061095832), [rkrux](https://github.com/bitcoin/bitcoin/pull/36978#pullrequestreview-6170046615) | 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. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36990](https://github.com/bitcoin/bitcoin/pull/36990) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36990.svg"></sub> (test: replace hard-coded RPC error codes by l0rinc) * [#35302](https://github.com/bitcoin/bitcoin/pull/35302) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35302.svg"></sub> (Silent Payments: Sending (take 2) by Eunovo) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
maflcko commented 2026-11-25 12:45:04 +00:00 (Migrated from github.com)

lgtm ACK f0518a30af

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

lgtm ACK f0518a30afb2b5a6c25525f5b624b203374f9eb9 This typo also caused a full copy of the hex string again (not that it matters)
stickies-v (Migrated from github.com) approved these changes 2026-11-25 16:27:55 +00:00
stickies-v (Migrated from github.com) left a comment
ACK f0518a30afb2b5a6c25525f5b624b203374f9eb9
stickies-v (Migrated from github.com) commented 2026-11-25 16:23:42 +00:00

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?

git diff on f0518a30af
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})

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 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 (Migrated from github.com) reviewed 2026-11-25 16:33:51 +00:00
maflcko (Migrated from github.com) commented 2026-11-25 16:33:51 +00:00

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?

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?
First-time contributor

🔵 Suggestion · Numeric data errors display an empty value

A numeric data value such as 99 is correctly rejected, but the diagnostic Data must be hexadecimal string (not '') misleadingly displays an empty string instead of identifying the wrong JSON type.

Location: src/rpc/rawtransaction_util.cpp:128 (head).

🔵 Suggestion · Numeric data rejection has no RPC release note

RPCs now reject numeric data previously accepted as hex, despite the existing string-only documentation; a short release note would explain the change, although no current client relying on numeric input was found.

Location: src/rpc/rawtransaction_util.cpp:128 (head).


Reviewed: f0518a30afb2 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:806938d40e279b6d701b2e78128bb1a09b55193a0b697245a1dce6241d3081c1:3607c760b63c01fb7f1f349adfc3fd0614b148a7df02e42f2f0edc2d448925a1 --> <!-- ralph-base:5ba05739881855cba25c3c05d6f82a61b8a0665f --> <!-- ralph-head:f0518a30afb2b5a6c25525f5b624b203374f9eb9 --> <!-- ralph-case:59e3a50a107e29fc5a5d105ab77c1327f7105796f751a0958384bcf099f0a8d2 --> **🔵 Suggestion · Numeric `data` errors display an empty value** A numeric `data` value such as `99` is correctly rejected, but the diagnostic `Data must be hexadecimal string (not '')` misleadingly displays an empty string instead of identifying the wrong JSON type. Location: [`src/rpc/rawtransaction_util.cpp:128`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0518a30afb2b5a6c25525f5b624b203374f9eb9/src/rpc/rawtransaction_util.cpp#L128>) (head). **🔵 Suggestion · Numeric `data` rejection has no RPC release note** RPCs now reject numeric `data` previously accepted as hex, despite the existing string-only documentation; a short release note would explain the change, although no current client relying on numeric input was found. Location: [`src/rpc/rawtransaction_util.cpp:128`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0518a30afb2b5a6c25525f5b624b203374f9eb9/src/rpc/rawtransaction_util.cpp#L128>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`f0518a30afb2`](<https://git.fish.foo/bitcoin/bitcoin/commit/f0518a30afb2b5a6c25525f5b624b203374f9eb9>) · [Review report](<https://review.fish.foo/report/36978-f0518a30afb2b5a6c25525f5b624b203374f9eb9-3116-1.html>) · [Previous reports](<https://review.fish.foo/traces/36978/>) [permalink](<https://review.fish.foo/report/36978-f0518a30afb2b5a6c25525f5b624b203374f9eb9-3116-1.html>)
rkrux (Migrated from github.com) approved these changes 2026-12-03 12:44:13 +00:00
rkrux (Migrated from github.com) left a comment

lgtm ACK f0518a30af

lgtm ACK f0518a30afb2b5a6c25525f5b624b203374f9eb9
maflcko commented 2026-12-03 12:45:06 +00:00 (Migrated from github.com)

rfm?

rfm?
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36978
No description provided.