Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: disallow objects as query parameter values (#1795)
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function validateSearchParameters(query: Record<string, any>): void { | ||
for (const key in query) { | ||
const value = query[key] | ||
// We don’t allow any objects as values for query parameters | ||
if (typeof value === 'object' && value !== null && !Array.isArray(value)) { | ||
throw new Error(`Objects are not supported as value for the "${key}" query parameter.`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import validateSearchParameters from '../../../lib/utils/validate-search-parameters' | ||
|
||
describe('validateSearchParameters', () => { | ||
test('does nothing if no values are objects', () => { | ||
const query = { | ||
booleanValue: true, | ||
stringValue: 'string', | ||
numberValue: 3, | ||
nullValue: null, | ||
undefinedValue: undefined, | ||
arrayValue: ['string'], | ||
} | ||
|
||
validateSearchParameters(query) | ||
}) | ||
|
||
test('throws if a value is an object', () => { | ||
const query = { | ||
booleanValue: true, | ||
stringValue: 'string', | ||
objectValue: {}, | ||
} | ||
const expectedErrorMessage = | ||
'Objects are not supported as value for the "objectValue" query parameter' | ||
|
||
expect(() => validateSearchParameters(query)).toThrow(expectedErrorMessage) | ||
}) | ||
|
||
test('adds the affected parameter to the error', () => { | ||
const query = { | ||
affectedParameter: { key: 'value' }, | ||
} | ||
const expectedErrorMessage = | ||
'Objects are not supported as value for the "affectedParameter" query parameter' | ||
|
||
expect(() => validateSearchParameters(query)).toThrow(expectedErrorMessage) | ||
}) | ||
}) |