Skip to content

Commit 316a0c5

Browse files
committed
rpc: addpeeraddress: throw on invalid IP
Throw RPC_CLIENT_INVALID_IP_OR_SUBNET when LookupHost(addr, false) fails in addpeeraddress. This aligns with setban/addconnection and avoids the opaque {"success": false} result for input errors. The JSON {success, error?} object remains for addrman outcomes only. Update test to match.
1 parent 74fa028 commit 316a0c5

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
@@ -1000,26 +1000,28 @@ static RPCHelpMan addpeeraddress()
10001000

10011001
UniValue obj(UniValue::VOBJ);
10021002
std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)};
1003+
if (!net_addr.has_value()) {
1004+
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Invalid IP address");
1005+
}
1006+
10031007
bool success{false};
10041008

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

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

test/functional/rpc_net.py

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

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

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

0 commit comments

Comments
 (0)