Skip to content

Commit

Permalink
fix prototype pollution in deepMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Oct 25, 2021
1 parent 7053473 commit 82cb6c4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
40 changes: 40 additions & 0 deletions __tests__/utils.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,44 @@ describe("Utility Function Tests:", function () {
});
});
}); // end parseS3 tests


describe("deepMerge", function () {

it("Should deep merge objects", function () {
let obj1 = {
"a": {
"b": {
"c": "test"
}
}
};
let obj2 = {
"a": {
"b": {
"c": "test2"
}
}
};

expect(utils.deepMerge(obj1, obj2)).toEqual({
"a": {
"b": {
"c": "test2"
}
}
});
})

it("Prevents prototype pollution", function () {
let payload = '{"__proto__":{"polluted":true}}';
expect({}.polluted).toBeUndefined();
utils.deepMerge({},JSON.parse(payload));
expect({}.polluted).toBeUndefined();
})


}); // end deepMerge tests


}); // end UTILITY tests
8 changes: 5 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ exports.parseS3 = (path) => {

// Deep Merge
exports.deepMerge = (a, b) => {
Object.keys(b).forEach((key) =>
key in a ? this.deepMerge(a[key], b[key]) : Object.assign(a, b)
);
Object.keys(b).forEach((key) => {
if (key === '__proto__') return;
if (typeof b[key] !== 'object') return Object.assign(a, b);
return key in a ? this.deepMerge(a[key], b[key]) : Object.assign(a, b);
});
return a;
};

Expand Down

0 comments on commit 82cb6c4

Please sign in to comment.