fix(commons): skip __proto__ / constructor / prototype keys in _.merge#3687
Closed
ridingsa wants to merge 1 commit into
Closed
fix(commons): skip __proto__ / constructor / prototype keys in _.merge#3687ridingsa wants to merge 1 commit into
ridingsa wants to merge 1 commit into
Conversation
The recursive `_.merge` implementation iterates `Object.keys(source)`
and calls `_.merge(target[key], source[key])` for nested objects.
`Object.keys` returns `__proto__` as an own-enumerable key when the
source object came from `JSON.parse('{"__proto__":...}')` (own
enumerable, unlike the object-literal `{__proto__: ...}` form which
sets the prototype slot).
Without filtering those keys, the recursive call resolves
`target['__proto__']` to `Object.prototype` and writes onto it; the
same shape applies to `constructor.prototype` chains. The merge ends
up mutating prototypes rather than the intended target.
Adds the standard prototype-mutating-key filter (`__proto__`,
`constructor`, `prototype`) inside the iteration. Mirrors the filter
already in lodash's `baseSet`, cosmiconfig's merge, and the standard
remediation pattern across recent npm-ecosystem fixes.
Tests:
- All 21 existing commons tests pass.
- Added one new test under `_` describe block confirming a
`{"__proto__":{"polluted":"X"}}` source does not mutate
`Object.prototype`, fresh-`{}` reads, or the target object.
This was referenced Jun 4, 2026
Member
|
Hey @ridingsa — thanks for this, and for the email heads-up. I really appreciate you taking the time to dig into it and write it up so clearly. We're going to get this patched and into a release shortly, and I'll make sure you're credited for the report and attributed on the commits. I'll follow up over email on the timing and the details. Thanks again 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The recursive
_.mergeimplementation in@feathersjs/commonsiteratesObject.keys(source)and calls_.merge(target[key], source[key])for nested objects.Object.keysreturns__proto__as an own-enumerable key when the source object came fromJSON.parse('{"__proto__":...}')— unlike the object-literal{__proto__: ...}form, JSON-parsed__proto__is an own enumerable property, not the prototype slot.Without filtering those keys, the recursive call resolves
target['__proto__']toObject.prototype(since it's truthy, theif (!target[key])branch is skipped) and writes onto it. The same shape applies toconstructor.prototypechains.This PR adds the standard prototype-mutating-key filter (
__proto__,constructor,prototype) inside the iteration — the same fix lodash applies inbaseSet, cosmiconfig applies in itsmerge, and several other widely-used libraries have landed for the same bug class.Diff (3 lines of substantive change)
merge(target: any, source: any) { if (_.isObject(target) && _.isObject(source)) { Object.keys(source).forEach((key) => { + // Skip prototype-chain-mutating keys. + if (key === '__proto__' || key === 'constructor' || key === 'prototype') { + return + } if (_.isObject(source[key])) { if (!target[key]) { Object.assign(target, { [key]: {} }) } _.merge(target[key], source[key]) } else { Object.assign(target, { [key]: source[key] }) } }) } return target }Tests
packages/commonstests pass._describeblock confirming that passing a{"__proto__":...}JSON-parsed source does not mutateObject.prototype, fresh-{}reads, or the target object. Also covers theconstructor.prototypeshape.Run output:
Notes
mergecontinues to work for all documented input shapes; only the (likely-unintended) prototype mutation is removed.