diff --git a/index.js b/index.js index 5fecaa0..3b94453 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,14 @@ function extend(target, source) { var value; for (var key in source) { + if (key === '__proto__' || key === 'constructor' || key === 'prototype') { + continue; + } + + if (!Object.prototype.hasOwnProperty.call(source, key)) { + continue; + } + value = source[key]; if (Array.isArray(value)) { diff --git a/test/extend.js b/test/extend.js index cbd8ca5..9470746 100644 --- a/test/extend.js +++ b/test/extend.js @@ -51,4 +51,13 @@ describe('extend', function() { ] }); }); + + it('should not pollute Object.prototype', function() { + var target = {}; + var source = JSON.parse('{"__proto__": {"polluted": "yes"}}'); + + util.extend(target, source); + + assert.strictEqual(Object.prototype.polluted, undefined); + }); }); \ No newline at end of file