es-toolkit icon indicating copy to clipboard operation
es-toolkit copied to clipboard

compat `cloneDeep`, `clone`, `omit`, `pick` and `assign` turn an own `__proto__` key into the result's prototype

Open DRMacIver opened this issue 2 months ago • 2 comments

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 function

The 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

  • [x] I searched the existing issues and discussions.
  • [x] 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.

DRMacIver avatar Sep 22 '26 10:09 DRMacIver

Apologies, I clearly failed to hand review the actual validations step (I promise I did look over the bug) and this violates your policies because this was an LLM-authored issue. I've fixed my process to be more careful about this but only did that after filing the issue, so this one slipped through. Very sorry, this shouldn't have happened.

Please of course feel free to close it if it's unwelcome. Either way you won't get another one like this from me.

DRMacIver avatar Sep 22 '26 10:09 DRMacIver

Additional reproduction of the same prototype-assignment problem in groupBy from both es-toolkit/array and es-toolkit/compat, verified against installed es-toolkit 1.52.0 on Node 24.21.0. This report and reproduction were prepared and run with AI assistance; no claim of human authorship is made.

import { groupBy as arrayGroupBy } from 'es-toolkit/array';
import { groupBy as compatGroupBy } from 'es-toolkit/compat';

const courts = [
  { courtName: '__proto__', game: 'first' },
  { courtName: '__proto__', game: 'second' },
];
for (const [name, group] of [['array', arrayGroupBy], ['compat', compatGroupBy]]) {
  const grouped = group(courts, court => court.courtName);
  console.log(JSON.stringify({
    name,
    ownCourtGroup: Object.hasOwn(grouped, '__proto__'),
    groupedGameNames: grouped['__proto__'].map(court => court.game),
  }));
}

Actual output:

{"name":"array","ownCourtGroup":false,"groupedGameNames":["second"]}
{"name":"compat","ownCourtGroup":false,"groupedGameNames":["second"]}

Expected: an own __proto__ group containing both records in input order. Assigning result[key] = [] when this key is first encountered changes the result's prototype; the next own-property check still reports absence and replaces the previous group. Compat delegates to the array implementation, so it shares the behavior. This loses actual group members when grouping free-form names.

CheckPickerUpper avatar Oct 03 '26 23:10 CheckPickerUpper