compat `cloneDeep`, `clone`, `omit`, `pick` and `assign` turn an own `__proto__` key into the result's prototype
es-toolkit version
1.52.0 (also main at ee72fc74)
Entry point
es-toolkit/compat
Reproduction
Cloning an object that has an own data property named __proto__ (what JSON.parse produces for that key) with compat's cloneDeep drops the key and installs its value as the prototype of the clone; lodash's cloneDeep keeps it as an own key and the clone's prototype stays Object.prototype. clone, omit, pick, assign and mapValues do the same as cloneDeep:
import { cloneDeep, clone, omit, pick, assign, mapValues } from 'es-toolkit/compat';
import lodash from 'lodash';
const parsed = () => JSON.parse('{"__proto__": {"isAdmin": true}, "x": 1}');
for (const [name, lib] of [['compat', { cloneDeep }], ['lodash', lodash]]) {
const copy = lib.cloneDeep(parsed());
console.log(name, 'cloneDeep: own keys', Object.keys(copy),
'isAdmin', copy.isAdmin,
'prototype is Object.prototype', Object.getPrototypeOf(copy) === Object.prototype);
}
for (const [name, f] of [
['clone', o => clone(o)],
['omit', o => omit(o, 'x')],
['pick', o => pick(o, ['x', '__proto__'])],
['assign', o => assign({}, o)],
['mapValues', o => mapValues(o, v => v)],
]) {
console.log('compat', name + ': isAdmin', f(parsed()).isAdmin);
}
const shadowed = JSON.parse('{"__proto__": {"hasOwnProperty": 1}, "x": 1}');
try {
console.log(cloneDeep(shadowed).hasOwnProperty('x'));
} catch (e) {
console.log(String(e));
}Current vs. expected behavior
Output:
compat cloneDeep: own keys [ 'x' ] isAdmin true prototype is Object.prototype false
lodash cloneDeep: own keys [ '__proto__', 'x' ] isAdmin undefined prototype is Object.prototype true
compat clone: isAdmin true
compat omit: isAdmin true
compat pick: isAdmin true
compat assign: isAdmin true
compat mapValues: isAdmin true
TypeError: cloneDeep(...).hasOwnProperty is not a functionThe compat results have only x as an own key and inherit isAdmin from the value that was stored under __proto__ in the input; when that value carries a hasOwnProperty key, the clone's hasOwnProperty is no longer a function. lodash returns an object whose own keys are __proto__ and x, with Object.prototype as its prototype and no isAdmin, and I expected the same from compat. The input object itself is unaffected: its own keys are __proto__ and x before and after each call.
Tested on es-toolkit 1.52.0 from npm and on main at ee72fc74 (src compiled with tsc), against lodash 4.17.21, Node 26.8.1.
Validations
- I searched the existing issues and discussions.
- I read the Contributing Guidelines.
- I wrote this description myself.
BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 41 other bugs in es-toolkit. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/typescript/es-toolkit. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.
Source: toss/es-toolkit