Skip to content

Commit

Permalink
filter out invalid keys
Browse files Browse the repository at this point in the history
- since this is just a patch bump, the invalid keys are only being filtered out to fix the bug
- in a future version, throw an error on invalid keys
  • Loading branch information
doowb committed Jun 18, 2021
1 parent dc25690 commit 66eb3f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

var toPath = require('to-object-path');

function isValidKey(key) {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
}

/**
* Defines a getter function on an object using property path notation.
*
Expand Down Expand Up @@ -40,11 +44,13 @@ function setGetter(obj, prop, getter) {

function define(obj, prop, getter) {
if (!~prop.indexOf('.')) {
defineProperty(obj, prop, getter);
if (isValidKey(prop)) {
defineProperty(obj, prop, getter);
}
return obj;
}

var keys = prop.split('.');
var keys = prop.split('.').filter(isValidKey);
var last = keys.pop();
var target = obj;
var key;
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ describe('set-getter', function() {
});

assert(!('polluted' in {}));
assert(!('polluted' in obj));
assert('polluted' in obj);

assert({}.polluted !== true);
assert(obj.polluted !== true);
assert(obj.polluted === true);
});

it('should not pollute the prototype when using array notation', function() {
Expand All @@ -123,9 +123,9 @@ describe('set-getter', function() {
});

assert(!('polluted' in {}));
assert(!('polluted' in obj));
assert('polluted' in obj);

assert({}.polluted !== true);
assert(obj.polluted !== true);
assert(obj.polluted === true);
});
});

0 comments on commit 66eb3f0

Please sign in to comment.