From fe741ffe8c3aee693b7bded3ceeb44c2639faa6e Mon Sep 17 00:00:00 2001 From: LeoTM <1881059+leotm@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:29:23 +0000 Subject: [PATCH 1/4] Fix structuredClone WeakMap/WeakSet/Promise prototype mutations for Hardened JS (SES) Fix: https://github.com/react/react-native/issues/60002 --- .../structuredClone/structuredClone.js | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/react-native/src/private/webapis/structuredClone/structuredClone.js b/packages/react-native/src/private/webapis/structuredClone/structuredClone.js index 820534ef2a57..ef64df93f736 100644 --- a/packages/react-native/src/private/webapis/structuredClone/structuredClone.js +++ b/packages/react-native/src/private/webapis/structuredClone/structuredClone.js @@ -295,8 +295,6 @@ export default function structuredClone(value: T): T { } } -const NON_SERIALIZABLE_OBJECT_KEY = Symbol('nonSerializableObject'); - function cloneArrayBuffer(value: ArrayBuffer): ArrayBuffer { const existingClone = memory.get(value); if (existingClone instanceof ArrayBuffer) { @@ -392,16 +390,11 @@ function createErrorClone(value: Error): Error { } function isNonSerializableObject(obj: T): boolean { - // $FlowExpectedError[invalid-in-lhs] - return NON_SERIALIZABLE_OBJECT_KEY in obj; -} - -function markClassAsNonSerializable(cls: Class): void { - // $FlowExpectedError[incompatible-use] - cls.prototype[NON_SERIALIZABLE_OBJECT_KEY] = true; + // Built-in prototypes must not be written to because Hardened JS + // environments (SES) freeze them, so the non-serializable + // built-ins are detected with `instanceof` instead of marking the + // prototypes with e.g. Symbol('nonSerializableObject'). + return ( + obj instanceof WeakMap || obj instanceof WeakSet || obj instanceof Promise + ); } - -// Non-serializable built-ins. -markClassAsNonSerializable(WeakMap); -markClassAsNonSerializable(WeakSet); -markClassAsNonSerializable(Promise); From c1c6035deb4936604309d476770d7d24be01c4e1 Mon Sep 17 00:00:00 2001 From: LeoTM <1881059+leotm@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:31:59 +0000 Subject: [PATCH 2/4] Add tests for WeakMap, WeakSet and Promise --- .../__tests__/structuredClone-test.js | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js diff --git a/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js new file mode 100644 index 000000000000..32f2c32443a2 --- /dev/null +++ b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js @@ -0,0 +1,57 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import DOMException from '../../errors/DOMException'; + +// Snapshot the built-in prototypes before the first evaluation of the +// module under test. `structuredClone` is required (not imported) below so +// its top-level code runs after this snapshot, which is what lets the +// no-mutation regression test detect writes to the built-in prototypes. +const PROTOTYPE_SYMBOLS = { + WeakMap: Object.getOwnPropertySymbols(WeakMap.prototype), + WeakSet: Object.getOwnPropertySymbols(WeakSet.prototype), + Promise: Object.getOwnPropertySymbols(Promise.prototype), +}; + +function expectDataCloneError(fn: () => unknown) { + try { + fn(); + } catch (error) { + expect(error).toBeInstanceOf(DOMException); + expect(error.name).toBe('DataCloneError'); + return; + } + + throw new Error('Expected function to throw DataCloneError, but it did not'); +} + +describe('structuredClone', () => { + it('does not add properties to built-in prototypes', () => { + require('../structuredClone'); + + expect(Object.getOwnPropertySymbols(WeakMap.prototype)).toEqual( + PROTOTYPE_SYMBOLS.WeakMap, + ); + expect(Object.getOwnPropertySymbols(WeakSet.prototype)).toEqual( + PROTOTYPE_SYMBOLS.WeakSet, + ); + expect(Object.getOwnPropertySymbols(Promise.prototype)).toEqual( + PROTOTYPE_SYMBOLS.Promise, + ); + }); + + it('throws DataCloneError for non-serializable built-ins', () => { + const {default: structuredClone} = require('../structuredClone'); + + expectDataCloneError(() => structuredClone(new WeakMap())); + expectDataCloneError(() => structuredClone(new WeakSet())); + expectDataCloneError(() => structuredClone(new Promise(() => {}))); + }); +}); From 4eda2136544231db8b497aba725569d68fb734a7 Mon Sep 17 00:00:00 2001 From: LeoTM <1881059+leotm@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:56:08 +0000 Subject: [PATCH 3/4] Revert "Add tests for WeakMap, WeakSet and Promise" This reverts commit c1c6035deb4936604309d476770d7d24be01c4e1. https://github.com/react/react-native/pull/60003#pullrequestreview-5924800311 --- .../__tests__/structuredClone-test.js | 57 ------------------- 1 file changed, 57 deletions(-) delete mode 100644 packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js diff --git a/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js deleted file mode 100644 index 32f2c32443a2..000000000000 --- a/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-test.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict-local - * @format - */ - -import DOMException from '../../errors/DOMException'; - -// Snapshot the built-in prototypes before the first evaluation of the -// module under test. `structuredClone` is required (not imported) below so -// its top-level code runs after this snapshot, which is what lets the -// no-mutation regression test detect writes to the built-in prototypes. -const PROTOTYPE_SYMBOLS = { - WeakMap: Object.getOwnPropertySymbols(WeakMap.prototype), - WeakSet: Object.getOwnPropertySymbols(WeakSet.prototype), - Promise: Object.getOwnPropertySymbols(Promise.prototype), -}; - -function expectDataCloneError(fn: () => unknown) { - try { - fn(); - } catch (error) { - expect(error).toBeInstanceOf(DOMException); - expect(error.name).toBe('DataCloneError'); - return; - } - - throw new Error('Expected function to throw DataCloneError, but it did not'); -} - -describe('structuredClone', () => { - it('does not add properties to built-in prototypes', () => { - require('../structuredClone'); - - expect(Object.getOwnPropertySymbols(WeakMap.prototype)).toEqual( - PROTOTYPE_SYMBOLS.WeakMap, - ); - expect(Object.getOwnPropertySymbols(WeakSet.prototype)).toEqual( - PROTOTYPE_SYMBOLS.WeakSet, - ); - expect(Object.getOwnPropertySymbols(Promise.prototype)).toEqual( - PROTOTYPE_SYMBOLS.Promise, - ); - }); - - it('throws DataCloneError for non-serializable built-ins', () => { - const {default: structuredClone} = require('../structuredClone'); - - expectDataCloneError(() => structuredClone(new WeakMap())); - expectDataCloneError(() => structuredClone(new WeakSet())); - expectDataCloneError(() => structuredClone(new Promise(() => {}))); - }); -}); From e41db8d9187e2211c07e096f2638baaa92668b5e Mon Sep 17 00:00:00 2001 From: LeoTM <1881059+leotm@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:56:53 +0000 Subject: [PATCH 4/4] Extend itest instead to fail on WeakMap/WeakSet/Promise prototype mutations https://github.com/react/react-native/pull/60003#pullrequestreview-5924800311 --- .../structuredClone/__tests__/structuredClone-itest.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-itest.js b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-itest.js index 03ff093daffb..cfcd464c43db 100644 --- a/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-itest.js +++ b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-itest.js @@ -117,6 +117,16 @@ describe('structuredClone', () => { expectDataCloneError(() => structuredClone(Promise.resolve(4))); }); + it('does not write to WeakMap, WeakSet, or Promise prototypes', () => { + Object.freeze(WeakMap.prototype); + Object.freeze(WeakSet.prototype); + Object.freeze(Promise.prototype); + + expectDataCloneError(() => structuredClone(new WeakMap())); + expectDataCloneError(() => structuredClone(new WeakSet())); + expectDataCloneError(() => structuredClone(new Promise(() => {}))); + }); + it('clones simple objects', () => { const value = {foo: 'bar'}; const clone = structuredClone(value);