fix(invert): use baseAssignValue for __proto__ key support#6233
Closed
neocrev wants to merge 1 commit into
Closed
Conversation
_.invert and _.invertBy silently drop keys when the value becomes '__proto__' because result['__proto__'] = value triggers the __proto__ accessor instead of setting a data property. Fix: use baseAssignValue which already handles __proto__ via Object.defineProperty, consistent with the rest of lodash. Fixes #6195
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.
Description
Fixes #6195
Problem
_.invertand_.invertBysilently drop keys when the inverted value is__proto__:The root cause is
result[value] = key— whenvalueis"__proto__", this triggers the__proto__accessor onObject.prototype, setting the prototype instead of a data property.Fix
Changed both
invertandinvertByto usebaseAssignValueinstead of direct property assignment.baseAssignValuealready has correct__proto__handling viaObject.defineProperty, consistent with the rest of lodash (e.g.,mapKeys,merge,assign).invert:result[value] = key→baseAssignValue(result, value, key)invertBy:result[value] = [key]→baseAssignValue(result, value, [key])The
hasOwnProperty.call(result, value)check ininvertByalready works correctly for__proto__(it checks own properties), so only the else branch needed changing.Verification