Skip to content

Commit

Permalink
fix: prevent Object properties to be used as parsing functions (#10570)
Browse files Browse the repository at this point in the history
**Motivation**

`parseQueryParams()` could had unexpected behavior when parameter names where valid `Object.prototype` properties. For example:

```
> parseQueryParams('?toString=42', {})
[Object: null prototype] { toString: '[object Object]' }
```

The code above was returning `{toString: '[object Object]'}` instead of the expected `{toString: '42'}`.

**Test plan**

Added a unit test reproducing the bug.
  • Loading branch information
matias-la committed Aug 7, 2022
1 parent e8c374e commit 7fbd3e5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
48 changes: 48 additions & 0 deletions packages/core/src/__tests__/getPathFromState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,54 @@ it('keeps query params if path is empty', () => {
).toEqual(path);
});

it('does not use Object.prototype properties as parsing functions', () => {
const path = '/?toString=42';
const config = {
screens: {
Foo: {
screens: {
Foe: 'foe',
Bar: {
screens: {
Qux: {
path: '',
parse: {},
},
Baz: 'baz',
},
},
},
},
},
};

const state = {
routes: [
{
name: 'Foo',
state: {
routes: [
{
name: 'Bar',
state: {
routes: [{ name: 'Qux', params: { toString: 42 } }],
},
},
],
},
},
],
};

expect(getPathFromState<object>(state, config)).toBe(path);
expect(
getPathFromState<object>(
getStateFromPath<object>(path, config) as State,
config
)
).toEqual(path);
});

it('cuts nested configs too', () => {
const path = '/foo/baz';
const config = {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/getStateFromPath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ const parseQueryParams = (

if (parseConfig) {
Object.keys(params).forEach((name) => {
if (parseConfig[name] && typeof params[name] === 'string') {
if (
Object.hasOwnProperty.call(parseConfig, name) &&
typeof params[name] === 'string'
) {
params[name] = parseConfig[name](params[name] as string);
}
});
Expand Down

0 comments on commit 7fbd3e5

Please sign in to comment.