Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Prototype Pollution #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -13,10 +13,12 @@
* @api public
*/

exports = module.exports = function(a, b){
exports = module.exports = function (a, b) {
if (a && b) {
for (var key in b) {
a[key] = b[key];
var allowedAttrs = Object.getOwnPropertyNames(b);

for (var allowedAttrs in b) {
a[allowedAttrs] = b[allowedAttrs];
}
}
return a;
55 changes: 35 additions & 20 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
var merge = require('../index');

describe('merge', function() {
describe('an object', function() {
describe('merge', function () {

describe('an object', function () {
var a = { foo: 'bar' }
, b = { bar: 'baz' };
var o = merge(a, b);
it('should merge properties into first object', function() {

it('should merge properties into first object', function () {
expect(Object.keys(a)).to.have.length(2);
expect(a.foo).to.be.equal('bar');
expect(a.bar).to.be.equal('baz');
});
it('should return first argument', function() {

it('should return first argument', function () {
expect(o).to.be.equal(a);
});
});
describe('an object with duplicate key', function() {

describe('an object with duplicate key', function () {
var a = { foo: 'bar', qux: 'corge' }
, b = { foo: 'baz' };
var o = merge(a, b);
it('should merge properties into first object', function() {

it('should merge properties into first object', function () {
expect(Object.keys(a)).to.have.length(2);
expect(a.foo).to.be.equal('baz');
expect(a.qux).to.be.equal('corge');
});
it('should return first argument', function() {

it('should return first argument', function () {
expect(o).to.be.equal(a);
});
});
describe('without a source object', function() {

describe('without a source object', function () {
var a = { foo: 'bar' };
var o = merge(a);
it('should leave first object unmodified', function() {

it('should leave first object unmodified', function () {
expect(Object.keys(a)).to.have.length(1);
expect(a.foo).to.be.equal('bar');
});
it('should return first argument', function() {

it('should return first argument', function () {
expect(o).to.be.equal(a);
});
});


describe('prototype pollution', function () {
var a = { foo: 'bar', qux: 'corge' }
, b = {
"__proto__": {
"polluted": "true",
}
};
var o = merge(a, b);

it('should leave first object unmodified', function () {
expect(o.__proto__.polluted).to.be.equal(undefined);
});

});

});