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

fix: prevent prototype pollution in rare error-cases #923

Merged
merged 1 commit into from Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/functions/merge.js
Expand Up @@ -23,7 +23,8 @@ function _merge(target, source) {
for (var key in source) {
if (
!Object.prototype.hasOwnProperty.call(source, key) ||
key === '__proto__'
key === '__proto__' ||
key === 'constructor'
) {
continue;
}
Expand Down
13 changes: 13 additions & 0 deletions test/spec/algoliasearch.helper/constructor.js
@@ -0,0 +1,13 @@
'use strict';

var algoliasearchHelper = require('../../../index');

test('not vulnerable to prototype pollution', () => {
try {
algoliasearchHelper({}, '', {constructor: {prototype: {test: 123}}});
} catch (e) {
// even if it throws an error, we need to be sure no vulnerability happens
}

expect({}.test).toBeUndefined();
});
12 changes: 12 additions & 0 deletions test/spec/functions/merge.js
Expand Up @@ -183,3 +183,15 @@ it('does not pollute the prototype', () => {

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

it('does not pollute the prototype in error condition', () => {
expect({}.polluted).toBe(undefined);

try {
merge({}, {'constructor': {'prototype': {'polluted': 'vulnerable to PP'}}});
} catch (e) {
// ignore
}

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