mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-12 05:32:22 +02:00
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:
@@ -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);
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user