Skip to content

Commit d717def

Browse files
authored
prototype pollution prevention (#1394)
* prototype pollution prevention * update base branch * update ubuntu version
1 parent 6051042 commit d717def

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ name: Rollbar.js CI
22

33
on:
44
push:
5-
branches: [master]
5+
branches: [next/2.x/main]
66
tags: [v*]
77
pull_request:
8-
branches: [master]
8+
branches: [next/2.x/main]
99

1010
jobs:
1111
build:
12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-22.04
1313

1414
strategy:
1515
matrix:

src/merge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function merge() {
3434
copy,
3535
clone,
3636
name,
37-
result = {},
37+
result = Object.create(null), // no prototype pollution on Object
3838
current = null,
3939
length = arguments.length;
4040

src/utility.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,10 @@ function set(obj, path, value) {
660660
if (!obj) {
661661
return;
662662
}
663+
664+
// Prevent prototype pollution by setting the prototype to null.
665+
Object.setPrototypeOf(obj, null);
666+
663667
var keys = path.split('.');
664668
var len = keys.length;
665669
if (len < 1) {

test/utility.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ describe('merge', function () {
446446
expect(e.amihere).to.eql('yes');
447447
done();
448448
});
449+
it('should be secure against prototype pollution', function () {
450+
const o1 = JSON.parse('{"__proto__": {"polluted": "yes"}}');
451+
const o2 = JSON.parse('{"__proto__": {"polluted": "yes"}}');
452+
const result = _.merge(o1, o2);
453+
expect({}.polluted).to.not.eql('yes');
454+
expect(result.polluted).to.not.eql('yes');
455+
});
449456
});
450457

451458
var traverse = require('../src/utility/traverse');
@@ -765,6 +772,12 @@ describe('set', function () {
765772
expect(o.foo.bar.buzz).to.eql(97);
766773
expect(o.foo.bar.baz.fizz).to.eql(1);
767774
});
775+
it('should be secure against prototype pollution', function () {
776+
const o = {};
777+
_.set(o, '__proto__.polluted', 'yes');
778+
expect({}.polluted).to.not.eql('yes');
779+
expect(o.polluted).to.not.eql('yes');
780+
});
768781
});
769782

770783
var scrub = require('../src/scrub');

0 commit comments

Comments
 (0)