Skip to content

Commit 8888bb4

Browse files
author
MarcoFalke
committed
rest: Reject + sign in /blockhashbyheight/
1 parent fafd43c commit 8888bb4

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/rest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,8 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
962962
std::string height_str;
963963
const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part);
964964

965-
int32_t blockheight = -1; // Initialization done only to prevent valgrind false positive, see https://github.com/bitcoin/bitcoin/pull/18785
966-
if (!ParseInt32(height_str, &blockheight) || blockheight < 0) {
965+
const auto blockheight{ToIntegral<int32_t>(height_str)};
966+
if (!blockheight || *blockheight < 0) {
967967
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str));
968968
}
969969

@@ -974,10 +974,10 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
974974
ChainstateManager& chainman = *maybe_chainman;
975975
LOCK(cs_main);
976976
const CChain& active_chain = chainman.ActiveChain();
977-
if (blockheight > active_chain.Height()) {
977+
if (*blockheight > active_chain.Height()) {
978978
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
979979
}
980-
pblockindex = active_chain[blockheight];
980+
pblockindex = active_chain[*blockheight];
981981
}
982982
switch (rf) {
983983
case RESTResponseFormat::BINARY: {

test/functional/interface_rest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ def run_test(self):
271271
# Check invalid blockhashbyheight requests
272272
resp = self.test_rest_request(f"/blockhashbyheight/{INVALID_PARAM}", ret_type=RetType.OBJ, status=400)
273273
assert_equal(resp.read().decode('utf-8').rstrip(), f"Invalid height: {INVALID_PARAM}")
274+
resp = self.test_rest_request("/blockhashbyheight/+1", ret_type=RetType.OBJ, status=400)
275+
assert_equal(resp.read().decode('utf-8').rstrip(), "Invalid height: 1")
274276
resp = self.test_rest_request("/blockhashbyheight/1000000", ret_type=RetType.OBJ, status=404)
275277
assert_equal(resp.read().decode('utf-8').rstrip(), "Block height out of range")
276278
resp = self.test_rest_request("/blockhashbyheight/-1", ret_type=RetType.OBJ, status=400)

0 commit comments

Comments
 (0)