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

Commit 776dff2

Browse files
committed
fix(constructor): prevent prototype pollution in rare error-cases
If a user-provided search parameter is used to instantiate search parameters, it was possible to construct it in such a way that `constructor.prototype` is attempted to be written. That throws an error, but if the error would be caught, the resulting injection still happened. This PR fixes that (small) vulnerability by ensuring `constructor`, is skipped, just like `__proto__`. fixes #922 This is similar/a follow-up to #880
1 parent c3c0c53 commit 776dff2

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/functions/merge.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function _merge(target, source) {
2323
for (var key in source) {
2424
if (
2525
!Object.prototype.hasOwnProperty.call(source, key) ||
26-
key === '__proto__'
26+
key === '__proto__' ||
27+
key === 'constructor'
2728
) {
2829
continue;
2930
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
var algoliasearchHelper = require('../../../index');
4+
5+
test('not vulnerable to prototype pollution', () => {
6+
try {
7+
algoliasearchHelper({}, '', {constructor: {prototype: {test: 123}}});
8+
} catch (e) {
9+
// even if it throws an error, we need to be sure no vulnerability happens
10+
}
11+
12+
expect({}.test).toBeUndefined();
13+
});

test/spec/functions/merge.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,15 @@ it('does not pollute the prototype', () => {
183183

184184
expect({}.polluted).toBe(undefined);
185185
});
186+
187+
it('does not pollute the prototype in error condition', () => {
188+
expect({}.polluted).toBe(undefined);
189+
190+
try {
191+
merge({}, {'constructor': {'prototype': {'polluted': 'vulnerable to PP'}}});
192+
} catch (e) {
193+
// ignore
194+
}
195+
196+
expect({}.polluted).toBe(undefined);
197+
});

0 commit comments

Comments
 (0)