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>
This commit is contained in:
2026-11-20 20:10:01 -06:00
co-authored by Claude Opus 5.5
parent 179429ef7e
commit 3537531901
3 changed files with 44 additions and 7 deletions
+13
View File
@@ -97,5 +97,18 @@ class HTTPBasicsTest (BitcoinTestFramework):
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()
+5
View File
@@ -420,6 +420,10 @@ static void libevent_log_cb(int severity, const char *msg)
LogPrint("libevent", "libevent: %s\n", msg);
}
/** Maximum size of an HTTP request line + headers. libevent's default is unbounded and it
* buffers all of it before the -rpcallowip ACL and auth checks run. Same value as upstream. */
static const size_t MAX_HEADERS_SIZE = 8192;
bool InitHTTPServer()
{
struct evhttp* http = 0;
@@ -466,6 +470,7 @@ bool InitHTTPServer()
}
evhttp_set_timeout(http, GetArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
evhttp_set_max_body_size(http, MAX_SIZE);
evhttp_set_gencb(http, http_request_cb, NULL);
+26 -7
View File
@@ -71,6 +71,22 @@ uint64_t the_commission(const CBlock *pblock,int32_t height);
int32_t hush_blockload(CBlock& block,CBlockIndex *pindex);
arith_uint256 hush_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc);
/**
* Releases cs_main for its scope and re-acquires it on exit, including when unwinding.
* For code already holding cs_main through a scoped LOCK(cs_main): if an exception escaped
* while cs_main was released by hand, that LOCK's destructor would unlock a mutex this thread
* no longer owns (BOOST_VERIFY -> abort).
*/
class CMainUnlockScope
{
public:
CMainUnlockScope() { LEAVE_CRITICAL_SECTION(cs_main); }
~CMainUnlockScope() { ENTER_CRITICAL_SECTION(cs_main); }
private:
CMainUnlockScope(const CMainUnlockScope&);
CMainUnlockScope& operator=(const CMainUnlockScope&);
};
/**
* Return average network hashes per second based on the last 'lookup' blocks,
* or over the difficulty averaging window if 'lookup' is nonpositive.
@@ -712,8 +728,8 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp, const CPubKey& myp
}
// Release the wallet and main lock while waiting
LEAVE_CRITICAL_SECTION(cs_main);
{
CMainUnlockScope unlockMain;
checktxtime = boost::get_system_time() + boost::posix_time::minutes(1);
boost::unique_lock<boost::mutex> lock(csBestBlock);
@@ -728,7 +744,6 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp, const CPubKey& myp
}
}
}
ENTER_CRITICAL_SECTION(cs_main);
if (!IsRPCRunning())
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
@@ -758,12 +773,16 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp, const CPubKey& myp
}
#ifdef ENABLE_WALLET
CReserveKey reservekey(pwalletMain);
LEAVE_CRITICAL_SECTION(cs_main);
pblocktemplate = CreateNewBlockWithKey(reservekey,pindexPrevNew->GetHeight()+1,HUSH_MAXGPUCOUNT,false);
#else
pblocktemplate = CreateNewBlockWithKey();
#endif
ENTER_CRITICAL_SECTION(cs_main);
{
// CreateNewBlockWithKey can throw (keypool/wallet DB errors); cs_main must be held again by then.
CMainUnlockScope unlockMain;
#ifdef ENABLE_WALLET
pblocktemplate = CreateNewBlockWithKey(reservekey,pindexPrevNew->GetHeight()+1,HUSH_MAXGPUCOUNT,false);
#else
pblocktemplate = CreateNewBlockWithKey();
#endif
}
if (!pblocktemplate)
throw std::runtime_error("CreateNewBlock(): create block failed");
//throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory or no available utxo for staking");