Skip to content

fix(web_core): stop parsing array items once maxItems fails - #4399

Open
pierreespitia wants to merge 4 commits into
a2ui-project:mainfrom
pierreespitia:fix/web-core-array-max-short-circuit
Open

pierreespitia wants to merge 4 commits into
a2ui-project:mainfrom
pierreespitia:fix/web-core-array-max-short-circuit

Conversation

@pierreespitia

Copy link
Copy Markdown

Description

Catalog.fromJson and AgentFunction schemas turn a JSON Schema maxItems into z.array().max() (schema_loader.ts). On Zod 3, a failed .max() is recorded but every item is still parsed, and the uniqueItems refinement then runs over the whole array. So an over-long array prop costs as much to validate as if there were no maxItems at all. That is the behaviour behind GHSA-mrhx-c7xc-m3w5, and it is reachable from web_core through catalog maxItems, not only from user-level zod code.

This PR checks the length first, in a fatal z.preprocess step, so parsing stops before the array schema sees the value:

  • Works the same on Zod 3 and Zod 4. Zod 4.6.6+ already short-circuits on its own, and the guard is then redundant but harmless. It does not depend on the zod/v3 stopgap discussed in [FEATURE]: Stopgap for GHSA-mrhx-c7xc-m3w5: depend on zod ^4.6.6 and import from "zod/v3" #4192, which keeps the unfixed v3 code path.
  • The schema loader already uses z.preprocess for not. zod-to-json-schema describes a preprocess by its inner schema, so the generated JSON Schema still carries maxItems.
  • An over-long array now reports a single too_big issue instead of too_big plus item errors.

Related: #4192 (stopgap discussion), #3825 (Zod 4 error shapes), #2753 (version-agnostic GenericBinder), #4352 (drop zod), #2160 / #2002 (Zod 4 support). This change is independent of all of them.

Test

New test in schema_loader.test.ts: a component with maxItems: 2 and uniqueItems gets a 3-item array wrapped in a Proxy that counts element reads.

Before (main, zod 3.25.76):

✖ rejects an array over maxItems without visiting its items
    actual: 3,
    expected: 0,
ℹ tests 32
ℹ pass 31
ℹ fail 1

After:

✔ rejects an array over maxItems without visiting its items
ℹ tests 32
ℹ pass 32
ℹ fail 0

Full yarn test in typescript/core/web_core (build + conformance + unit): 1659 / 1659 pass. Prettier is clean, and eslint reports no new warnings.

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a validation vulnerability (GHSA-mrhx-c7xc-m3w5) by ensuring that arrays exceeding maxItems fail validation on their length before their items are parsed or checked for uniqueness. This is achieved by introducing a rejectOversizedArray preprocessor function in schema_loader.ts that adds a fatal too_big issue early. A corresponding unit test using a Proxy has been added to schema_loader.test.ts to verify that array items are not accessed when the limit is exceeded. There are no review comments, and the changes look solid, so I have no feedback to provide.

@pierreespitia
pierreespitia force-pushed the fix/web-core-array-max-short-circuit branch from 2a047b5 to b37f294 Compare November 24, 2026 13:11
@polina-c

polina-c commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

From triage bot, analysis:

For humans

TL;DR
Case: this external PR is a small security fix with a test in the web_core schema loader, which draft #4352 would remove.
Suggestion: assign gspencergoog and add status: assigned-for-triage, so that they decide on it.

Summary:
pierreespitia makes an array property with maxItems in a catalog loaded by Catalog.fromJson (or in an AgentFunction schema) fail on its length before zod parses its items or runs the uniqueItems check. On zod 3, a failed .max() still parses every item, which is the problem behind GHSA-mrhx-c7xc-m3w5.

Targeted issues:
#4192

Why flagged for triage:
This PR has no assignee.

Suggestion:
Assign gspencergoog and add status: assigned-for-triage, type: bug, component: core sdk and lang: typescript. gspencergoog wrote 7 of the last 15 commits to schema_loader.ts. #4192 is assigned to Varun-S10, who asked gspencergoog to look at it, and the suggestion on #4386 also names gspencergoog, so the same person decides on all three.

What I checked:

  1. The change is minor: one helper in schema_loader.ts that wraps the array schema in a fatal z.preprocess length check, a test that counts item reads with a Proxy, and a CHANGELOG entry. The CLA check passes.
  2. It overlaps with work in progress. Draft #4352 deletes schema_loader.ts and drops zod from web_core, and the team has not decided on that direction yet (see #2160). #4349, #4285 and #4057 also edit schema_loader.ts, but not the array conversion. fix(web_core): enforce JSON Schema Draft 2020-12 keywords in Zod conversion (#3998) #4285 is already assigned to gspencergoog.
  3. The team can maintain it: the helper is used in one place, and the PR says that zod 4.6.6 and later already stop early, so it does no harm after a zod 4 migration.

The expert needs to decide whether to take this fix for the zod 3 code now, or wait for the direction of #4352. web_core stays on zod 3: the root package.json pins zod to ^3.25.76.

The PR works on #4192 but does not close it. #4192 first proposed to depend on zod ^4.6.6 and import from zod/v3, and pierreespitia showed that zod/v3 does not have the advisory's fix, which samanosr confirmed. #4386 bumps zod in the web_core package.json to ^4.6.6 and does not touch the code.

  1. Add labels: status: assigned-for-triage, type: bug, component: core sdk, lang: typescript
  2. Assign: gspencergoog
  3. Related items: feat(web_core)!: validate against the spec JSON Schema with an app-supplied engine, drop zod #4352, fix(web_core): check v1.0 catalogs read from JSON against the v1.0 common types #4349, fix(web_core): enforce JSON Schema Draft 2020-12 keywords in Zod conversion (#3998) #4285, fix(web_core): enforce minProperties, maxProperties and typeless not in JSON schemas #4057, chore(deps): bump the npm_and_yarn group across 3 directories with 15 updates #4386, [FEATURE]: Abstract dependency on Zod on web_core. #2160

Reply:

From triage:

Thanks for the fix and for the test that counts item reads. Assigning this PR to gspencergoog, who maintains the web_core schema loader, to decide on it. Draft #4352 would remove schema_loader.ts, so the decision also depends on the direction the team takes in #2160.

To adjust the analysis and apply its suggestion, open it in the hosted triage UI, or run in your a2ui checkout:

tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4399
For software
<a2ui>
root = TriageAnalysis(
  tldr="""Case: this external PR is a small security fix with a test in the `web_core` schema loader, which draft [#4352](https://github.com/a2ui-project/a2ui/pull/4352) would remove.
Suggestion: assign `gspencergoog` and add `status: assigned-for-triage`, so that they decide on it.""",
  summary="""`pierreespitia` makes an array property with `maxItems` in a catalog loaded by `Catalog.fromJson` (or in an `AgentFunction` schema) fail on its length before zod parses its items or runs the `uniqueItems` check. On zod 3, a failed `.max()` still parses every item, which is the problem behind [GHSA-mrhx-c7xc-m3w5](https://github.com/advisories/GHSA-mrhx-c7xc-m3w5).""",
  reason="This PR has no assignee.",
  suggestion="""Assign `gspencergoog` and add `status: assigned-for-triage`, `type: bug`, `component: core sdk` and `lang: typescript`. `gspencergoog` wrote [7 of the last 15 commits](https://github.com/a2ui-project/a2ui/commits/main/typescript/core/web_core/src/catalog/schema_loader.ts) to `schema_loader.ts`. [#4192](https://github.com/a2ui-project/a2ui/issues/4192) is assigned to `Varun-S10`, who [asked](https://github.com/a2ui-project/a2ui/issues/4192#issuecomment-6846855241) `gspencergoog` to look at it, and the [suggestion](https://github.com/a2ui-project/a2ui/pull/4386#issuecomment-7117700443) on [#4386](https://github.com/a2ui-project/a2ui/pull/4386) also names `gspencergoog`, so the same person decides on all three.

What I checked:

1. The change is minor: one helper in [`schema_loader.ts`](https://github.com/a2ui-project/a2ui/blob/c26be2dd087b541c46e0296c164ae13bf35f7836/typescript/core/web_core/src/catalog/schema_loader.ts#L482-L490) that wraps the array schema in a fatal `z.preprocess` length check, a test that counts item reads with a `Proxy`, and a CHANGELOG entry. The CLA check passes.
2. It overlaps with work in progress. Draft [#4352](https://github.com/a2ui-project/a2ui/pull/4352) deletes `schema_loader.ts` and drops zod from `web_core`, and the team has not decided on that direction yet (see [#2160](https://github.com/a2ui-project/a2ui/issues/2160)). [#4349](https://github.com/a2ui-project/a2ui/pull/4349), [#4285](https://github.com/a2ui-project/a2ui/pull/4285) and [#4057](https://github.com/a2ui-project/a2ui/pull/4057) also edit `schema_loader.ts`, but not the array conversion. #4285 is already assigned to `gspencergoog`.
3. The team can maintain it: the helper is used in one place, and the PR says that zod 4.6.6 and later already stop early, so it does no harm after a zod 4 migration.

The expert needs to decide whether to take this fix for the zod 3 code now, or wait for the direction of [#4352](https://github.com/a2ui-project/a2ui/pull/4352). `web_core` stays on zod 3: the root [`package.json`](https://github.com/a2ui-project/a2ui/blob/c26be2dd087b541c46e0296c164ae13bf35f7836/package.json#L51) pins `zod` to `^3.25.76`.

The PR works on [#4192](https://github.com/a2ui-project/a2ui/issues/4192) but does not close it. #4192 first proposed to depend on zod `^4.6.6` and import from `zod/v3`, and `pierreespitia` [showed](https://github.com/a2ui-project/a2ui/issues/4192#issuecomment-6920304270) that `zod/v3` does not have the advisory's fix, which `samanosr` [confirmed](https://github.com/a2ui-project/a2ui/issues/4192#issuecomment-7104350736). [#4386](https://github.com/a2ui-project/a2ui/pull/4386) bumps `zod` in the `web_core` `package.json` to `^4.6.6` and does not touch the code.""",
  reply="""**From triage:**

Thanks for the fix and for the test that counts item reads. Assigning this PR to `gspencergoog`, who maintains the `web_core` schema loader, to decide on it. Draft [#4352](https://github.com/a2ui-project/a2ui/pull/4352) would remove `schema_loader.ts`, so the decision also depends on the direction the team takes in [#2160](https://github.com/a2ui-project/a2ui/issues/2160).""",
  added_labels=["status: assigned-for-triage", "type: bug", "component: core sdk", "lang: typescript"],
  new_assignees=["gspencergoog"],
  targeted_issues=[4192],
  related_items=[4352, 4349, 4285, 4057, 4386, 2160]
)
</a2ui>

This analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision.

@pierreespitia
pierreespitia force-pushed the fix/web-core-array-max-short-circuit branch from b37f294 to b1fc745 Compare November 25, 2026 12:33
@polina-c

polina-c commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

From triage bot, analysis:

For humans

TL;DR
Case: this external PR is a small security fix with a test in the web_core schema loader, which draft #4352 would remove.
Suggestion: assign gspencergoog and add status: assigned-for-triage, so that they decide on it.

Summary:
pierreespitia makes an array property with maxItems in a catalog loaded by Catalog.fromJson (or in an AgentFunction schema) fail on its length before zod parses its items or runs the uniqueItems check. On zod 3, a failed .max() still parses every item, which is the problem behind GHSA-mrhx-c7xc-m3w5.

Targeted issues:
#4192

Why flagged for triage:
This PR has no assignee.

Suggestion:
Assign gspencergoog and add status: assigned-for-triage, type: bug, component: core sdk and lang: typescript. The decision is the same as in the previous suggestion: since then, the author rebased the PR on main and linked the CHANGELOG entry to the PR, without changing the fix. gspencergoog wrote 7 of the last 15 commits to schema_loader.ts. #4192 is assigned to Varun-S10, who asked gspencergoog to look at it, and the suggestion on #4386 also names gspencergoog, so the same person decides on all three.

What I checked:

  1. The change is minor: one helper in schema_loader.ts that wraps the array schema in a fatal z.preprocess length check, called for arrays with maxItems, a test that counts item reads with a Proxy, and a CHANGELOG entry. The CLA check passes, and the PR has no merge conflicts.
  2. It overlaps with work in progress. Draft #4352 deletes schema_loader.ts and drops zod from web_core, and the team has not decided on that direction yet (see #2160). #4349, #4285 and #4057 also edit schema_loader.ts, but not the array conversion. fix(web_core): enforce JSON Schema Draft 2020-12 keywords in Zod conversion (#3998) #4285 and fix(web_core): enforce minProperties, maxProperties and typeless not in JSON schemas #4057 are already assigned to gspencergoog.
  3. The team can maintain it: the helper is used in one place, and the PR says that zod 4.6.6 and later already stop early, so it does no harm after a zod 4 migration.

The expert needs to decide whether to take this fix for the zod 3 code now, or wait for the direction of #4352. web_core stays on zod 3: the root package.json pins zod to ^3.25.76.

The PR works on #4192 but does not close it. #4192 first proposed to depend on zod ^4.6.6 and import from zod/v3, and pierreespitia showed that zod/v3 does not have the advisory's fix, which samanosr confirmed. #4386 bumps zod in the web_core package.json to ^4.6.6 and does not touch the code.

  1. Add labels: status: assigned-for-triage, type: bug, component: core sdk, lang: typescript
  2. Assign: gspencergoog
  3. Related items: feat(web_core)!: validate against the spec JSON Schema with an app-supplied engine, drop zod #4352, fix(web_core): check v1.0 catalogs read from JSON against the v1.0 common types #4349, fix(web_core): enforce JSON Schema Draft 2020-12 keywords in Zod conversion (#3998) #4285, fix(web_core): enforce minProperties, maxProperties and typeless not in JSON schemas #4057, chore(deps): bump the npm_and_yarn group across 3 directories with 15 updates #4386, [FEATURE]: Abstract dependency on Zod on web_core. #2160

Reply:

From triage:

Thanks for the fix and for the test that counts item reads. Assigning this PR to gspencergoog, who maintains the web_core schema loader, to decide on it. Draft #4352 would remove schema_loader.ts, so the decision also depends on the direction the team takes in #2160.

To adjust the analysis and apply its suggestion, open it in the hosted triage UI, or run in your a2ui checkout:

tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4399
For software
<a2ui>
root = TriageAnalysis(
  tldr="""Case: this external PR is a small security fix with a test in the `web_core` schema loader, which draft [#4352](https://github.com/a2ui-project/a2ui/pull/4352) would remove.
Suggestion: assign `gspencergoog` and add `status: assigned-for-triage`, so that they decide on it.""",
  summary="""`pierreespitia` makes an array property with `maxItems` in a catalog loaded by `Catalog.fromJson` (or in an `AgentFunction` schema) fail on its length before zod parses its items or runs the `uniqueItems` check. On zod 3, a failed `.max()` still parses every item, which is the problem behind [GHSA-mrhx-c7xc-m3w5](https://github.com/advisories/GHSA-mrhx-c7xc-m3w5).""",
  reason="""This PR has no assignee.""",
  suggestion="""Assign `gspencergoog` and add `status: assigned-for-triage`, `type: bug`, `component: core sdk` and `lang: typescript`. The decision is the same as in the [previous suggestion](https://github.com/a2ui-project/a2ui/pull/4399#issuecomment-7134066941): since then, the author rebased the PR on `main` and [linked](https://github.com/a2ui-project/a2ui/pull/4399/commits/b1fc745a71d98dbc6ff43a04df6baa4b813bc9a2) the CHANGELOG entry to the PR, without changing the fix. `gspencergoog` wrote [7 of the last 15 commits](https://github.com/a2ui-project/a2ui/commits/main/typescript/core/web_core/src/catalog/schema_loader.ts) to `schema_loader.ts`. [#4192](https://github.com/a2ui-project/a2ui/issues/4192) is assigned to `Varun-S10`, who [asked](https://github.com/a2ui-project/a2ui/issues/4192#issuecomment-6846855241) `gspencergoog` to look at it, and the [suggestion](https://github.com/a2ui-project/a2ui/pull/4386#issuecomment-7117700443) on [#4386](https://github.com/a2ui-project/a2ui/pull/4386) also names `gspencergoog`, so the same person decides on all three.

What I checked:

1. The change is minor: one helper in [`schema_loader.ts`](https://github.com/a2ui-project/a2ui/blob/b1fc745a71d98dbc6ff43a04df6baa4b813bc9a2/typescript/core/web_core/src/catalog/schema_loader.ts#L208-L222) that wraps the array schema in a fatal `z.preprocess` length check, [called](https://github.com/a2ui-project/a2ui/blob/b1fc745a71d98dbc6ff43a04df6baa4b813bc9a2/typescript/core/web_core/src/catalog/schema_loader.ts#L522-L524) for arrays with `maxItems`, a test that counts item reads with a `Proxy`, and a CHANGELOG entry. The CLA check passes, and the PR has no merge conflicts.
2. It overlaps with work in progress. Draft [#4352](https://github.com/a2ui-project/a2ui/pull/4352) deletes `schema_loader.ts` and drops zod from `web_core`, and the team has not decided on that direction yet (see [#2160](https://github.com/a2ui-project/a2ui/issues/2160)). [#4349](https://github.com/a2ui-project/a2ui/pull/4349), [#4285](https://github.com/a2ui-project/a2ui/pull/4285) and [#4057](https://github.com/a2ui-project/a2ui/pull/4057) also edit `schema_loader.ts`, but not the array conversion. #4285 and #4057 are already assigned to `gspencergoog`.
3. The team can maintain it: the helper is used in one place, and the PR says that zod 4.6.6 and later already stop early, so it does no harm after a zod 4 migration.

The expert needs to decide whether to take this fix for the zod 3 code now, or wait for the direction of [#4352](https://github.com/a2ui-project/a2ui/pull/4352). `web_core` stays on zod 3: the root [`package.json`](https://github.com/a2ui-project/a2ui/blob/a23b19c4f2102695c506adb7d1ded1f5f81a7688/package.json#L51) pins `zod` to `^3.25.76`.

The PR works on [#4192](https://github.com/a2ui-project/a2ui/issues/4192) but does not close it. #4192 first proposed to depend on zod `^4.6.6` and import from `zod/v3`, and `pierreespitia` [showed](https://github.com/a2ui-project/a2ui/issues/4192#issuecomment-6920304270) that `zod/v3` does not have the advisory's fix, which `samanosr` [confirmed](https://github.com/a2ui-project/a2ui/issues/4192#issuecomment-7104350736). [#4386](https://github.com/a2ui-project/a2ui/pull/4386) bumps `zod` in the `web_core` `package.json` to `^4.6.6` and does not touch the code.""",
  reply="""**From triage:**

Thanks for the fix and for the test that counts item reads. Assigning this PR to `gspencergoog`, who maintains the `web_core` schema loader, to decide on it. Draft [#4352](https://github.com/a2ui-project/a2ui/pull/4352) would remove `schema_loader.ts`, so the decision also depends on the direction the team takes in [#2160](https://github.com/a2ui-project/a2ui/issues/2160).""",
  added_labels=["status: assigned-for-triage", "type: bug", "component: core sdk", "lang: typescript"],
  new_assignees=["gspencergoog"],
  targeted_issues=[4192],
  related_items=[4352, 4349, 4285, 4057, 4386, 2160]
)
</a2ui>

This analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision.

@polina-c

Copy link
Copy Markdown
Collaborator

From triage:

Thanks for the fix and for the test that counts item reads. Assigning this PR to gspencergoog, who maintains the web_core schema loader, to decide on it. Draft #4352 would remove schema_loader.ts, so the decision also depends on the direction the team takes in #2160.

@polina-c polina-c added component: core sdk Affects the core SDK layer in any language lang: typescript Typescript agent SDK issues status: assigned-for-triage issue/pr is assigned to a domain expert for further triage type: bug Something isn't working and removed status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs labels Nov 25, 2026

@gspencergoog gspencergoog left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds rejectOversizedArray in typescript/core/web_core/src/catalog/schema_loader.ts so array properties declaring maxItems fail fast via a fatal z.preprocess too_big issue before Zod 3 parses elements or runs uniqueItems refinements. The approach stays compatible with zod-to-json-schema, getDeclaredSchemaKeys, getKnownSchemaKeys, unwrapZodField, and Catalog.toJson(). Two medium and two low refinements remain: finalizePropertyZod wraps not outside rejectOversizedArray, CHANGELOG.md overstates Zod 4 behavior, the synthetic too_big issue hardcodes message and omits exact: false, and the new test uses valid unique items.

🤖 Agent-generated

);
}
if (typeof propSchema.maxItems === 'number') {
arr = rejectOversizedArray(arr, propSchema.maxItems);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applying rejectOversizedArray before finalizePropertyZod places propSchema.not's z.preprocess outside the length guard. When a schema declares both maxItems and not, notSchema.safeParse(val) executes before rejectOversizedArray can abort. Oversized arrays still trigger full item traversal inside notSchema and emit an extra custom issue when not matches, violating the length-check bound and single-issue contract.

--- a/typescript/core/web_core/src/catalog/schema_loader.ts
+++ b/typescript/core/web_core/src/catalog/schema_loader.ts
@@ -160,6 +160,7 @@ function finalizePropertyZod(
   rootDoc?: Record<string, unknown>,
   visitedPointers?: Set<string>,
   defCache?: Map<string, z.ZodTypeAny>,
+  maxItems?: number,
 ): z.ZodTypeAny {
   let result = baseZod;
   if (propSchema.not && typeof propSchema.not === 'object') {
@@ -181,6 +182,9 @@ function finalizePropertyZod(
       return val;
     }, result);
   }
+  if (typeof maxItems === 'number') {
+    result = rejectOversizedArray(result, maxItems);
+  }
   if (propSchema.default !== undefined) {
     result = result.default(propSchema.default);
   }
@@ -519,9 +523,7 @@ function convertPropertyToZod(
         {message: 'Array items must be unique'},
       );
     }
-    if (typeof propSchema.maxItems === 'number') {
-      arr = rejectOversizedArray(arr, propSchema.maxItems);
-    }
-    return finalizePropertyZod(arr, propSchema, rootDoc, visitedPointers, defCache);
+    const maxItems = typeof propSchema.maxItems === 'number' ? propSchema.maxItems : undefined;
+    return finalizePropertyZod(arr, propSchema, rootDoc, visitedPointers, defCache, maxItems);
   }
Details

Impact: Oversized arrays still trigger full item traversal inside notSchema and emit an extra custom issue when not matches, violating the length-check bound and single-issue contract.

Evidence (Deduction): convertPropertyToZod (typescript/core/web_core/src/catalog/schema_loader.ts:522-525) wraps arr in rejectOversizedArray before calling finalizePropertyZod; finalizePropertyZod (typescript/core/web_core/src/catalog/schema_loader.ts:165-182) then wraps result in an outer z.preprocess calling notSchema.safeParse(val), which traverses every item via ZodArray._parse (zod/v3/types.js:1808-1810).

Putting rejectOversizedArray between not and .default() preserves not's precondition because rejectOversizedArray returns val unchanged without filling defaults, keeps ZodDefault outermost so zodField instanceof z.ZodDefault at schema_loader.ts:709 still works, and leaves zod-to-json-schema output unchanged.

🤖 Agent-generated

Comment thread typescript/core/web_core/CHANGELOG.md Outdated
- **BREAKING CHANGE**: Remove the limit of 5 on nested function calls from `validateRecursionAndPaths`, and remove the `MAX_FUNC_CALL_DEPTH` export. Function call nesting is bounded only by the global depth limit (`MAX_GLOBAL_DEPTH`), so payloads that failed with `functionCall depth > 5` are now accepted. Recommended migration: drop references to `MAX_FUNC_CALL_DEPTH` ([#4296](https://github.com/a2ui-project/a2ui/pull/4296)).
- **BREAKING CHANGE**: `MessageProcessor` rejects a payload whose batch arrays and `{"messages": [...]}` wrappers nest deeper than `MAX_GLOBAL_DEPTH` with `A2uiRecursionError`, with or without a `validationConfig`. With a `validationConfig`, each message's depth counts from `0` inside those containers, and keys beside `messages` on a wrapper are checked for depth and `path` syntax. Recommended migration: send messages in one array or wrapper, and remove invalid values beside `messages` ([#4296](https://github.com/a2ui-project/a2ui/pull/4296)).
- Add an optional `startDepth` parameter to `validateRecursionAndPaths`, a non-negative integer that defaults to `0`, for checking a value held by a container whose depth is already counted ([#4296](https://github.com/a2ui-project/a2ui/pull/4296)).
- An array property declared with `maxItems` in JSON Schema (by `Catalog.fromJson` or an `AgentFunction` `schema`) now fails on its length before its items are parsed or checked for `uniqueItems`, so a value over `maxItems` reports a single `too_big` issue and costs no more to validate than its length check, on Zod 3 as on Zod 4 (GHSA-mrhx-c7xc-m3w5, [#4192](https://github.com/a2ui-project/a2ui/issues/4192), [#4399](https://github.com/a2ui-project/a2ui/pull/4399)).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a2ui/web_core pins Zod 3 (^3.25.76) and rejectOversizedArray uses Zod 3 z.preprocess and fatal issue fields. Unpatched Zod 4 releases also iterate every item before running .max(). Readers can mistakenly infer that @a2ui/web_core runs on Zod 4 or that every Zod 4 release already blocks GHSA-mrhx-c7xc-m3w5.

Suggested change
- An array property declared with `maxItems` in JSON Schema (by `Catalog.fromJson` or an `AgentFunction` `schema`) now fails on its length before its items are parsed or checked for `uniqueItems`, so a value over `maxItems` reports a single `too_big` issue and costs no more to validate than its length check, on Zod 3 as on Zod 4 (GHSA-mrhx-c7xc-m3w5, [#4192](https://github.com/a2ui-project/a2ui/issues/4192), [#4399](https://github.com/a2ui-project/a2ui/pull/4399)).
- An array property declared with `maxItems` in JSON Schema (by `Catalog.fromJson` or an `AgentFunction` `schema`) now fails on its length before its items are parsed or checked for `uniqueItems`, so a value over `maxItems` reports a single `too_big` issue and costs no more to validate than its length check under Zod 3 (GHSA-mrhx-c7xc-m3w5, [#4192](https://github.com/a2ui-project/a2ui/issues/4192), [#4399](https://github.com/a2ui-project/a2ui/pull/4399)).
Details

Impact: Readers can mistakenly infer that @a2ui/web_core runs on Zod 4 or that every Zod 4 release already blocks GHSA-mrhx-c7xc-m3w5.

Evidence (Read): "on Zod 3 as on Zod 4" (typescript/core/web_core/CHANGELOG.md:7) vs "zod": "^3.25.76" (typescript/core/web_core/package.json:313) and $ZodArray iterating all elements before checks in for (let i = 0; i < input.length; i++) (node_modules/zod/v4/core/schemas.js:637-649).

🤖 Agent-generated

Comment on lines +211 to +218
ctx.addIssue({
code: 'too_big',
maximum: maxItems,
inclusive: true,
type: 'array',
message: `Array must contain at most ${maxItems} element(s)`,
fatal: true,
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a hardcoded message in ctx.addIssue skips Zod's error-map chain, and omitting exact: false differs from ZodArray.max(). Omitting message lets defaultErrorMap generate the identical English text. Consumers with a custom ZodErrorMap or code inspecting issue.exact see inconsistent too_big issues compared to z.array().max().

Suggested change
ctx.addIssue({
code: 'too_big',
maximum: maxItems,
inclusive: true,
type: 'array',
message: `Array must contain at most ${maxItems} element(s)`,
fatal: true,
});
ctx.addIssue({
code: z.ZodIssueCode.too_big,
maximum: maxItems,
inclusive: true,
exact: false,
type: 'array',
fatal: true,
});
Details

Impact: Consumers with a custom ZodErrorMap or code inspecting issue.exact see inconsistent too_big issues compared to z.array().max().

Evidence (Read): ctx.addIssue({ code: 'too_big', maximum: maxItems, inclusive: true, type: 'array', message: ..., fatal: true }) (typescript/core/web_core/src/catalog/schema_loader.ts:211-218) vs ZodArray._parse (node_modules/zod/v3/types.js:1790-1797) setting exact: false with message: undefined, and makeIssue (node_modules/zod/v3/helpers/parseUtil.js:10-16) returning early when issueData.message !== undefined.

🤖 Agent-generated

// Counts reads of array elements, so the test notices if the items are
// parsed (or passed to the `uniqueItems` check) after `maxItems` fails.
let itemReads = 0;
const tags = new Proxy(['a', 'b', 'c'], {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every element in ['a', 'b', 'c'] already satisfies ^[a-z]+$ and is unique. Even without fast-fail, result.error.issues would still contain only ['too_big', ['tags']]. Using invalid duplicate items tests both zero element reads and suppression of item-level and uniqueItems errors on oversized arrays.

Suggested change
const tags = new Proxy(['a', 'b', 'c'], {
const tags = new Proxy(['A', 'A', 'A'], {
Details

Impact: Using invalid duplicate items tests both zero element reads and suppression of item-level and uniqueItems errors on oversized arrays.

Evidence (Read): const tags = new Proxy(['a', 'b', 'c'], { (typescript/core/web_core/src/catalog/schema_loader.test.ts:616) against items: {type: 'string', pattern: '^[a-z]+$'}, uniqueItems: true (schema_loader.test.ts:603-605) and result.error.issues.map(issue => [issue.code, issue.path]) (schema_loader.test.ts:625-628).

🤖 Agent-generated

`Catalog.fromJson` and `AgentFunction` schemas turn a JSON Schema
`maxItems` into `z.array().max()`. Zod 3 records the failed check but
still parses every item and runs the `uniqueItems` refinement, so the
cost of validating a value is not bounded by `maxItems`
(GHSA-mrhx-c7xc-m3w5). Check the length first in a fatal preprocess
step, which works the same on Zod 3 and Zod 4 and leaves the generated
JSON Schema unchanged.

Refs a2ui-project#4192
- Run the length check outside the `not` check, so an oversized array is
  rejected before `not` parses its items, and keep `.default()` outermost.
- Build the `too_big` issue like `ZodArray.max()` does: no fixed message,
  and `exact: false`.
- Say in the CHANGELOG that the guarantee holds under Zod 3.
- Test with invalid duplicate items, and add a case with both `maxItems`
  and `not`.

Refs a2ui-project#4192
0.17.0 was released while this PR was open, so the entry belongs under
Unreleased.
@pierreespitia
pierreespitia force-pushed the fix/web-core-array-max-short-circuit branch from 5a61d2d to 264272c Compare December 1, 2026 23:59
@pierreespitia

Copy link
Copy Markdown
Author

Thanks for the review. I applied the four suggestions and rebased on current main (the CHANGELOG entry is now under Unreleased, since 0.17.0 was released in the meantime). The web_core suite passes locally (1669 tests).

The Web and Node.js CI and the other workflows are waiting for approval on the latest commit, since this is a first contribution from this account. Could a maintainer approve them so the checks run? Once they are green, it should be ready to merge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: core sdk Affects the core SDK layer in any language lang: typescript Typescript agent SDK issues status: assigned-for-triage issue/pr is assigned to a domain expert for further triage type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants