From 12daa54d2e2f065cdf9d30cc94f04d17795305bd Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 17 Sep 2026 23:21:14 +0200 Subject: [PATCH] deps: V8: backport 93cd21e8254b MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: skip fast transition path for escaped keys JSDataObjectBuilder's fast transition path previously compared undecoded source bytes of an escaped key against existing transitions. GetKeyChars() returned undecoded source bytes sliced using the decoded length, so escaped keys could incorrectly match persisted transitions and corrupt the keys of subsequent JSON.parse() calls. This patch fixes it by skipping the fast path when the key has escape characters. Co-Authored-By: 3zrvvv@gmail.com Bug: 521080746 Change-Id: I77720cc34758180224ca8e0e5de0af48556ed013 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8412610 Commit-Queue: Joyee Cheung Reviewed-by: Leszek Swirski Cr-Commit-Position: refs/heads/main@{#109925} Refs: https://github.com/v8/v8/commit/93cd21e8254b65c15ff131b2dcadf19beadb559d Co-authored-by: Joyee Cheung PR-URL: https://github.com/nodejs-private/node-private/pull/999 Refs: https://hackerone.com/reports/3799145 Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: Ulises Gascón --- common.gypi | 2 +- deps/v8/src/json/json-parser.cc | 7 +- deps/v8/src/json/json-parser.h | 6 ++ .../regress-json-escaped-key-transition.js | 76 +++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 deps/v8/test/mjsunit/regress/regress-json-escaped-key-transition.js diff --git a/common.gypi b/common.gypi index 39229ef5878..2967d1e63a4 100644 --- a/common.gypi +++ b/common.gypi @@ -44,7 +44,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.39', + 'v8_embedder_string': '-node.40', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/json/json-parser.cc b/deps/v8/src/json/json-parser.cc index 46995de03a0..30710e97728 100644 --- a/deps/v8/src/json/json-parser.cc +++ b/deps/v8/src/json/json-parser.cc @@ -1067,7 +1067,12 @@ class JSDataObjectBuilder { descriptor_index)), isolate_); target_map = expected_final_map_; - } else { + } else if (key_chars.data() != nullptr) { + // The byte-compare transition fast path is only valid when `key_chars` + // faithfully represent the decoded key. Callers that cannot provide valid + // chars (the JSON parser passes an empty vector for escaped keys, whose raw + // source bytes differ from the decoded key) fall through to the + // decode-then-FindTransitionToField path below. TransitionsAccessor transitions(isolate_, *map_); auto expected_transition = transitions.ExpectedTransition(key_chars); if (!expected_transition.first.is_null()) { diff --git a/deps/v8/src/json/json-parser.h b/deps/v8/src/json/json-parser.h index eb846a94a1a..0e397f16f9c 100644 --- a/deps/v8/src/json/json-parser.h +++ b/deps/v8/src/json/json-parser.h @@ -376,6 +376,12 @@ class JsonParser final { JsonString ScanJsonPropertyKey(JsonContinuation* cont); base::uc32 ScanUnicodeCharacter(); base::Vector GetKeyChars(JsonString key) { + // For escaped keys the source range starting at `key.start()` holds the raw + // (undecoded) characters while `key.length()` is the decoded length, so the + // bytes here do not represent the actual decoded key. Return an empty + // vector to signal that the byte-compare transition fast path must be + // skipped (see JSDataObjectBuilder::TryFastTransitionToPropertyKey). + if (key.has_escape()) return base::Vector(); return base::Vector(chars_ + key.start(), key.length()); } Handle MakeString(const JsonString& string, diff --git a/deps/v8/test/mjsunit/regress/regress-json-escaped-key-transition.js b/deps/v8/test/mjsunit/regress/regress-json-escaped-key-transition.js new file mode 100644 index 00000000000..e1b2421ff82 --- /dev/null +++ b/deps/v8/test/mjsunit/regress/regress-json-escaped-key-transition.js @@ -0,0 +1,76 @@ +// Copyright 2026 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Regression test: JSON.parse must not match a hidden-class transition for an +// escaped property key using the raw (undecoded) source bytes. The builder's +// fast transition path compared `chars_ + key.start()` over `key.length()` +// (the *decoded* length), so escaped keys (e.g. "\t", "\uXXXX") collapsed to +// the byte 0x5C and matched a previously-seen "\\" key, making JSON.parse adopt +// the wrong key. Transitions are isolate-global, so a prior parse corrupted a +// later, unrelated parse. + +// A single-char escaped key must keep its decoded value even after a sibling +// "\\" (backslash) key has been parsed on the same prefix map. +(function TestEscapedKeyNotRenamedByPriorBackslashKey() { + function canaryKeys() { + return Object.keys(JSON.parse('{"\\r":1,"\\f":2}')); + } + assertArrayEquals(['\r', '\f'], canaryKeys()); + // Plants a {} -> "\" transition in the global transition tree. + JSON.parse('{"\\r":3,"\\\\":4}'); + // Must be unaffected: "\f" stays U+000C, not U+005C. + assertArrayEquals(['\r', '\f'], canaryKeys()); +})(); + +// Multi-character escaped key: decoded length 2 must not be compared against +// the first two raw bytes ("\" + "r"). +(function TestMultiCharEscapedKey() { + function keys() { + return Object.keys(JSON.parse('{"\\t":1,"\\r\\r":2}')); + } + assertArrayEquals(['\t', '\r\r'], keys()); + JSON.parse('{"\\t":1,"\\\\r":2}'); // plants the literal "\r" (0x5C 0x72) key + assertArrayEquals(['\t', '\r\r'], keys()); +})(); + +// \uXXXX-escaped single-character key (here decodes to plain 'A'). +(function TestUnicodeEscapedKey() { + function keys() { + return Object.keys(JSON.parse('{"x":1,"\\u0041":2}')); + } + assertArrayEquals(['x', 'A'], keys()); + JSON.parse('{"x":1,"\\\\":2}'); + assertArrayEquals(['x', 'A'], keys()); +})(); + +// Override/collide: an escaped key must not be renamed onto a real "\" sibling +// and silently destroy a property (key count must be preserved). +(function TestNoPropertyDestruction() { + // Prime the transition state that previously triggered the collapse. + JSON.parse('{"p":1,"\\\\":0}'); + JSON.parse('{"p":1,"\\\\":0,"\\t":0}'); + const o = JSON.parse('{"p":1,"\\t":"a","\\\\":"b"}'); + assertEquals(3, Object.keys(o).length); + assertEquals('a', o['\t']); + assertEquals('b', o['\\']); +})(); + +// Self-contained single document: a later sibling object value must not be +// corrupted by an earlier one within the same parse. +(function TestNestedSiblingNotCorrupted() { + const o = JSON.parse( + '{"a":{"p":1,"\\\\":9},"b":{"p":1,"\\\\":"A","\\t":7},' + + '"c":{"p":1,"\\t":7,"\\\\":"B"}}'); + assertEquals(3, Object.keys(o.c).length); + assertEquals(7, o.c['\t']); + assertEquals('B', o.c['\\']); +})(); + +// A plain (unescaped) empty key must still work (regression guard for the +// empty-key_chars path the fix relies on). +(function TestEmptyKeyStillWorks() { + const o = JSON.parse('{"a":1,"":2,"b":3}'); + assertArrayEquals(['a', '', 'b'], Object.keys(o)); + assertEquals(2, o['']); +})();