Skip to content

Commit

Permalink
rest: Reject negative outpoint index in getutxos parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Jul 12, 2024
1 parent 4d6af61 commit fab54db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,14 +788,15 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::

for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
{
int32_t nOutput;
std::string strTxid = uriParts[i].substr(0, uriParts[i].find('-'));
std::string strOutput = uriParts[i].substr(uriParts[i].find('-')+1);
auto output{ToIntegral<uint32_t>(strOutput)};

if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
if (!output || !IsHex(strTxid)) {
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
}

vOutPoints.emplace_back(TxidFromString(strTxid), (uint32_t)nOutput);
vOutPoints.emplace_back(TxidFromString(strTxid), *output);
}

if (vOutPoints.size() > 0)
Expand Down
5 changes: 4 additions & 1 deletion test/functional/interface_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,13 @@ def run_test(self):
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spending[0]}-{spending[1]}")
assert_equal(len(json_obj['utxos']), 1)

# Do some invalid requests
self.log.info("Check some invalid requests")
self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.JSON, body='{"checkmempool', status=400, ret_type=RetType.OBJ)
self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body='{"checkmempool', status=400, ret_type=RetType.OBJ)
self.test_rest_request("/getutxos/checkmempool", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ)
self.test_rest_request(f"/getutxos/{spending[0]}_+1", ret_type=RetType.OBJ, status=400)
self.test_rest_request(f"/getutxos/{spending[0]}-+1", ret_type=RetType.OBJ, status=400)
self.test_rest_request(f"/getutxos/{spending[0]}--1", ret_type=RetType.OBJ, status=400)

# Test limits
long_uri = '/'.join([f"{txid}-{n_}" for n_ in range(20)])
Expand Down

0 comments on commit fab54db

Please sign in to comment.