fix: keep __proto__ keys as own properties#47
Open
spokodev wants to merge 1 commit into
Open
Conversation
`__proto__` as an identifier or string key in an object literal is the
prototype setter, not an own property. So an object with an own
`__proto__` key serialized to `{__proto__:value}`, and `eval` applied it
as the prototype and dropped the entry. Emit `__proto__` as a computed
key (`{['__proto__']:value}`) so it round-trips as an own property.
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.
Problem
An own
__proto__property is dropped on a round-trip:{ __proto__: 42 }in an object literal is the prototype setter, not an own property, so the entry disappears (and a__proto__value that is an object even changes the result's prototype, which then breaks re-stringifying it).Root cause
quoteKeyemits__proto__as a bare identifier (it is a valid identifier name), producing the prototype-setter form. A quoted string key ("__proto__") would behave the same way — only a computed key creates an own property.Fix
Emit
__proto__as a computed key:{['__proto__']:42}is an own property and leaves the prototype untouched, matchingJSON.parse/structuredClone. Other keys (includingconstructor,toString) are unaffected.Test plan
__proto__key (fails onmain, passes with the fix).