Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
fix(serve): fix wildcard queries false positives
Browse files Browse the repository at this point in the history
When a wildcard was used for a query param, it would match even if that query param was undefined

fix #211
  • Loading branch information
tamj0rd2 committed May 26, 2020
1 parent 60097ff commit 09b8694
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/commands/serve/server/query-validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ describe('wildcard queries', () => {
expect(validateQuery(endpoint, query)).toBe(true)
})

it('returns false if an expected value is undefined', () => {
const endpoint = '/api/resource?hello=*'
const query = {}

expect(validateQuery(endpoint, query)).toBe(false)
})

it('returns true for an array of any values', () => {
const endpoint = '/api/resource?hello=*'
const query = { hello: [randomString(), randomString()] }
Expand Down
12 changes: 9 additions & 3 deletions src/commands/serve/server/query-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ const isStringArray = (x: unknown): x is string[] => Array.isArray(x) && typeof

const compareQuery = (expected: Query[number], actual: Query[number]): boolean => {
if (typeof expected === 'string') {
if (expected === '*') return true
if (typeof actual === 'string') return expected === actual
if (isStringArray(actual)) return actual.includes(expected)
if (typeof actual === 'string') {
if (expected === '*' && actual !== undefined) return true
return expected === actual
}

if (isStringArray(actual)) {
return expected === '*' || actual.includes(expected)
}

return false
}

Expand Down

0 comments on commit 09b8694

Please sign in to comment.