Skip to content

Commit

Permalink
fix: do not allow invalid hazardous keys in query (#880)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
cawa-93 and posva committed Apr 26, 2021
1 parent c6460f0 commit ecd52e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions __tests__/parseQuery.spec.ts
Expand Up @@ -85,4 +85,15 @@ describe('parseQuery', () => {

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

it('ignores __proto__', () => {
const query = parseQuery('__proto__=1')
expect(query.__proto__).toEqual(Object.prototype)
expect(query.constructor).toEqual(Object)
})

it('ignores build-in methods', () => {
const query = parseQuery('toString=1')
expect(query.toString).toEqual(Object.prototype.toString)
})
})
6 changes: 6 additions & 0 deletions src/query.ts
Expand Up @@ -55,6 +55,12 @@ export function parseQuery(search: string): LocationQuery {
// allow the = character
let eqPos = searchParam.indexOf('=')
let key = decode(eqPos < 0 ? searchParam : searchParam.slice(0, eqPos))

// this ignores ?__proto__&toString
if (Object.prototype.hasOwnProperty(key)) {
continue
}

let value = eqPos < 0 ? null : decode(searchParam.slice(eqPos + 1))

if (key in query) {
Expand Down

0 comments on commit ecd52e0

Please sign in to comment.