Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case sensitive query parameters #8852

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@
}
},
{
"url": "/api/v1/contracts/results/logs?timestamp=gte:1639010151.000000000&timestamp=lte:1639010161.000000000&limit=3&order=desc&index=lte:1",
"urls": [
"/api/v1/contracts/results/logs?timestamp=gte:1639010151.000000000&timestamp=lte:1639010161.000000000&limit=3&order=desc&index=lte:1",
"/api/v1/contracts/results/logs?timestamp=gte:1639010151.000000000&timestamp=lte:1639010161.000000000&limit=3&order=DESC&index=lte:1",
"/api/v1/contracts/results/logs?timestamp=gte:1639010151.000000000&timestamp=lte:1639010161.000000000&limit=3&order=DeSc&index=lte:1"
],
"responseStatus": 200,
"responseJson": {
"logs": [
Expand Down Expand Up @@ -324,7 +328,9 @@
},
{
"urls": [
"/api/v1/contracts/results/logs?index=gte:0&timestamp=gte:1639010141.000000000&index=lte:1&timestamp=lte:1639010141.000000000&limit=1&order=asc"
"/api/v1/contracts/results/logs?index=gte:0&timestamp=gte:1639010141.000000000&index=lte:1&timestamp=lte:1639010141.000000000&limit=1&order=asc",
"/api/v1/contracts/results/logs?index=gte:0&timestamp=gte:1639010141.000000000&index=lte:1&timestamp=lte:1639010141.000000000&limit=1&order=ASC",
"/api/v1/contracts/results/logs?index=gte:0&timestamp=gte:1639010141.000000000&index=lte:1&timestamp=lte:1639010141.000000000&limit=1&order=Asc"
],
"responseStatus": 200,
"responseJson": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@
},
"tests": [
{
"url": "/api/v1/transactions?timestamp=1565779209.711927001&account.id=0.0.9&type=credit&result=success",
"urls": [
"/api/v1/transactions?timestamp=1565779209.711927001&account.id=0.0.9&type=credit&result=success&order=asc",
"/api/v1/transactions?timestamp=1565779209.711927001&account.id=0.0.9&type=credit&result=SUCCESS&order=ASC",
"/api/v1/transactions?timestamp=1565779209.711927001&account.id=0.0.9&type=credit&result=SucCess&order=AsC"
],
"responseStatus": 200,
"responseJson": {
"transactions": [
Expand Down
12 changes: 12 additions & 0 deletions hedera-mirror-rest/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2039,3 +2039,15 @@ describe('bigIntMin', () => {
expect(utils.bigIntMin(a, b)).toEqual(expected);
});
});

describe('lowerCaseQueryValue', () => {
test.each`
input | expected
${'success'} | ${'success'}
${'SUCCESS'} | ${'success'}
${'SUCCess'} | ${'success'}
${100} | ${100}
`('$input', ({input, expected}) => {
expect(utils.lowerCaseQueryValue(input)).toEqual(expected);
});
});
13 changes: 10 additions & 3 deletions hedera-mirror-rest/middleware/requestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import httpContext from 'express-http-context';
import qs from 'qs';

import {httpStatusCodes, requestIdLabel, requestStartTime} from '../constants';
import {randomString} from '../utils';
import {lowerCaseQueryValue, randomString} from '../utils';

const queryCanonicalizationMap = {
order: lowerCaseQueryValue,
result: lowerCaseQueryValue,
};

const requestLogger = async (req, res, next) => {
const requestId = await randomString(8);
Expand Down Expand Up @@ -57,11 +62,13 @@ const requestQueryParser = (queryString) => {
const caseInsensitiveQueryString = {};
for (const [key, value] of Object.entries(parsedQueryString)) {
const lowerKey = key.toLowerCase();
const canonicalizationFunc = queryCanonicalizationMap[lowerKey];
const canonicalValue = canonicalizationFunc ? canonicalizationFunc(value) : value;
if (lowerKey in caseInsensitiveQueryString) {
// handle repeated values, merge into an array
caseInsensitiveQueryString[lowerKey] = merge(caseInsensitiveQueryString[lowerKey], value);
caseInsensitiveQueryString[lowerKey] = merge(caseInsensitiveQueryString[lowerKey], canonicalValue);
} else {
caseInsensitiveQueryString[lowerKey] = value;
caseInsensitiveQueryString[lowerKey] = canonicalValue;
}
}

Expand Down
3 changes: 3 additions & 0 deletions hedera-mirror-rest/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ const isValidAddressBookFileIdPattern = (fileId) => {
return addressBookFileIdPattern.includes(fileId);
};

const lowerCaseQueryValue = (queryValue) => (typeof queryValue === 'string' ? queryValue.toLowerCase() : queryValue);

/**
* Validate input parameters for the rest apis
* @param {String} param Parameter to be validated
Expand Down Expand Up @@ -1743,6 +1745,7 @@ export {
isValidSlot,
isValidTimestampParam,
isValidValueIgnoreCase,
lowerCaseQueryValue,
ltLte,
mergeParams,
nowInNs,
Expand Down
Loading