Skip to content

mcp: reject deeply nested tool arguments before validation - #1179

Open
ez-lbz wants to merge 1 commit into
modelcontextprotocol:mainfrom
ez-lbz:fix/mcp-limit-json-nesting-depth
Open

mcp: reject deeply nested tool arguments before validation#1179
ez-lbz wants to merge 1 commit into
modelcontextprotocol:mainfrom
ez-lbz:fix/mcp-limit-json-nesting-depth

Conversation

@ez-lbz

@ez-lbz ez-lbz commented Aug 16, 2026

Copy link
Copy Markdown

Summary

Tool input is unmarshaled with a JSON decoder that imposes no nesting limit of its own, and JSON Schema validation can become quadratic in the nesting depth of the input. A small adversarial payload (a few tens of kilobytes) can therefore burn minutes of CPU during validation of tools/call arguments.

This change scans the raw input for nesting depth before unmarshaling or validating, and rejects input deeper than 1000 levels. The scan is linear in the length of the input and ignores the contents of string literals.

Tests

  • Added TestApplySchemaRejectsDeeplyNestedInput in mcp/tool_test.go, covering:
    • rejection at depths above the limit (1000, 2000, 20000),
    • acceptance at or below the limit,
    • that braces inside string literals do not count toward nesting depth.

Tool input is unmarshaled with a JSON decoder that imposes no nesting
limit of its own, and schema validation can become quadratic in the
nesting depth of the input. A small adversarial payload of a few tens of
kilobytes can therefore burn minutes of CPU during validation.

Scan the raw input for nesting depth before unmarshaling or validating,
and reject input deeper than 1000 levels with an error. The scan is
linear in the length of the input and ignores string literal contents.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant