Skip to content

Commit 3b5fb6e

Browse files
committed
Merge bitcoin#26213: rpc: Strict type checking for RPC boolean parameters
fa0153e refactor: Replace isTrue with get_bool (MarcoFalke) fa2cc5d bugfix: Strict type checking for RPC boolean parameters (MarcoFalke) Pull request description: ACKs for top commit: ryanofsky: Code review ACK fa0153e furszy: Code ACK fa0153e Tree-SHA512: b221f823c69d90c94447fd491071ff3659cfd512872b495ebc3e711f50633351974102c9ef7e50fa4a393c4131d349adea8fd41cc9d66f1f31e1f5e7a5f78757
2 parents 9e229a5 + fa0153e commit 3b5fb6e

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

doc/release-notes-26213.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Low-level changes
2+
=================
3+
4+
- Previously `setban`, `addpeeraddress`, `walletcreatefundedpsbt`, methods
5+
allowed non-boolean and non-null values to be passed as boolean parameters.
6+
Any string, number, array, or object value that was passed would be treated
7+
as false. After this change, passing any value except `true`, `false`, or
8+
`null` now triggers a JSON value is not of expected type error. (#26213)

src/rpc/net.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,7 @@ static RPCHelpMan setban()
731731
if (!request.params[2].isNull())
732732
banTime = request.params[2].getInt<int64_t>();
733733

734-
bool absolute = false;
735-
if (request.params[3].isTrue())
736-
absolute = true;
734+
const bool absolute{request.params[3].isNull() ? false : request.params[3].get_bool()};
737735

738736
if (isSubnet) {
739737
node.banman->Ban(subNet, banTime, absolute);
@@ -942,7 +940,7 @@ static RPCHelpMan addpeeraddress()
942940

943941
const std::string& addr_string{request.params[0].get_str()};
944942
const auto port{request.params[1].getInt<uint16_t>()};
945-
const bool tried{request.params[2].isTrue()};
943+
const bool tried{request.params[2].isNull() ? false : request.params[2].get_bool()};
946944

947945
UniValue obj(UniValue::VOBJ);
948946
CNetAddr net_addr;

src/rpc/rawtransaction.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static RPCHelpMan createrawtransaction()
304304

305305
std::optional<bool> rbf;
306306
if (!request.params[3].isNull()) {
307-
rbf = request.params[3].isTrue();
307+
rbf = request.params[3].get_bool();
308308
}
309309
CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], rbf);
310310

@@ -1449,7 +1449,7 @@ static RPCHelpMan createpsbt()
14491449

14501450
std::optional<bool> rbf;
14511451
if (!request.params[3].isNull()) {
1452-
rbf = request.params[3].isTrue();
1452+
rbf = request.params[3].get_bool();
14531453
}
14541454
CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], rbf);
14551455

src/wallet/rpc/spend.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -1651,11 +1651,8 @@ RPCHelpMan walletcreatefundedpsbt()
16511651

16521652
CAmount fee;
16531653
int change_position;
1654-
bool rbf{wallet.m_signal_rbf};
16551654
const UniValue &replaceable_arg = options["replaceable"];
1656-
if (!replaceable_arg.isNull()) {
1657-
rbf = replaceable_arg.isTrue();
1658-
}
1655+
const bool rbf{replaceable_arg.isNull() ? wallet.m_signal_rbf : replaceable_arg.get_bool()};
16591656
CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], rbf);
16601657
CCoinControl coin_control;
16611658
// Automatically select coins, unless at least one is manually selected. Can

test/functional/rpc_net.py

+3
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ def test_addpeeraddress(self):
307307
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
308308
assert_equal(node.getnodeaddresses(count=0), [])
309309

310+
self.log.debug("Test that non-bool tried fails")
311+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type bool", self.nodes[0].addpeeraddress, address="1.2.3.4", tried="True", port=1234)
312+
310313
self.log.debug("Test that adding an address with invalid port fails")
311314
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress, address="1.2.3.4", port=-1)
312315
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress,address="1.2.3.4", port=65536)

0 commit comments

Comments
 (0)