Skip to content

A request regex is bounded twice: the pattern length at the door, the NFA builder state budget behind it - #181

Open
AgentSparko wants to merge 2 commits into
TandemLLM:mainfrom
AgentSparko:fix/regex-dos
Open

AgentSparko wants to merge 2 commits into
TandemLLM:mainfrom
AgentSparko:fix/regex-dos

Conversation

@AgentSparko

Copy link
Copy Markdown
Contributor

Two walls on the request regex, from the engine audit: the pattern's length at the door, the NFA builder's state budget behind it. A test that failed before the fix and passes after is included.

Critical: a request regex could ask for a billion automaton states

structured_outputs: {"regex": ...} was accepted from any request and compiled on the
handler's thread. The builder caps each repetition at 1,000 copies, but nested
repetitions compose: ((\d{1000}){1000}){1000} asks for ~10^9 NFA states, and the NFA
is built whole before the DFA's own 20,000-state cap is ever asked. On this class of
box the OOM freezes the machine — one request, no auth beyond what the port allows.

Two walls now:

  • server/compat.py — a request regex longer than ACT_LIMITS["regex_chars"] (8,192
    characters, the act path's own limit, so the door is consistent with SET_CONSTRAINT)
    is a 400 that names structured_outputs.
  • engine/grammar.py — NFA_STATE_BUDGET = 250_000: the NFA builder raises
    GrammarError once it has created that many states, so composition cannot explode no
    matter which door the pattern came in (regex, choice list, json_schema, tools).

The budget number, honestly. Measured NFA sizes of legitimate grammars (this box,
engine venv): json_object (depth 3) 6,955 states; a realistic json_schema ~9.9k;
the tool-call language for the bench's 4 tools 3,326; for 20 agent-style tools 29,842
(DFA 14,951 — the pre-existing 20,000-state DFA cap, not the budget, is what a very
large tool set hits first). 250,000 is ~8x the largest grammar measured and ~1.7x what
a ~100-tool request would build; it bounds one build to tens of MB and about a second,
while every grammar real traffic sends compiles untouched. The test asserts both sides:
the refusal names the budget and arrives with the automaton still at or under it, and
json_object / json_schema / the twenty-tool call language still compile.

Tests

  • tests/test_grammar_dos.py — nested repetitions die at the state budget (before:
    (\d{1000}){300} built its 300k states and only then hit the DFA cap; the audit's
    10^9 pattern built until the box died); an over-long request regex is a 400 (before:
    it passed as a pattern); legitimate grammars still compile.

Regression run (CPU, engine venv)

tests/test_grammar.py 26 passed · tests/test_set_constraint.py 12 passed ·
tests/test_toolformats.py 6 passed.

Notes for the reviewer

  • The budget is per compile_regex call, so the SRV-619 parts path (calls × document
    compiled apart) gets the budget per part, as before per whole.
  • choice and json_schema patterns are not length-capped at the door (they are
    bounded by the body limit); the state budget is what holds them, and it is the wall
    that matters — length alone never bounded the composed repetition counts.
  • The ACT_LIMITS comment still names only the DFA's 20,000-state bound; the build
    budget is documented at NFA_STATE_BUDGET and in the structured_pattern comment.

@github-project-automation github-project-automation Bot moved this to Triage in TandemLLM Nov 21, 2026
@github-actions github-actions Bot added engine/core Engine: model code, loaders, the decode and verify loop server/api Server: the OpenAI API, tool calls, streaming, structured output labels Nov 21, 2026

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

engine/core Engine: model code, loaders, the decode and verify loop server/api Server: the OpenAI API, tool calls, streaming, structured output

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant