Repository navigation
fix(v4): check array, set and map size bounds before parsing elements - #6652
Merged
Merged
Conversation
.max(), .length() and .size() ran after every element was walked, so an oversized input built one issue per bad element before the bound was reported. Leading size checks now reject the input first, both in the interpreter and in the compiled path. Fixes #6643
Contributor
|
One small thing worth flagging: this doesn't cover the unbounded case from the CVE. Making z.array(z.string().uuid()).safeParse(Array(1_000_000).fill(0));
// still 1M issues
z.array(z.string().uuid()).safeParse(Array(1_000_000).fill(0), { abortEarly: true });
// 1 issue
|
3 of 4 tasks
This was referenced Nov 1, 2026
5 tasks done
6 tasks
4 tasks
1 task
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
.max(),.length()and.size()on arrays, sets and maps now reject an oversized input before any element is parsed. The bound used to run after the walk, so it bounded nothing:z.array(z.string().uuid()).max(100)given a million numbers built 1,000,001 issues before it reportedtoo_big.Only size checks that come before every other check take part, since something like
.overwrite()can shrink the value first. The early rejection aborts, so a later.refine()never sees elements that weren't parsed, and an oversized input reports just thetoo_bigissue rather than its element issues too. The compiled path gets the same check ahead of its loop. Unbounded containers are unchanged.parseonly on schemas that carry an upper bound, so an unbounded container runs the same code as before.z.array(z.string()).max(100)over 10 items: 65.9 → 67.4 ns (+1–3%, interleaved, but taken at load 10–20). Construction within noise.zod/miniarray fixture +243 B gzipped (3311 → 3554, +7.3%), +227 with amaxLengthcheck. Classic array +215 (+1.0%). Bundles without an array, set or map pay nothing (zod-mini-objectunchanged).Putting the check inside the existing parse closure was smaller (~190 B) but pushed it past V8's inlining budget:
z.array(z.string()).max(n)parses went 58 → 71 ns.Fixes #6643