Skip to content

Commit

Permalink
Drop __proto__ keys (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninevra committed Apr 7, 2021
1 parent eefcc77 commit ddca73d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -33,6 +33,11 @@ const mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
for (const [key, value] of Object.entries(object)) {
let [newKey, newValue, {shouldRecurse = true} = {}] = mapper(key, value, object);

// Drop `__proto__` keys.
if (newKey === '__proto__') {
continue;
}

if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
newValue = Array.isArray(newValue) ?
mapArray(newValue) :
Expand Down
15 changes: 7 additions & 8 deletions test.js
Expand Up @@ -153,14 +153,13 @@ test('validates input', t => {
}, TypeError);
});

test.failing('identity function preserves __proto__ keys', t => {
test('__proto__ keys are safely dropped', t => {
const input = {['__proto__']: {one: 1}};
t.deepEqual(mapObject(input, (key, value) => [key, value]), input);
});
const output = mapObject(input, (key, value) => [key, value]);
t.deepEqual(output, {});

test.failing('mapper can produce __proto__ keys', t => {
t.deepEqual(
mapObject({proto: {one: 1}}, (key, value) => [`__${key}__`, value]),
{['__proto__']: {one: 1}}
);
// AVA's equality checking isn't quite strict enough to catch the difference
// between plain objects as prototypes and Object.prototype, so we also check
// the prototype by identity
t.is(Object.getPrototypeOf(output), Object.prototype);
});

0 comments on commit ddca73d

Please sign in to comment.