http: check rpcallowip immediately after accepting connection

Instead of sending 403 Forbidden, disconnect as soon as possible.

To facilitate unit testing, this commit includes a refactor
that moves the subnet allow list and relevant methods
into the HTTPServer class instead of file-scope static scope.
This commit is contained in:
Matthew Zipkin
2026-05-23 09:24:21 -04:00
parent 32941e1314
commit d1ed2a6e25
7 changed files with 75 additions and 39 deletions

View File

@@ -215,6 +215,11 @@ public:
Assume(m_listen.empty()); // Missing call to StopListening()
}
/**
* Parse the user's -rpcallowip settings and populate m_allow_subnets
*/
bool InitHTTPAllowList();
/**
* Bind to a new address:port, start listening and add the listen socket to `m_listen`.
* @param[in] to Where to bind.
@@ -376,6 +381,16 @@ private:
*/
std::chrono::seconds m_rpcservertimeout{DEFAULT_HTTP_SERVER_TIMEOUT};
/**
* List of subnets to allow HTTP connections from
*/
std::vector<CSubNet> m_allow_subnets;
/**
* Check an incoming connection's source IP against the allow list
*/
bool ClientAllowed(const CNetAddr& netaddr) const;
/**
* Accept a connection.
* @param[in] listen_sock Socket on which to accept the connection.