rpc: reject empty node argument in addnode

An empty (or whitespace only) node address cannot be resolved, but would
be added to the added nodes list and retried indefinitely, the same way
that an empty -addnode value was before the previous commits.

Reject it for all commands instead. Returning false from AddNode() would
report the misleading "Node already added" error, so check it here.
This commit is contained in:
w0xlt
2026-07-26 00:18:42 -07:00
parent 69465de447
commit 90ce21e21d
2 changed files with 11 additions and 0 deletions

View File

@@ -47,6 +47,7 @@
using node::NodeContext;
using util::Join;
using util::TrimStringView;
const std::vector<std::string> CONNECTION_TYPE_DOC{
"outbound-full-relay (default automatic connections)",
@@ -348,6 +349,11 @@ static RPCMethod addnode()
CConnman& connman = EnsureConnman(node);
const auto node_arg{self.Arg<std::string_view>("node")};
if (TrimStringView(node_arg).empty()) {
// Such a node would never resolve, but would be retried indefinitely.
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Node address cannot be empty");
}
bool node_v2transport = connman.GetLocalServices() & NODE_P2P_V2;
bool use_v2transport = self.MaybeArg<bool>("v2transport").value_or(node_v2transport);

View File

@@ -272,6 +272,11 @@ class NetTest(BitcoinTestFramework):
assert_equal(added_nodes[0]['addednode'], "11.22.33.44")
self.log.info("Check that an invalid command returns an error")
assert_raises_rpc_error(-1, 'addnode "node" "command"', self.nodes[0].addnode, node=ip_port, command='abc')
self.log.info("Check that an empty node address returns an error")
for command in ['add', 'remove', 'onetry']:
assert_raises_rpc_error(-8, "Node address cannot be empty", self.nodes[0].addnode, node="", command=command)
assert_raises_rpc_error(-8, "Node address cannot be empty", self.nodes[0].addnode, node=" ", command=command)
assert_equal(len(self.nodes[0].getaddednodeinfo()), 1)
self.log.info("Check that trying to remove the node again returns an error")
assert_raises_rpc_error(-24, "Node could not be removed", self.nodes[0].addnode, node=ip_port, command='remove')
self.log.info("Check that a non-existent node returns an error")