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

Commit

Permalink
fix(SearchParameters): ignore invalid parameters (#880)
Browse files Browse the repository at this point in the history
* fix(SearchParameters): ignore invalid parameters

These parameters could be used maliciously, so are explicilty not allowed in merge

* clearer test
  • Loading branch information
Haroenv committed Oct 19, 2021
1 parent dff9e32 commit 4ff542b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/functions/merge.js
Expand Up @@ -21,7 +21,10 @@ function _merge(target, source) {
}

for (var key in source) {
if (!Object.prototype.hasOwnProperty.call(source, key)) {
if (
!Object.prototype.hasOwnProperty.call(source, key) ||
key === '__proto__'
) {
continue;
}

Expand All @@ -32,7 +35,10 @@ function _merge(target, source) {
continue;
}

if (isObjectOrArrayOrFunction(targetVal) && isObjectOrArrayOrFunction(sourceVal)) {
if (
isObjectOrArrayOrFunction(targetVal) &&
isObjectOrArrayOrFunction(sourceVal)
) {
target[key] = _merge(targetVal, sourceVal);
} else {
target[key] = clone(sourceVal);
Expand Down
13 changes: 13 additions & 0 deletions test/spec/functions/defaultsPure.js
Expand Up @@ -92,3 +92,16 @@ it('should keep the keys order when adding facet refinements', function() {
);
expect(Object.keys(actual)).toEqual(['facet1', 'facet2']);
});

it('does not pollute the prototype', () => {
var payload = JSON.parse('{"__proto__": {"polluted": "vulnerable to PP"}}');
var subject = {};

expect(subject.polluted).toBe(undefined);

const out = defaults({}, payload);

expect(out).toEqual({});

expect({}.polluted).toBe(undefined);
});
1 change: 0 additions & 1 deletion test/spec/functions/intersection.js
Expand Up @@ -18,4 +18,3 @@ test('it should not produce duplicate primitive values', function() {
'2'
]);
});

13 changes: 13 additions & 0 deletions test/spec/functions/merge.js
Expand Up @@ -170,3 +170,16 @@ it('should not convert strings to arrays when merging arrays of `source`', funct

expect(actual).toStrictEqual({a: ['x', 'y', 'z']});
});

it('does not pollute the prototype', () => {
var payload = JSON.parse('{"__proto__": {"polluted": "vulnerable to PP"}}');
var subject = {};

expect(subject.polluted).toBe(undefined);

const out = merge({}, payload);

expect(out).toEqual({});

expect({}.polluted).toBe(undefined);
});

0 comments on commit 4ff542b

Please sign in to comment.