Bound input size in validate_expression_tool / validate_style_tool to prevent heap OOM#126
Merged
Merged
Conversation
The depth cap from the previous commit only protects against deeply-nested input; a shallow-but-huge array (e.g. a "match" expression with millions of branches) sails past it untouched. Confirmed by reproducing under a constrained heap: unlike the stack-overflow case, exhausting the heap is not a catchable JS error - it aborts the whole process (SIGABRT, "FATAL ERROR: Reached heap limit"), taking down every concurrent MCP session, not just the one request. - Cap the JSON-string form of `expression`/`style` in their Zod schemas, so oversized payloads are rejected before JSON.parse ever runs. For validate_expression_tool this has to be a `.refine()` rather than `.max()` on the union member directly, since the sibling `z.any()` branch would otherwise swallow oversized strings right past the length check. - Add an O(1) array-length/object-key-count guard (checked before iterating or copying) alongside the existing depth guard, so the already-parsed object-input path is covered too, and so a single oversized array/object anywhere in the tree is rejected without visiting its elements. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Valiunia
approved these changes
Jul 15, 2026
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.
Summary
Follow-up to #125, addressing review feedback from @Valiunia about unbounded recursion in the new style-spec-delegating validators.
The depth cap added in #125 only protects against deep adversarial input (a long linear chain). It does not protect against wide-but-shallow input - e.g. a single
matchexpression with millions of branches, at depth ~1 - which sails past the cap untouched and gets handed whole tocreatePropertyExpression/validate().try/catch?RangeErroris a normal JS exceptionReproduced the wide case under a memory-constrained process (
node --max-old-space-size=150) and confirmed it's worse than the stack-overflow case: heap exhaustion is not a catchable JS exception - V8 aborts the process directly (FATAL ERROR: Reached heap limit,SIGABRT), which would take down every concurrent MCP session, not just the one request.Fixed with two layers of defense:
expression(256 KB) andstyle(10 MB) in their Zod schemas, rejected beforeJSON.parseever runs. Note: forvalidate_expression_tool,z.union([z.string().max(N), z.any()])does not work, since the siblingz.any()branch matches everything, including oversized strings, and swallows them right past the length check - had to use a.refine()instead..lengthbefore ever iterating into or copying the collection, alongside the existing depth guard. This covers the already-parsed object-input path too (not just JSON strings), and rejects an oversized array/object anywhere in the tree without visiting its elements.Test plan
npm test- all 596 tests pass, including new regression tests for both the wide-payload and oversized-string cases in both tools (asserting fast rejection, not just non-crash)npm run lint- 0 errorsnpm run build- succeedsnode --max-old-space-size=150+ a 2,000,000-branchmatchexpression →SIGABRT/FATAL ERROR: Reached heap limit) and confirmed post-fix it's rejected in ~1ms with a clear error instead🤖 Generated with Claude Code