T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/grep.py:36
- Finding
- Regular Expression Denial of Service Through Untrusted Search Patterns## Vulnerability Details **File Location**: `scripts/grep.py`, lines 36-47 **Vulnerability Type**: Regular Expression Denial of Service (ReDoS) **Risk Level**: Medium **Vulnerable Code**: ```python # Compile regex flags = re.IGNORECASE if ignore_case else 0 if word_regexp: pattern = r'\b' + pattern + r'\b' try: regex = re.compile(pattern, flags) except re.error as e: return [f"Invalid pattern: {e}"] matches = [] for i, line in enumerate(lines): line_match = regex.search(line) ``` ### Technical Analysis The command-line search pattern is passed directly to Python's backtracking `re` engine. Although malformed expressions are caught, syntactically valid expressions with nested or ambiguous quantifiers can require exponential matching time. For example, a pattern such as `(a+)+$` can cause catastrophic backtracking when evaluated against a sufficiently long line consisting of many `a` characters followed by a nonmatching character. The implementation imposes no pattern-complexity limit, input-line length limit, execution timeout, or cancellation mechanism. Consequently, a user who controls both the pattern and searched content—or who can direct the utility to adversarial content—can consume excessive CPU resources. ### Attack Path 1. The attacker creates or identifies a readable text file containing a long near-matching line, such as thousands of `a` characters followed by `X`. 2. The attacker invokes or induces the Agent to invoke the Skill using a pathological expression such as `(a+)+$`. 3. `re.compile()` accepts the expression because it is syntactically valid. 4. `regex.search(line)` enters extensive backtracking while processing the crafted line. 5. The Skill process consumes CPU until matching completes, the task times out, or an external resource limit terminates it. ### Impact Assessment Successful exploitation can delay or halt the Skill invocation, cause Agent task tim ...[truncated 270 chars]
- Remediation
- ## Remediation Suggestions - Replace Python's backtracking regular-expression implementation with a linear-time engine where supported. - If the existing engine must be retained, execute each match in an isolated worker with a strict wall-clock timeout and terminate workers that exceed it. - Enforce reasonable limits on pattern length and input-line length. - Reject or restrict high-risk regex constructs, while recognizing that static filtering alone is not a complete defense. - Apply process-level CPU and memory limits when the Skill handles untrusted patterns or files. - Return a clear timeout error rather than allowing an unbounded search to block the Agent.
