fix(regex): bound regex matching to prevent ReDoS - #2271
Open
feiiiiii5 wants to merge 1 commit into
Open
Conversation
Configured regex patterns were compiled with the stdlib backtracking engine and evaluated without a time limit against user input, model output, and retrieval text, so a pathological configured pattern plus crafted text could pin a worker indefinitely. Compile patterns with the timeout-capable third-party regex module (an existing transitive dependency, now declared directly) and bound each match to 0.5s. If a pattern times out, the text is treated as matching (fail-closed) with a warning, so forbidden content cannot slip through because the matcher was too slow. Invalid-pattern errors and case-insensitive behavior are preserved. Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
Author
|
@Pouyanpi gentle ping for review when you have a chance. This bounds regex matching to fail closed on catastrophic-backtracking timeouts, with regression tests; CI is green. Happy to adjust anything. |
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.
Root Cause
RegexDetectionOptions.compile_patternspre-compiles configured patterns with the stdlib backtrackingreengine, anddetect_regex_patternevaluates them withcompiled.search(text)— no time limit, no input bound. A pathological configured pattern (e.g. nested quantifiers) combined with crafted input, model output, or retrieval text can cause catastrophic backtracking and pin a worker indefinitely (ReDoS).Fix
regexmodule (already an existing transitive dependency; now declared directly inpyproject.toml).REGEX_MATCH_TIMEOUT_SECONDS = 0.5(nemoguardrails/library/regex/actions.py).case_insensitivebehavior and invalid-pattern error reporting at config load (regex.errorandre.errorare both caught and surfaced as the existingValueError).Test
test_regex_action_bounds_catastrophic_backtracking: the pathological pattern(a|aa)+$against"a"*40 + "b"returns a blocked outcome within 2s instead of hanging (previously effectively unbounded underre).test_regex_action_valid_pattern_still_matches_with_timeout: ordinary case-insensitive patterns still match within the budget.tests/test_regex_detection.py: 18 passed (including invalid-pattern-at-config-load, case-insensitive, multiple-pattern, input/output/retrieval flows).ruff==0.14.6check + format clean on changed files.Diff scope
4 files, +87/-6:
nemoguardrails/library/regex/rail_config.py,nemoguardrails/library/regex/actions.py,tests/test_regex_detection.py,pyproject.toml.AI Disclosure
AI-assisted implementation and tests; human review of the fail-closed semantics, the
regex-module compatibility (superset ofre, error-type handling), timeout budget, and final diff before submission.Closes #2203