Skip to content

Commit 65e909d

Browse files
committed
Merge bitcoin/bitcoin#33430: rpc: addpeeraddress: throw on invalid IP
316a0c5 rpc: addpeeraddress: throw on invalid IP (John Moffett) Pull request description: Right now we return an opaque `{"success" : false}` in `addpeeraddress` for an empty or invalid IP. This changes it to throw `RPC_CLIENT_INVALID_IP_OR_SUBNET` with the error message `Invalid IP address`. Tests updated to match. ACKs for top commit: sipa: utACK 316a0c5 achow101: ACK 316a0c5 vasild: ACK 316a0c5 pablomartin4btc: tACK 316a0c5 Tree-SHA512: 79a8ce127d0a24b2eb1f31bc3294b895d0c6424032a6b49168259e0e94aff69723d067adf1b4dc3c9b79e597531e5b65e4b8fc5a8e21fba0b81f99168de12b96
2 parents 31b29f8 + 316a0c5 commit 65e909d

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/rpc/net.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,26 +1004,28 @@ static RPCHelpMan addpeeraddress()
10041004

10051005
UniValue obj(UniValue::VOBJ);
10061006
std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)};
1007+
if (!net_addr.has_value()) {
1008+
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Invalid IP address");
1009+
}
1010+
10071011
bool success{false};
10081012

1009-
if (net_addr.has_value()) {
1010-
CService service{net_addr.value(), port};
1011-
CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
1012-
address.nTime = Now<NodeSeconds>();
1013-
// The source address is set equal to the address. This is equivalent to the peer
1014-
// announcing itself.
1015-
if (addrman.Add({address}, address)) {
1016-
success = true;
1017-
if (tried) {
1018-
// Attempt to move the address to the tried addresses table.
1019-
if (!addrman.Good(address)) {
1020-
success = false;
1021-
obj.pushKV("error", "failed-adding-to-tried");
1022-
}
1013+
CService service{net_addr.value(), port};
1014+
CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
1015+
address.nTime = Now<NodeSeconds>();
1016+
// The source address is set equal to the address. This is equivalent to the peer
1017+
// announcing itself.
1018+
if (addrman.Add({address}, address)) {
1019+
success = true;
1020+
if (tried) {
1021+
// Attempt to move the address to the tried addresses table.
1022+
if (!addrman.Good(address)) {
1023+
success = false;
1024+
obj.pushKV("error", "failed-adding-to-tried");
10231025
}
1024-
} else {
1025-
obj.pushKV("error", "failed-adding-to-new");
10261026
}
1027+
} else {
1028+
obj.pushKV("error", "failed-adding-to-new");
10271029
}
10281030

10291031
obj.pushKV("success", success);

test/functional/rpc_net.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,12 @@ def test_addpeeraddress(self):
346346
assert "unknown command: addpeeraddress" not in node.help("addpeeraddress")
347347

348348
self.log.debug("Test that adding an empty address fails")
349-
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
349+
assert_raises_rpc_error(-30, "Invalid IP address", node.addpeeraddress, address="", port=8333)
350350
assert_equal(node.getnodeaddresses(count=0), [])
351351

352+
self.log.debug("Test that adding a non-IP/hostname fails (no DNS lookup allowed)")
353+
assert_raises_rpc_error(-30, "Invalid IP address", node.addpeeraddress, address="not_an_ip", port=8333)
354+
352355
self.log.debug("Test that non-bool tried fails")
353356
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)
354357

0 commit comments

Comments
 (0)