From 89605778eff9f904de126819e4c1f0eeb4fc263f Mon Sep 17 00:00:00 2001 From: NetWin Date: Wed, 25 Nov 2026 11:44:50 +0100 Subject: [PATCH 1/4] fix(json): read reviver values as own data to keep host prototypes out 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. --- packages/maple/docs/divergences.md | 3 ++ packages/maple/src/builtins/json/json.spec.ts | 16 +++++++ .../maple/src/builtins/json/statics/parse.ts | 7 ++- .../maple/src/support/own-property.spec.ts | 43 ++++++++++++++++++- packages/maple/src/support/own-property.ts | 13 ++++++ 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/packages/maple/docs/divergences.md b/packages/maple/docs/divergences.md index ef44d1b..0aed1a1 100644 --- a/packages/maple/docs/divergences.md +++ b/packages/maple/docs/divergences.md @@ -52,6 +52,9 @@ one that does; the linked section has the detail. - Set algebra refuses set-like objects other than Sets and Maps, and `Iterator.from` refuses an object with a script `next` ([Built-ins](builtins.md#map-and-set)). - A JSON reviver receives no third `context` argument ([JSON](builtins.md#json)). +- A JSON reviver reads its holder's own data only, so a key a callback deletes before it is + visited reads as `undefined`, where native JavaScript would read the host prototype + ([Security](security.md#how-the-guarantees-hold)). - The legacy `RegExp.$1` statics, the String HTML methods and `Intl` are not available. ## Errors and limits diff --git a/packages/maple/src/builtins/json/json.spec.ts b/packages/maple/src/builtins/json/json.spec.ts index 28fafdd..941fbcf 100644 --- a/packages/maple/src/builtins/json/json.spec.ts +++ b/packages/maple/src/builtins/json/json.spec.ts @@ -99,6 +99,22 @@ describe('JSON.parse', () => { ).toBe('LIMIT_EXCEEDED'); }); + it('reads reviver values as own data, never through the host prototype', () => { + const code = ` + const seen = []; + JSON.parse('{"a":0,"hasOwnProperty":0}', function (k, v) { + if (k === 'a') { delete this.hasOwnProperty; } + if (k === 'hasOwnProperty') { seen.push(typeof v); } + return v; + }); + return seen; + `; + + const seen = run(code); + + expect(seen).toEqual(['undefined']); + }); + it('refuses to revive text nested deeper than 1000 levels with a catchable RangeError', () => { const deep = '['.repeat(20_000) + ']'.repeat(20_000); diff --git a/packages/maple/src/builtins/json/statics/parse.ts b/packages/maple/src/builtins/json/statics/parse.ts index 503bad2..2ee4d20 100644 --- a/packages/maple/src/builtins/json/statics/parse.ts +++ b/packages/maple/src/builtins/json/statics/parse.ts @@ -1,6 +1,6 @@ import { isPlainObject } from '../../../support/is-plain-object.ts'; import { MAX_NESTING } from '../../../support/nesting-limit.ts'; -import { defineOwn } from '../../../support/own-property.ts'; +import { defineOwn, ownDataValue } from '../../../support/own-property.ts'; import { toString } from '../../../values/coercion.ts'; import { type CallContext, @@ -41,7 +41,10 @@ function revive( `A value nested deeper than ${MAX_NESTING} levels cannot be revived` ); } - const value: unknown = Reflect.get(holder, key); + // A reviver callback runs with the holder as `this` and can delete a key that the enclosing walk + // has already queued. Reading own data only keeps a now-missing key from resolving against the + // host prototype and handing the callback a live host reference. + const value: unknown = ownDataValue(holder, key); if (Array.isArray(value)) { for (let index = 0; index < value.length; index++) { const revived = revive(value, String(index), reviver, context, depth + 1); diff --git a/packages/maple/src/support/own-property.spec.ts b/packages/maple/src/support/own-property.spec.ts index 92530a5..72e1ee3 100644 --- a/packages/maple/src/support/own-property.spec.ts +++ b/packages/maple/src/support/own-property.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it, vi } from 'vitest'; -import { defineOwn } from './own-property.ts'; +import { defineOwn, ownDataValue } from './own-property.ts'; const DATA_PROPERTY = { enumerable: true, writable: true, configurable: true }; @@ -79,3 +79,44 @@ describe('defineOwn', () => { }); }); }); + +describe('ownDataValue', () => { + it('returns the value of an own data property, enumerable or not', () => { + const target = Object.defineProperty({ a: 1 }, 'hidden', { value: 2 }); + + const values = [ownDataValue(target, 'a'), ownDataValue(target, 'hidden')]; + + expect(values).toEqual([1, 2]); + }); + + it('returns undefined for a missing key and for one the prototype answers', () => { + const target = {}; + + const values = [ownDataValue(target, 'a'), ownDataValue(target, 'hasOwnProperty')]; + + expect(values).toEqual([undefined, undefined]); + }); + + it('returns undefined for an own accessor without calling it', () => { + const get = vi.fn<() => unknown>(() => 1); + const target = Object.defineProperty({}, 'probe', { get }); + + const value = ownDataValue(target, 'probe'); + + expect(value).toBeUndefined(); + expect(get).not.toHaveBeenCalled(); + }); + + it('returns undefined for an array hole and for an index past the end', () => { + // oxlint-disable-next-line no-sparse-arrays -- the hole is what is being read + const target = [1, , 3]; + + const values = [ + ownDataValue(target, '0'), + ownDataValue(target, '1'), + ownDataValue(target, '5') + ]; + + expect(values).toEqual([1, undefined, undefined]); + }); +}); diff --git a/packages/maple/src/support/own-property.ts b/packages/maple/src/support/own-property.ts index 98a39cc..7e28e5b 100644 --- a/packages/maple/src/support/own-property.ts +++ b/packages/maple/src/support/own-property.ts @@ -15,3 +15,16 @@ export function defineOwn(target: object, key: string, value: unknown): void { configurable: true }); } + +/** + * The target's own data value under `key`, or `undefined` when the key is missing, inherited or an + * accessor. Reading own data keeps a visit from falling through to the host prototype when a key + * is no longer own. + */ +export function ownDataValue(target: object, key: string): unknown { + const descriptor = Object.getOwnPropertyDescriptor(target, key); + if (descriptor === undefined || !('value' in descriptor)) { + return undefined; + } + return descriptor.value; +} From a4798c17f24d9be77a6ca9fbb13f4dd16b8f050f Mon Sep 17 00:00:00 2001 From: NetWin Date: Wed, 25 Nov 2026 11:45:51 +0100 Subject: [PATCH 2/4] fix(json): ignore a reviver write-back to a frozen holder instead of 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. --- packages/maple/src/builtins/json/json.spec.ts | 18 ++++++++++ .../maple/src/builtins/json/statics/parse.ts | 36 ++++++++++++------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/maple/src/builtins/json/json.spec.ts b/packages/maple/src/builtins/json/json.spec.ts index 941fbcf..bb62621 100644 --- a/packages/maple/src/builtins/json/json.spec.ts +++ b/packages/maple/src/builtins/json/json.spec.ts @@ -115,6 +115,24 @@ describe('JSON.parse', () => { expect(seen).toEqual(['undefined']); }); + it('ignores a reviver write-back to a frozen holder, like the host', () => { + const array = run( + `return JSON.parse('[1,2]', function (k, v) { + if (k === '0') { Object.freeze(this); } + return typeof v === 'number' ? v + 100 : v; + });` + ); + const object = run( + `return JSON.parse('{"a":1}', function (k, v) { + if (k === 'a') { Object.freeze(this); } + return typeof v === 'number' ? v + 100 : v; + });` + ); + + expect(array).toEqual([1, 2]); + expect(object).toEqual({ a: 1 }); + }); + it('refuses to revive text nested deeper than 1000 levels with a catchable RangeError', () => { const deep = '['.repeat(20_000) + ']'.repeat(20_000); diff --git a/packages/maple/src/builtins/json/statics/parse.ts b/packages/maple/src/builtins/json/statics/parse.ts index 2ee4d20..700f3da 100644 --- a/packages/maple/src/builtins/json/statics/parse.ts +++ b/packages/maple/src/builtins/json/statics/parse.ts @@ -47,22 +47,34 @@ function revive( const value: unknown = ownDataValue(holder, key); if (Array.isArray(value)) { for (let index = 0; index < value.length; index++) { - const revived = revive(value, String(index), reviver, context, depth + 1); - if (revived === undefined) { - Reflect.deleteProperty(value, String(index)); - } else { - value[index] = revived; - } + writeBack(value, String(index), revive(value, String(index), reviver, context, depth + 1)); } } else if (isPlainObject(value)) { for (const propertyKey of Object.keys(value)) { - const revived = revive(value, propertyKey, reviver, context, depth + 1); - if (revived === undefined) { - Reflect.deleteProperty(value, propertyKey); - } else { - defineOwn(value, propertyKey, revived); - } + writeBack(value, propertyKey, revive(value, propertyKey, reviver, context, depth + 1)); } } return context.call(holder, reviver, [key, value]); } + +/** + * Writes a revived value back as an own data property, or removes it when the reviver returned + * `undefined`. A callback can freeze its holder mid-walk; like the spec's CreateDataProperty, a + * write that a frozen holder refuses is left as it is rather than thrown to the host. + */ +function writeBack( + holder: Record | Array, + key: string, + revived: unknown +): void { + if (revived === undefined) { + Reflect.deleteProperty(holder, key); + return; + } + Reflect.defineProperty(holder, key, { + value: revived, + writable: true, + enumerable: true, + configurable: true + }); +} From 5552ed36d35d18913a9b57c0f60cd2cb5907c393 Mon Sep 17 00:00:00 2001 From: NetWin Date: Wed, 25 Nov 2026 11:46:27 +0100 Subject: [PATCH 3/4] fix(json): read stringify values as own data to keep host prototypes 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. --- packages/maple/docs/divergences.md | 4 +- packages/maple/src/builtins/json/json.spec.ts | 43 +++++++++++++++++++ .../src/builtins/json/statics/stringify.ts | 10 +++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/packages/maple/docs/divergences.md b/packages/maple/docs/divergences.md index 0aed1a1..87d667e 100644 --- a/packages/maple/docs/divergences.md +++ b/packages/maple/docs/divergences.md @@ -52,8 +52,8 @@ one that does; the linked section has the detail. - Set algebra refuses set-like objects other than Sets and Maps, and `Iterator.from` refuses an object with a script `next` ([Built-ins](builtins.md#map-and-set)). - A JSON reviver receives no third `context` argument ([JSON](builtins.md#json)). -- A JSON reviver reads its holder's own data only, so a key a callback deletes before it is - visited reads as `undefined`, where native JavaScript would read the host prototype +- A JSON reviver and replacer read their holder's own data only, so a key a callback deletes + before it is visited reads as `undefined`, where native JavaScript would read the host prototype ([Security](security.md#how-the-guarantees-hold)). - The legacy `RegExp.$1` statics, the String HTML methods and `Intl` are not available. diff --git a/packages/maple/src/builtins/json/json.spec.ts b/packages/maple/src/builtins/json/json.spec.ts index bb62621..45f7023 100644 --- a/packages/maple/src/builtins/json/json.spec.ts +++ b/packages/maple/src/builtins/json/json.spec.ts @@ -256,6 +256,49 @@ describe('JSON.stringify', () => { ).toBe('TypeError'); }); + it('reads replacer values as own data, never through the host prototype', () => { + const code = ` + const seen = []; + JSON.stringify({ a: 0, hasOwnProperty: 0 }, function (k, v) { + if (k === 'a') { delete this.hasOwnProperty; } + if (k === 'hasOwnProperty') { seen.push(typeof v); } + return v; + }); + return seen; + `; + + const seen = run(code); + + expect(seen).toEqual(['undefined']); + }); + + it('reads array elements as own data, never through the host prototype', () => { + const code = ` + const seen = []; + const text = JSON.stringify([0, 1], function (k, v) { + if (k === '0') { delete this[1]; } + if (k === '1') { seen.push(typeof v); } + return v; + }); + return [text, seen]; + `; + // oxlint-disable-next-line no-extend-native -- stands in for a host page that polluted the prototype + Object.defineProperty(Array.prototype, '1', { + value: 'host', + writable: true, + configurable: true + }); + + let result: unknown; + try { + result = run(code); + } finally { + Reflect.deleteProperty(Array.prototype, '1'); + } + + expect(result).toEqual(['[0,null]', ['undefined']]); + }); + it("writes an array replacer's keys in its order, own properties only", () => { expect(run('return JSON.stringify({ a: 1, b: 2 }, ["b", "a", "b"]);')).toBe('{"b":2,"a":1}'); expect(run('return JSON.stringify({ a: 1 }, ["a", "missing", "toString"]);')).toBe('{"a":1}'); diff --git a/packages/maple/src/builtins/json/statics/stringify.ts b/packages/maple/src/builtins/json/statics/stringify.ts index 9f29c76..b616134 100644 --- a/packages/maple/src/builtins/json/statics/stringify.ts +++ b/packages/maple/src/builtins/json/statics/stringify.ts @@ -1,6 +1,6 @@ import { isPlainObject } from '../../../support/is-plain-object.ts'; import { MAX_NESTING } from '../../../support/nesting-limit.ts'; -import { defineOwn } from '../../../support/own-property.ts'; +import { defineOwn, ownDataValue } from '../../../support/own-property.ts'; import { getOwnPropertiesOf } from '../../../values/members.ts'; import { type CallContext, isCallable, NativeFunction } from '../../../values/native.ts'; import { SandboxObject } from '../../../values/sandbox-object.ts'; @@ -150,7 +150,9 @@ function serialize( const parts: Array = []; const length = current.length; for (let index = 0; index < length; index++) { - parts.push(serialize(current, String(index), current[index], inner, serialization) ?? 'null'); + // A replacer can delete an element the walk has not reached; own data only, as below + const element = ownDataValue(current, String(index)); + parts.push(serialize(current, String(index), element, inner, serialization) ?? 'null'); } result = wrap('[', ']', parts, gap, indent, inner); } else { @@ -160,7 +162,9 @@ function serialize( if (isPlainObject(current)) { properties = { has: (propertyKey: string): boolean => Object.hasOwn(current, propertyKey), - get: (propertyKey: string): unknown => current[propertyKey], + // A replacer runs with the holder as `this` between the key snapshot and this read; own + // data only keeps a deleted key from resolving against the host prototype. + get: (propertyKey: string): unknown => ownDataValue(current, propertyKey), keys: (): Array => Object.keys(current) }; } else { From 90be6659506dfc34f66a303ee028a98ac1e356f7 Mon Sep 17 00:00:00 2001 From: NetWin Date: Wed, 25 Nov 2026 12:22:01 +0100 Subject: [PATCH 4/4] fix(json): read own data with hasOwn so a polluted host prototype can'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. --- .../maple/src/support/own-property.spec.ts | 21 +++++++++++++++++++ packages/maple/src/support/own-property.ts | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/maple/src/support/own-property.spec.ts b/packages/maple/src/support/own-property.spec.ts index 72e1ee3..998ed78 100644 --- a/packages/maple/src/support/own-property.spec.ts +++ b/packages/maple/src/support/own-property.spec.ts @@ -107,6 +107,27 @@ describe('ownDataValue', () => { expect(get).not.toHaveBeenCalled(); }); + it('returns undefined for an own accessor even when the host prototype carries a value', () => { + const get = vi.fn<() => unknown>(() => 'host'); + const target = Object.defineProperty({}, 'probe', { + get, + enumerable: true, + configurable: true + }); + // oxlint-disable-next-line no-extend-native -- stands in for a host page that polluted the prototype + Object.defineProperty(Object.prototype, 'value', { value: 'polluted', configurable: true }); + + let result: unknown; + try { + result = ownDataValue(target, 'probe'); + } finally { + Reflect.deleteProperty(Object.prototype, 'value'); + } + + expect(result).toBeUndefined(); + expect(get).not.toHaveBeenCalled(); + }); + it('returns undefined for an array hole and for an index past the end', () => { // oxlint-disable-next-line no-sparse-arrays -- the hole is what is being read const target = [1, , 3]; diff --git a/packages/maple/src/support/own-property.ts b/packages/maple/src/support/own-property.ts index 7e28e5b..1aa099c 100644 --- a/packages/maple/src/support/own-property.ts +++ b/packages/maple/src/support/own-property.ts @@ -23,7 +23,10 @@ export function defineOwn(target: object, key: string, value: unknown): void { */ export function ownDataValue(target: object, key: string): unknown { const descriptor = Object.getOwnPropertyDescriptor(target, key); - if (descriptor === undefined || !('value' in descriptor)) { + // `Object.hasOwn`, not `'value' in descriptor`: the descriptor's own prototype is the host + // `Object.prototype`, so `in` would read a `value` the embedding page added there and misread an + // accessor as data. + if (descriptor === undefined || !Object.hasOwn(descriptor, 'value')) { return undefined; } return descriptor.value;