Skip to content

tokenizer: a split pattern is held to a work allowance, and one too deep or too large is refused - #3071

Merged
ashhart merged 2 commits into
mainfrom
security/tokenizer-regex-budget
Nov 13, 2026
Merged

ashhart merged 2 commits into
mainfrom
security/tokenizer-regex-budget

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

What this changes

Before: a tokenizer.json Split pattern ran on a backtracking matcher with no limit. (?:a+)+b never finished on a run of 64 letters, 100,000 nested groups ran the pattern parser off its stack, and the 26-byte pattern (?:(?:a{1000}){1000}){1000} compiled to 29 GB. After: a search runs at most 2^20 instructions plus 2,048 for each byte of its text and then ends with RegexBudget, so the request fails by name. A pattern is refused at load past 65,536 instructions, 64 nested groups or 8 quantifiers on one element.

  • 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: 1a26bd6149c156b097b6d53c7c68755cfe90af97
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 345 on the base, 345 on this head, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig fmt --check and git diff --check: pass
    • zig test --dep sys -Mroot=zig/src/core/tokenizer/tokenizer.zig -Msys=zig/src/platform/sys.zig -lc (the tokenizer module has no host step of its own): all 58 tests passed
    • zig build test-server-cpu: 5/5 steps succeeded; 1325/1332 tests passed (7 skipped)
    • zig build test-golden test-server-security test-pull-security: 76/76 steps succeeded
    • zig build check-native-cuda-host compile-flashnext-mtp-parity-host compile-glm-native-open: 11/11 steps succeeded
    • zig build native: 66/66 steps succeeded
  • New tests:
    • security: a pattern that backtracks without end stops at its text's allowance
    • security: a pattern nested, repeated or quantified past the compiler's limits is refused, not compiled
    • the split patterns checkpoints ship use a small part of their allowance on long runs
  • CI: runs on this push.
  • Not run, and why: Prompt speed was not measured: the matcher now counts each instruction. 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

  • Main against branch: could not be run. It needs a served checkpoint.
  • Boxes it reaches: prompt tokenization on every backend, for a tokenizer with a regex Split or ByteLevel step.
  • New switch? Default, accepted values, profile row: none.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: this head on Zig 0.17.0, on an Apple silicon Mac, CPU-only steps. No GPU and no checkpoint was used.
  • Paths and platforms the change reaches, which I ran and which I could not: the tokenizer module's tests and the server's CPU suite on macOS.
  • Exactness: not measured. A text inside its allowance is matched instruction for instruction as on main, and the existing pattern tests pass unchanged.
  • Main against this branch, same machine and checkpoint: not run, for the reason above.
  • Decode speed before and after: not measured.
  • Prompt speed before and after: not measured.
  • Licence of any new weights or drafter: n/a.

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. On main the same three inputs were run against the unchanged matcher: the search was still running when it was killed after 30 seconds, the nested pattern ended in a segmentation fault, and the repeated pattern compiled with 29 GB resident.
  • 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.

For a change that reaches inference:

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not measured: no served run.
  • No precision traded for speed.
  • Decode and prompt processing are no slower than main. Not measured: no served run.

Notes for the reviewer

  • The patterns checkpoints ship (Qwen, LFM2, Ling, GPT-2 ByteLevel) use at most 33.5 instructions a byte on long runs of spaces, letters, digits, punctuation and mixed text, about a sixtieth of the allowance. A test holds them under a sixteenth.
  • A pattern that backtracks heavily on some legitimate text would now fail that request where it used to finish late. The two allowance numbers are constants in regex.zig.
  • The tests moved to regex_test.zig, which keeps regex.zig within 600 lines. hf_text.zig is at 600, so its message chain became a switch of the same length.

Suggested changelog line

Tokenizer: a split pattern that backtracks without end stops at a work allowance for its text, and one nested or repeated past the compiler's limits is refused at load.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review: ready. We checked the split patterns of the tokenizers we serve: Qwen3.5/3.8, Flash Next, LFM3/LFM2.5, Bolt, MiniMax, Unlimited-OCR, plus GPT-2 ByteLevel. The deepest group is 1 level (limit 64), at most 2 quantifiers in an element (limit 8), and 2–123 instructions (limit 65,536).

We also ran this PR's matcher on 64 KiB adversarial runs, 3,000 random mixes and real texts up to 1 MB. The worst case was 33.5 instructions a byte against the 2,048 allowance, about 61× headroom, and every split was identical to main's. The allowance resets per piece and counts instructions, not time, so it's deterministic. The regex pass is level with main within noise.

One follow-up, not blocking: the new regex_test.zig cases may not run in CI today. A tokenizer host step in the fast lists is on its way separately.

  • Synthena (Ash's Agent)

…eep or too large is refused

A tokenizer.json's Split pattern runs on a backtracking matcher with no limit. `(?:a+)+b` never finished
on a run of 64 letters, 100,000 nested groups ran the parser off its stack, and
`(?:(?:a{1000}){1000}){1000}` compiled to 29 GB. A search now runs at most 2^20 instructions plus 2,048
for each byte of its text and then ends with RegexBudget, about 60 times what the shipped patterns need
on their worst runs. A pattern is refused past 65,536 instructions, 64 nested groups or 8 quantifiers on
one element. The tests move to regex_test.zig to keep regex.zig within 600 lines.
@ashhart
ashhart force-pushed the security/tokenizer-regex-budget branch from 1a26bd6 to ccb33a4 Compare November 13, 2026 02:04
@ashhart
ashhart merged commit 2cd0ec7 into main Nov 13, 2026
4 checks passed
@ashhart
ashhart deleted the security/tokenizer-regex-budget branch November 13, 2026 02:04
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