fix(helpers): resolveObjectKey returns wrong value for Object.prototype keys - #12286
Open
contactjawad wants to merge 1 commit into
Open
fix(helpers): resolveObjectKey returns wrong value for Object.prototype keys#12286contactjawad wants to merge 1 commit into
contactjawad wants to merge 1 commit into
Conversation
…pe keys The resolver cache was a plain object literal, so it inherited from Object.prototype. Keys matching prototype members (e.g. toString, hasOwnProperty, valueOf) hit the inherited method instead of falling through to build the correct resolver, yielding wrong output. Use a null-prototype object for the cache.
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.
What
resolveObjectKeyreturns an incorrect value (or throws) when the key name collides with anObject.prototypemember such astoString,hasOwnProperty,valueOf,isPrototypeOf,__proto__, etc.Why
The module-level resolver cache
keyResolverswas created as a plain object literal, so it inherits fromObject.prototype. The cache-miss checksees the inherited prototype method as a truthy value for any such key, and never builds the correct resolver. That inherited function is then invoked as
resolver(obj), producing garbage (e.g.toStringcalled with the wrongthisreturns'[object Undefined]') or throwing (__proto__is not callable).How
Make the cache prototype-free by creating it with
Object.create(null). Keys that matchObject.prototypemembers now miss the cache and fall through to_getKeyResolver, which builds the correct literal path resolver. Existing'',x, andyfast-paths are unchanged.Test
Added a case under the
resolveObjectKeydescribe block asserting own properties named after prototype members resolve to their real values:Before the fix the first assertion fails with
'[object Undefined]' to equal 5; after the fix all pass.