Skip to content

Commit

Permalink
Copy __proto__ successfully in older engines
Browse files Browse the repository at this point in the history
- If Object.defineProperty is unavailable, fall back to regular assignment
- In getProperty(), return undefined instead of {} when getting __proto__
- Improve style in the __proto__ test
  • Loading branch information
mnespor committed Apr 25, 2018
1 parent 294e9c3 commit ebd56b3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -31,9 +31,9 @@ var isPlainObject = function isPlainObject(obj) {
return typeof key === 'undefined' || hasOwn.call(obj, key);
};

// If name is '__proto__', define it as an own property on target
// If name is '__proto__', and Object.defineProperty is available, define __proto__ as an own property on target
var setProperty = function setProperty(target, options) {
if (options.name === '__proto__') {
if (Object.defineProperty && options.name === '__proto__') {
Object.defineProperty(target, options.name, {
enumerable: true,
configurable: true,
Expand All @@ -48,7 +48,7 @@ var setProperty = function setProperty(target, options) {
// Return a new object instead of __proto__ if '__proto__' is not an own property
var getProperty = function getProperty(obj, name) {
if (name === '__proto__' && !hasOwn.call(obj, name)) {
return {};
return undefined;
}

return obj[name];
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -40,4 +40,3 @@
},
"license": "MIT"
}

3 changes: 1 addition & 2 deletions test/index.js
Expand Up @@ -633,7 +633,6 @@ test('__proto__ is merged as an own property', function (t) {
extend(true, target, malicious);
t.notOk(target.george);
t.ok(Object.prototype.hasOwnProperty.call(target, '__proto__'));
var name = '__proto__';
t.equal(target[name].george, 1);
t.deepEqual(target.__proto__, { george: 1 }); // eslint-disable-line no-proto
t.end();
});

0 comments on commit ebd56b3

Please sign in to comment.