perf: fix O(n²) catastrophic backtracking in redact regex (salvage #4779)#4962
Merged
perf: fix O(n²) catastrophic backtracking in redact regex (salvage #4779)#4962
Conversation
…ile read guard
Two pre-existing issues causing test_file_read_guards timeouts on CI:
1. agent/redact.py: _ENV_ASSIGN_RE used unbounded [A-Z_]* with
IGNORECASE, matching any letter/underscore to end-of-string at
each position → O(n²) backtracking on 100K+ char inputs.
Bounded to {0,50} since env var names are never that long.
2. tools/file_tools.py: redact_sensitive_text() ran BEFORE the
character-count guard, so oversized content (that would be rejected
anyway) went through the expensive regex first. Reordered to check
size limit before redaction.
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.
Salvage of PR #4779 by @acsezen. Cherry-picked with authorship preserved.
Fixes
Regex bound (
agent/redact.py):[A-Z_]*→[A-Z_]{0,50}in_ENV_ASSIGN_RE. Eliminates O(n²) catastrophic backtracking on large uniform strings. Benefits all callers (file_tools, terminal_tool, browser_tool, code_execution_tool).Guard reorder (
tools/file_tools.py): Character-count guard now runs beforeredact_sensitive_text(). Oversized content rejected immediately without touching the regex.Results
Closes #4779