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: do not allow invalid hazardous keys in query #880

Merged
merged 6 commits into from Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions __tests__/parseQuery.spec.ts
Expand Up @@ -85,4 +85,15 @@ describe('parseQuery', () => {

expect('decoding "%"').toHaveBeenWarnedTimes(1)
})

it('keep prototype', () => {
cawa-93 marked this conversation as resolved.
Show resolved Hide resolved
const query = parseQuery('__proto__=1')
expect(query.__proto__).toEqual(Object.prototype)
expect(query.constructor).toEqual(Object)
})

it('keep build-in methods', () => {
cawa-93 marked this conversation as resolved.
Show resolved Hide resolved
const query = parseQuery('toString=1')
expect(query.toString).toEqual(Object.prototype.toString)
})
})
11 changes: 11 additions & 0 deletions src/query.ts
Expand Up @@ -33,6 +33,14 @@ export type LocationQueryRaw = Record<
LocationQueryValueRaw | LocationQueryValueRaw[]
>

/**
* Do not allow invalid hazardous query keys
*/
function isAllowedQueryKey(key: string): boolean {
const keysBlockList = ['__proto__', 'constructor', 'prototype']
return !keysBlockList.includes(key) && !Object.prototype.hasOwnProperty(key)
}

/**
* Transforms a queryString into a {@link LocationQuery} object. Accept both, a
* version with the leading `?` and without Should work as URLSearchParams
Expand All @@ -55,6 +63,9 @@ export function parseQuery(search: string): LocationQuery {
// allow the = character
let eqPos = searchParam.indexOf('=')
let key = decode(eqPos < 0 ? searchParam : searchParam.slice(0, eqPos))
if (!isAllowedQueryKey(key)) {
cawa-93 marked this conversation as resolved.
Show resolved Hide resolved
continue
}
let value = eqPos < 0 ? null : decode(searchParam.slice(eqPos + 1))

if (key in query) {
Expand Down