Skip to content

Commit

Permalink
fix(SearchParameters): ignore invalid parameters (algolia/algoliasear…
Browse files Browse the repository at this point in the history
…ch-helper-js#880)

* 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 f9d4ef6 commit 18f38b5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/algoliasearch-helper/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 packages/algoliasearch-helper/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);
});
Expand Up @@ -18,4 +18,3 @@ test('it should not produce duplicate primitive values', function() {
'2'
]);
});

13 changes: 13 additions & 0 deletions packages/algoliasearch-helper/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 18f38b5

Please sign in to comment.