From e4dd725b647a7ce36ff29cb880d1c7dc8a487580 Mon Sep 17 00:00:00 2001 From: Gang Zhao Date: Mon, 23 Nov 2026 10:06:53 -0800 Subject: [PATCH] Make a "__proto__" key an own property in valueFromDynamic Summary: `jsi::valueFromDynamic` fills each new object with `setProperty`, which acts like JS assignment and consults the prototype chain, while `JSON.parse` defines own data properties. So a `"__proto__"` key in native data, such as server JSON parsed with folly and passed to JS by React Native, calls the `__proto__` setter on `Object.prototype` instead of becoming an own property: an object or null value replaces the converted object's prototype, and any other value is dropped. Fix, without a JSI API change: an object whose `folly::dynamic` has a `"__proto__"` key is filled while its prototype is null, then gets its prototype back. With no prototype chain, the store defines an own data property. Other objects, and arrays, are converted as before, so they keep their hidden classes; the added cost is one key lookup per object. Assignment also differs from `JSON.parse` when `Object.prototype` or `Array.prototype` has an accessor or a read-only property. That needs a modified prototype, which should be prevented where the prototype is modified, so those cases are left alone. For the same reason, `jsi::Array::createWithElements` is unchanged. On JSC this is incomplete: JSCRuntime cuts property names at the first NUL, so a key such as `"__proto__\0x"` becomes `"__proto__"` without matching the lookup, and can still call the setter. A code comment records this existing JSCRuntime limitation. Changelog: [General][Fixed] - Fix "__proto__" handling in JSI `valueFromDynamic` Differential Revision: D132359056 --- .../ReactCommon/jsi/jsi/JSIDynamic.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp b/packages/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp index 842e2dce1d1d..3868bb67aa6f 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp @@ -84,6 +84,23 @@ Value valueFromDynamic(Runtime& runtime, const folly::dynamic& dynInput) { } case folly::dynamic::OBJECT: { Object obj = std::move(top.obj); + // Storing a "__proto__" key into an object whose prototype is + // Object.prototype calls the inherited __proto__ setter + // (ES2026 10.1.9.2, step 6), which replaces the prototype with an + // object or null value and drops any other value + // (ES2026 20.1.3.8.2). JSON.parse makes the key an own property + // instead (ES2026 13.2.5.6, step 2), so an object with that key is + // filled while its prototype is null. + // + // JSCRuntime cuts property names at the first NUL, so on JSC a key + // such as "__proto__\0x" is stored as "__proto__" too. This lookup + // doesn't match it, so it can still call the setter. + bool hasProtoKey = top.dyn->count("__proto__"); + Value prototype; + if (hasProtoKey) { + prototype = obj.getPrototype(runtime); + obj.setPrototype(runtime, Value::null()); + } for (const auto& element : top.dyn->items()) { if (element.first.isNumber() || element.first.isString()) { obj.setProperty( @@ -92,6 +109,9 @@ Value valueFromDynamic(Runtime& runtime, const folly::dynamic& dynInput) { valueFromDynamicShallow(runtime, stack, element.second)); } } + if (hasProtoKey) { + obj.setPrototype(runtime, prototype); + } break; } default: