security: reject nested-quantifier regex in Split/Replace to prevent ReDoS (CWE-1333)#2060
Open
Allen930311 wants to merge 1 commit into
Open
security: reject nested-quantifier regex in Split/Replace to prevent ReDoS (CWE-1333)#2060Allen930311 wants to merge 1 commit into
Allen930311 wants to merge 1 commit into
Conversation
… CWE-1333) The Split pre-tokenizer and Replace normalizer both accept user-supplied regex patterns that are compiled and executed by Oniguruma, a backtracking regex engine. An attacker can craft a pattern with nested quantifiers (e.g. `(a+)+`) that causes catastrophic backtracking — O(2^n) — when tokenising carefully chosen input, resulting in a denial-of-service (CWE-1333 / CWE-400). Fix: - Add `utils::check_redos_risk()` that parses the pattern with `regex-syntax` (already a dependency) and returns an error when nested quantifiers are detected. Patterns using Oniguruma-specific syntax that `regex-syntax` cannot parse are passed through unchanged (best-effort check). - Call `check_redos_risk()` in `Split::new()` and `Replace::new()` for the `Regex` variant, before the Oniguruma compilation step. No new dependencies are introduced; `regex-syntax` is already in Cargo.toml. Affected call sites: tokenizers/src/pre_tokenizers/split.rs — SplitPattern::Regex tokenizers/src/normalizers/replace.rs — ReplacePattern::Regex Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
The
Splitpre-tokenizer andReplacenormalizer both accept arbitrary regex patterns supplied by users via JSON configuration. These patterns are compiled and executed by Oniguruma, a backtracking regex engine.An attacker can craft a tokenizer JSON that contains a pattern with nested quantifiers (e.g.
(a+)+,(a*)*,([a-z]+)+). When such a tokenizer is loaded by a service and then used to tokenize carefully chosen input, Oniguruma performs exponential backtracking — O(2^n) — hanging the process for seconds (or indefinitely).CWE: CWE-1333 (Inefficient Regular Expression Complexity) / CWE-400 (Uncontrolled Resource Consumption)
Proof of Concept
Expected output (exponential growth):
The same PoC works for the
Replacenormalizer with aRegexpattern.Changes
tokenizers/src/utils/mod.rscheck_redos_risk(pattern)usingregex-syntaxAST analysistokenizers/src/pre_tokenizers/split.rsSplit::new()forSplitPattern::Regextokenizers/src/normalizers/replace.rsReplace::new()forReplacePattern::Regexregex-syntaxis already a declared dependency — no new crates required.Patterns using Oniguruma-specific syntax that
regex-syntaxcannot parse are silently accepted (best-effort). Hardcoded library patterns (ByteLevel, etc.) are not affected.Test plan
Split::new(SplitPattern::Regex("(a+)+".into()), ...)→ returnsErr(rejected)Replace::new(ReplacePattern::Regex("(a*)*".into()), ...)→ returnsErr(rejected)\s+,\w+,[a-z]+→ accepted unchanged\p{L}+→ accepted unchanged (best-effort)