Skip to content

Keep host prototype functions away from JSON revivers and replacers - #28

Merged
NetWin merged 4 commits into
incrementfrom
fix/json-host-prototype-isolation
Nov 25, 2026
Merged

NetWin merged 4 commits into
incrementfrom
fix/json-host-prototype-isolation

Conversation

@NetWin

@NetWin NetWin commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What changes

A script can no longer get hold of a host function through JSON.parse or JSON.stringify.

  • A reviver or replacer that deleted a key of its holder before that key was visited received the host's own function of that name, such as Object.prototype.hasOwnProperty, as the value. The script could hold that reference but not call it. The value is now undefined.
  • A reviver that freezes, seals or locks its holder no longer ends the run with an error the script cannot catch. The refused write is skipped and the holder keeps its value, as in JavaScript.

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: new ownDataValue(target, key). It returns the value of an own data property and undefined for a missing key, an inherited key or an accessor.
  • json/statics/parse.ts:
    • revive reads each visited value with ownDataValue instead of Reflect.get.
    • The write-back moves into writeBack, which uses Reflect.defineProperty and Reflect.deleteProperty. Both return false on a holder that refuses the change instead of throwing, which matches the spec's CreateDataProperty.
  • json/statics/stringify.ts: the values of a plain object and the elements of an array are read with ownDataValue. 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

  • Tests:
    • json.spec.ts: a reviver and a replacer that delete a queued hasOwnProperty key see undefined. A replacer that deletes a queued array element sees undefined even when Array.prototype carries that index. A reviver that freezes its holder leaves an array and an object unchanged.
    • own-property.spec.ts: ownDataValue for own data, missing and inherited keys, an accessor that must not be called, and array holes.
  • Parity: none added. js-interpreter ignores the reviver, and Node hands the callback its own prototype function, so none of the parity helpers can express the case.
  • Docs: divergences.md lists the new divergence and links to the "Own properties only" guarantee in security.md.

…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.
@NetWin NetWin self-assigned this Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

📋 Release Preview

Changelog

0.7.2 (2026-11-25)

🩹 Bug Fixes

  • json: ignore a reviver write-back to a frozen holder instead of throwing (a4798c1)
  • json: read own data with hasOwn so a polluted host prototype can't fake a value (90be665)
  • json: read reviver values as own data to keep host prototypes out of the sandbox (8960577)
  • json: read stringify values as own data to keep host prototypes out of the sandbox (5552ed3)

🏗 Dependency Updates

  • deps: update dependencies (7810563)

@NetWin
NetWin marked this pull request as ready for review November 25, 2026 11:07
Copilot AI lite review requested due to automatic review settings November 25, 2026 11:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 ownDataValue for 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.
@NetWin
NetWin merged commit 4230fd7 into increment Nov 25, 2026
8 checks passed
@NetWin
NetWin deleted the fix/json-host-prototype-isolation branch November 25, 2026 11:24
@NetWin NetWin mentioned this pull request Nov 25, 2026
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.7.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants