Repository navigation
Keep host prototype functions away from JSON revivers and replacers - #28
Merged
Merged
Conversation
…t of the sandbox The reviver snapshots a holder's own keys, then reads each queued value with Reflect.get. A callback runs with the holder as `this` between the snapshot and the read, so deleting a still-queued key let the read fall through to the host Object.prototype and handed the callback a live host reference, bypassing the member guard. Resolve the own data descriptor per visit instead; a missing or inherited key reads as undefined.
…throwing A reviver callback can freeze its holder, after which the walk's plain assignment and defineProperty write-back threw a host TypeError that escaped the run as an uncatchable error. Write back through Reflect, whose define and delete return false rather than throwing, so a refused write is left as it is. This matches the host, which ignores the failed write and keeps the original value.
…out of the sandbox stringify reads a plain object's queued values and an array's elements with a plain member access. A replacer runs with the holder as `this` between the key snapshot and that read, so deleting a still-queued key let the read fall through to the host prototype and handed the replacer a live host reference. Read the own data descriptor per key instead, matching the reviver.
📋 Release PreviewChangelog0.7.2 (2026-11-25)🩹 Bug Fixes
🏗 Dependency Updates
|
There was a problem hiding this comment.
🟡 Changes recommended
A critical issue remains because using the in operator can treat an inherited Object.prototype.value as an own descriptor field and pass a host function to a reviver or replacer.
1 open issue
What changed in this PR
This pull request hardens JSON reviver and replacer traversal against host-prototype values and makes reviver write-back tolerant of locked holders.
Changes:
- Adds
ownDataValuefor reading only own data properties. - Updates JSON parsing and stringification to avoid prototype fallthrough and tolerate refused writes.
- Adds regression tests and documents the intentional JavaScript divergence.
| File | Description |
|---|---|
| packages/maple/src/support/own-property.ts | Adds the ownDataValue helper for safely reading own data properties. |
| packages/maple/src/support/own-property.spec.ts | Tests own, missing, inherited, accessor, and array-hole behavior for ownDataValue. |
| packages/maple/src/builtins/json/statics/stringify.ts | Updates replacer traversal to read object properties and array elements without prototype fallback. |
| packages/maple/src/builtins/json/statics/parse.ts | Updates reviver traversal to use own-data reads and tolerant write-back operations. |
| packages/maple/src/builtins/json/json.spec.ts | Adds coverage for deleted keys, polluted prototypes, and locked holders during JSON operations. |
| packages/maple/docs/divergences.md | Documents the own-properties-only behavior of JSON callbacks. |
Review effort: Lite
…'t fake a value ownDataValue used `'value' in descriptor`, but a descriptor's own prototype is the host Object.prototype, so a `value` the embedding page added there would misread an accessor as a data property. Use Object.hasOwn, which reads the descriptor's own fields only.
Merged
|
🎉 This PR is included in version 0.7.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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 changes
A script can no longer get hold of a host function through
JSON.parseorJSON.stringify.Object.prototype.hasOwnProperty, as the value. The script could hold that reference but not call it. The value is nowundefined.New divergence from JavaScript: a reviver and a replacer read their holder's own data only. For a key that a callback deleted before its visit, native JavaScript reads the prototype; Maple gives
undefined. Nothing changes for scripts that do not delete keys of the holder during the walk.How
support/own-property.ts: newownDataValue(target, key). It returns the value of an own data property andundefinedfor a missing key, an inherited key or an accessor.json/statics/parse.ts:revivereads each visited value withownDataValueinstead ofReflect.get.writeBack, which usesReflect.definePropertyandReflect.deleteProperty. Both returnfalseon a holder that refuses the change instead of throwing, which matches the spec'sCreateDataProperty.json/statics/stringify.ts: the values of a plain object and the elements of an array are read withownDataValue. An array has no index keys on its prototype unless the host page added some, so the array read is hardening.Tests, parity and docs
json.spec.ts: a reviver and a replacer that delete a queuedhasOwnPropertykey seeundefined. A replacer that deletes a queued array element seesundefinedeven whenArray.prototypecarries that index. A reviver that freezes its holder leaves an array and an object unchanged.own-property.spec.ts:ownDataValuefor own data, missing and inherited keys, an accessor that must not be called, and array holes.divergences.mdlists the new divergence and links to the "Own properties only" guarantee insecurity.md.