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

@@ -5,6 +5,7 @@
"""Test the HTTP server basics."""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.netutil import NETWORK_ERRORS
from test_framework.util import assert_equal, str_to_b64str
import http.client
@@ -17,15 +18,6 @@ RPCSERVERTIMEOUT = 2
MAX_HEADERS_SIZE = 8192
MAX_BODY_SIZE = 32 * 1024 * 1024
# When a test expects a server disconnection, any of these errors are
# acceptable. The specific event is determined by race condition and platform OS.
NETWORK_ERRORS = (
BrokenPipeError, # write to a closed socket/pipe
ConnectionResetError, # connection forcibly closed by peer
ConnectionAbortedError, # connection aborted locally or by network stack
http.client.ResponseNotReady, # server response not ready or connection out of sync
)
class BitcoinHTTPConnection:
def __init__(self, node):
self.url = urllib.parse.urlparse(node.url)