From d38e3aed89eea665399eef07f5c3b64b14d4f586 Mon Sep 17 00:00:00 2001 From: tdb3 <106488469+tdb3@users.noreply.github.com> Date: Wed, 21 Aug 2024 20:19:13 -0400 Subject: [PATCH] fix: handle invalid rpcbind port earlier Previously, when an invalid port was specified in `-rpcbind`, the `SplitHostPort()` return value in `HTTPBindAddresses()` was ignored and attempt would be made to bind to the default rpcbind port (with the host/port treated as a host). This rearranges port checking code in `AppInitMain()` to handle the invalid port before reaching `HTTPBindAddresses()`. Also adjusts associated functional tests. --- src/init.cpp | 6 +++--- test/functional/rpc_bind.py | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index c2e634b670f..8fbdb880bc9 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1279,6 +1279,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) RegisterZMQRPCCommands(tableRPC); #endif + // Check port numbers + if (!CheckHostPortOptions(args)) return false; + /* Start the RPC server already. It will be started in "warmup" mode * and not really process calls already (but it will signify connections * that the server is there and will be ready later). Warmup mode will @@ -1369,9 +1372,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) validation_signals.RegisterValidationInterface(fee_estimator); } - // Check port numbers - if (!CheckHostPortOptions(args)) return false; - for (const std::string& socket_addr : args.GetArgs("-bind")) { std::string host_out; uint16_t port_out{0}; diff --git a/test/functional/rpc_bind.py b/test/functional/rpc_bind.py index 0aabba2c462..69afd45b9af 100755 --- a/test/functional/rpc_bind.py +++ b/test/functional/rpc_bind.py @@ -6,7 +6,6 @@ from test_framework.netutil import all_interfaces, addr_to_hex, get_bind_addrs, test_ipv6_local from test_framework.test_framework import BitcoinTestFramework, SkipTest -from test_framework.test_node import ErrorMatch from test_framework.util import assert_equal, assert_raises_rpc_error, get_rpc_proxy, rpc_port, rpc_url class RPCBindTest(BitcoinTestFramework): @@ -55,9 +54,9 @@ class RPCBindTest(BitcoinTestFramework): base_args = ['-disablewallet', '-nolisten'] if allow_ips: base_args += ['-rpcallowip=' + x for x in allow_ips] - init_error = 'Error' # generic error will be adjusted in next commit + init_error = 'Error: Invalid port specified in -rpcbind: ' for addr in addresses: - self.nodes[0].assert_start_raises_init_error(base_args + [f'-rpcbind={addr}'], init_error, ErrorMatch.PARTIAL_REGEX) + self.nodes[0].assert_start_raises_init_error(base_args + [f'-rpcbind={addr}'], init_error + f"'{addr}'") def run_allowip_test(self, allow_ips, rpchost, rpcport): '''