Files
dragonx/qa/rpc-tests/httpbasics.py
T
DanSandClaude Opus 5.5 3537531901 rpc: cap HTTP request headers at 8 KiB, and keep getblocktemplate from aborting on an error
From the general audit, both still on dev:

- libevent's max_headers_size defaults to unlimited, and the request line and headers are buffered
  before the -rpcallowip check or authentication runs, so one connection could grow the node's memory
  with every byte it sent. Capped at 8 KiB. Regtest: a 9 KiB header and a 10 KiB request line now get
  400 (the old binary accepted the first and processed the second); normal calls are unaffected.
  qa/rpc-tests/httpbasics.py gains a request-line case.
- getblocktemplate left cs_main with LEAVE_CRITICAL_SECTION around CreateNewBlockWithKey. If that
  threw (a wallet or BDB fault), the enclosing scoped lock's destructor unlocked an already unlocked
  mutex while unwinding, which aborts the node with nothing logged. It now re-enters cs_main first,
  so the error reaches the caller. Regtest: getblocktemplate returns templates as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-20 20:10:01 -06:00

115 lines
5.0 KiB
Python
Executable File

#!/usr/bin/env python2
# Copyright (c) 2016-2024 The Hush developers
# Copyright (c) 2014 The Bitcoin Core developers
# Distributed under the GPLv3 software license, see the accompanying
# file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
# Test rpc http basics
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, start_nodes
import base64
try:
import http.client as httplib
except ImportError:
import httplib
try:
import urllib.parse as urlparse
except ImportError:
import urlparse
class HTTPBasicsTest (BitcoinTestFramework):
def setup_nodes(self):
return start_nodes(4, self.options.tmpdir)
def run_test(self):
#################################################
# lowlevel check for http persistent connection #
#################################################
url = urlparse.urlparse(self.nodes[0].url)
authpair = url.username + ':' + url.password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
out1 = conn.getresponse().read()
assert_equal('"error":null' in out1, True)
assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!
# send 2nd request without closing connection
conn.request('POST', '/', '{"method": "getchaintips"}', headers)
out2 = conn.getresponse().read()
assert_equal('"error":null' in out2, True) # must also response with a correct json-rpc message
assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!
conn.close()
# same should be if we add keep-alive because this should be the std. behaviour
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"}
conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
out1 = conn.getresponse().read()
assert_equal('"error":null' in out1, True)
assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!
# send 2nd request without closing connection
conn.request('POST', '/', '{"method": "getchaintips"}', headers)
out2 = conn.getresponse().read()
assert_equal('"error":null' in out2, True) # must also response with a correct json-rpc message
assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!
conn.close()
# now do the same with "Connection: close"
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"}
conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
out1 = conn.getresponse().read()
assert_equal('"error":null' in out1, True)
assert_equal(conn.sock!=None, False) # now the connection must be closed after the response
# node1 (2nd node) is running with disabled keep-alive option
urlNode1 = urlparse.urlparse(self.nodes[1].url)
authpair = urlNode1.username + ':' + urlNode1.password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
out1 = conn.getresponse().read()
assert_equal('"error":null' in out1, True)
# node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
urlNode2 = urlparse.urlparse(self.nodes[2].url)
authpair = urlNode2.username + ':' + urlNode2.password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
out1 = conn.getresponse().read()
assert_equal('"error":null' in out1, True)
assert_equal(conn.sock!=None, True) # connection must be closed because hushd should use keep-alive by default
# Check excessive request size: request line + headers are capped at 8 KiB
conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
conn.connect()
conn.request('GET', '/' + ('x'*1000), '', headers)
out1 = conn.getresponse()
assert_equal(out1.status, httplib.NOT_FOUND)
conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
conn.connect()
conn.request('GET', '/' + ('x'*10000), '', headers)
out1 = conn.getresponse()
assert_equal(out1.status, httplib.BAD_REQUEST)
if __name__ == '__main__':
HTTPBasicsTest().main()