rest: Reject + sign in /blockhashbyheight/

This commit is contained in:
MarcoFalke committed 2025-05-15 22:51:15 +02:00
1 parent fafd43c691
commit 8888bb499d
2 files changed
+6 -4

No files matched your search

+4 -4
View File
@@ -962,8 +962,8 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
std::string height_str; std::string height_str;
const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part); const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part);
int32_t blockheight = -1; // Initialization done only to prevent valgrind false positive, see https://github.com/bitcoin/bitcoin/pull/18785 const auto blockheight{ToIntegral<int32_t>(height_str)};
if (!ParseInt32(height_str, &blockheight) || blockheight < 0) { if (!blockheight || *blockheight < 0) {
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str)); return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str));
} }
@@ -974,10 +974,10 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
ChainstateManager& chainman = *maybe_chainman; ChainstateManager& chainman = *maybe_chainman;
LOCK(cs_main); LOCK(cs_main);
const CChain& active_chain = chainman.ActiveChain(); const CChain& active_chain = chainman.ActiveChain();
if (blockheight > active_chain.Height()) { if (*blockheight > active_chain.Height()) {
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range"); return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
} }
pblockindex = active_chain[blockheight]; pblockindex = active_chain[*blockheight];
} }
switch (rf) { switch (rf) {
case RESTResponseFormat::BINARY: { case RESTResponseFormat::BINARY: {
+2
View File
@@ -271,6 +271,8 @@ class RESTTest (BitcoinTestFramework):
# Check invalid blockhashbyheight requests # Check invalid blockhashbyheight requests
resp = self.test_rest_request(f"/blockhashbyheight/{INVALID_PARAM}", ret_type=RetType.OBJ, status=400) resp = self.test_rest_request(f"/blockhashbyheight/{INVALID_PARAM}", ret_type=RetType.OBJ, status=400)
assert_equal(resp.read().decode('utf-8').rstrip(), f"Invalid height: {INVALID_PARAM}") assert_equal(resp.read().decode('utf-8').rstrip(), f"Invalid height: {INVALID_PARAM}")
resp = self.test_rest_request("/blockhashbyheight/+1", ret_type=RetType.OBJ, status=400)
assert_equal(resp.read().decode('utf-8').rstrip(), "Invalid height: 1")
resp = self.test_rest_request("/blockhashbyheight/1000000", ret_type=RetType.OBJ, status=404) resp = self.test_rest_request("/blockhashbyheight/1000000", ret_type=RetType.OBJ, status=404)
assert_equal(resp.read().decode('utf-8').rstrip(), "Block height out of range") assert_equal(resp.read().decode('utf-8').rstrip(), "Block height out of range")
resp = self.test_rest_request("/blockhashbyheight/-1", ret_type=RetType.OBJ, status=400) resp = self.test_rest_request("/blockhashbyheight/-1", ret_type=RetType.OBJ, status=400)