Skip to content

Commit

Permalink
Fix against prototype pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneware committed Mar 5, 2021
1 parent a2bc9f8 commit 9e58884
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -75,9 +75,9 @@ function apply(changes, target, modify) {
ptr[prop] = {};
}

if (i < len - 1) {
if (i < len - 1 && ptr.hasOwnProperty(prop)) {
ptr = ptr[prop];
} else {
} else if (prop !== '__proto__') {
ptr[prop] = ch.value;
}
});
Expand All @@ -101,7 +101,7 @@ function apply(changes, target, modify) {
} else {
if (Array.isArray(ptr)) {
ptr.splice(parseInt(prop, 10), 1);
} else {
} else if (ptr.hasOwnProperty(prop)) {
delete ptr[prop];
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Expand Up @@ -243,4 +243,13 @@ describe('changeset', function () {
]);
done();
});

it('should not allow prototype pollution', function(done) {
var changeset = [
{ type: 'put', key: ['__proto__','polluted'], value: 'Yes! Its Polluted'}
];
diff.apply(changeset, {}, true);
expect({}.polluted).to.not.equal('Yes! Its Polluted');
done();
})
});

1 comment on commit 9e58884

@abergmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CVE-2021-25915 was assigned to this commit.

Please sign in to comment.