Skip to content

server: a grammar over 512 KiB is refused before it is compiled - #3038

Merged
ashhart merged 4 commits into
mainfrom
security/grammar-size-cap
Nov 17, 2026
Merged

ashhart merged 4 commits into
mainfrom
security/grammar-size-cap

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Before: a grammar of any size up to the 32 MiB body limit went to xgrammar, whose compile grows with the square of the text while the server holds one lock: a 2 MB enum schema took 61 seconds on eight cores. After: a grammar over 512 KiB gets HTTP 400 before the compiler sees it.

  • Issue: n/a. SECURITY.md asks for private reports, so there is no public issue.
  • Stacked on: main.
  • atc overlap --json: not run.

Checks on this head

  • Head commit: 3c0a0f6a52caca491336c83b49d77cf5f3fcb707. Three commits: the change (1e8cd3f8e90f), the review fixes (de792da9f348, described in the comments below), and a merge of main at 223da7659b2f. The merge keeps both sides of tool_required_test.zig, this PR's test and main's helper from grammar budget: exploring a grammar stops at a work limit and a time limit #3037, and folds the size check into the statement after it so server.zig stays at 600 lines. The check's order and message are unchanged.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 341 on main, 341 on this head, 0 new.
  • Tests run on the merged tree, with the command and pass, fail and skip counts:
    • zig fmt --check and git diff --check: pass
    • zig build test-grammar-size-host test-tool-required-host: 6/6 steps succeeded; 51/51 tests passed
    • zig build test-server-security test-pull-security: 72/72 steps succeeded; 47/47 tests passed
    • zig build test-server-cpu: 5/5 steps succeeded; 1339/1346 tests passed (7 skipped)
    • zig build check-native-cuda-host compile-flashnext-mtp-parity-host compile-glm-native-open: 11/11 steps succeeded
    • zig build -Dxgrammar=<xgrammar v0.2.8> test-grammar-budget test-grammar-tools: 8/8 steps succeeded; 52/52 tests passed
    • zig build test-golden: 5/5 steps succeeded
    • these ran on the merge of main at 2cd0ec7fa50d. The head merges 223da7659b2f, two commits later, which touch no file of this PR, and on the head test-grammar-size-host test-tool-required-host test-server-security pass again
    • the security step failed once in five runs, while other steps were compiling beside it, and passed the four after. It passes on main the same way
  • New tests:
    • security: a grammar over the size limit is refused before the compiler sees it, and one at the limit compiles
    • security: tools count once against the grammar size limit: 300 KiB of required tools is served, 600 KiB refused, from the review fixes
  • CI: runs on this push.
  • Not run, and why: Not run inside a running server. zig build test and every step that runs on a GPU were not run: this Mac's GPU was serving a model, and no CUDA, ROCm or Intel hardware was available.

Receipt

n/a. This change cannot reach inference: it refuses a request before any grammar is compiled. A request that passes takes the same path as on main.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. Main with only this branch's tests added, zig build test-server-cpu: exit 1. It does not compile: the new test reads grammar.max_text, which the fix adds, so this base shows no failure on behavior. On the earlier base b38a4dd02 the tests failed with the fix taken out.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

Notes for the reviewer

  • 512 KiB is a constant. A compile at the cap still holds the lock for about 4 seconds.
  • Tools are measured by the grammar for one call, which holds each tool's name and schema once, and a tool list over the limit gets its own 400. That came with the review fixes.

Suggested changelog line

Server: a grammar over 512 KiB is refused with HTTP 400 before it is compiled.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review: almost ready; three small fixes first.

  1. Tool schemas are counted twice. The limit measures text in compiledStructure. With tool_choice: "required" and parallel calls, every tool's schema appears twice in the structural tag (tool_grammar.zig near :113). So about 256 KiB of tool schemas, which main serves, gets a 400. Limit the tools' own size, or the text before it is doubled.
  2. The refusal message confuses tool clients. It says "the grammar is N bytes" to a client that sent no grammar. Give tools their own message.
  3. The docs say "as sent". A schema is measured after it is re-serialized, so correct that wording.

The check itself is right: one length compare per request, a clean 400 before any token, and nothing on the token path.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review fixes pushed (de792da):

  • Tools count once. The limit measures the grammar built for one call, which holds each tool's name and schema once, before the parallel form repeats it. 300 KiB of tools with tool_choice: "required" and parallel calls is served. A long tool name or a very large tool list still counts, so nothing reaches the compiler uncapped.
  • Tool requests over the limit get their own 400, naming the tools' size and the limit.
  • The docs now say what's measured: a schema after it is re-serialized, and choices as the re-serialized JSON list.

Results:

  • test-grammar-size-host, test-tool-required-host, test-server-security and test-server-cpu: 1410/1417 tests (7 skipped). security_check.py: 24/24.
  • test-grammar-tools against the pinned grammar library: 46/46.

Lean: 345 before and after.

  • Synthena (Ash's Agent)

@ashhart
ashhart marked this pull request as ready for review November 12, 2026 23:22
@ashhart
ashhart force-pushed the security/grammar-size-cap branch from 3c0a0f6 to d3eea8d Compare November 14, 2026 07:28
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
…inted commit

Eight are for PRs that haven't landed (#3457, #3560, #2979, #3588, #2824, #3233, #2000, #3038); they return when those PRs land. The ninth repeated #2878's line, which is already in its release.
xgrammar's compile grows with the square of the grammar's text and the server holds one lock across it.
A grammar over 512 KiB now gets HTTP 400 before the compiler sees it.
…al of their own

A sent grammar is checked where it is read, before compiling. A tool grammar is measured as the grammar for one call,
which holds each tool's name and schema once, so the repeat a parallel-call grammar adds no longer refuses tools under
the limit (300 KiB of required tools is served); names and many small tools still count. Its 400 names tools. The docs
say a JSON schema or a choice list is measured as the server re-serializes it.
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