T09 · Insecure Skill Coding Practices
- Location
scripts/regex_tester.py:64- Finding
Unbounded Regular Expression Execution Enables Denial of Service
- Content
View full analysis
Vulnerability Details
File Location:
scripts/regex_tester.py, lines 64–72
Vulnerability Type: Regular Expression Denial of Service and uncontrolled resource consumption
Risk Level: MediumVulnerable Code:
python try: compiled = re.compile(pattern, flags) except re.error as e: if as_json: print(json.dumps({"valid": False, "error": str(e)})) else: print(f"Invalid regex: {e}", file=sys.stderr) return 2 matches = list(compiled.finditer(text))Technical Analysis
The application compiles and evaluates a user-controlled regular expression against user-controlled text using Python's backtracking
reengine. No execution timeout, input-size restriction, pattern-complexity restriction, memory limit, or match-count limit is applied.Certain expressions, such as
(a+)+$, can trigger catastrophic backtracking when evaluated against a long near-matching string. CPU consumption may grow exponentially with input length. Additionally, converting the complete iterator into a list causes every match object to be retained simultaneously. Large inputs or patterns producing many matches can therefore cause excessive memory use even without catastrophic backtracking.The regular expression itself does not provide command execution or elevated privileges. The exploitable consequence is resource exhaustion in the process or service invoking this skill.
Attack Path
- An attacker submits a pathological pattern, such as
(a+)+$. - The attacker supplies a sufficiently long near-matching input, such as many
acharacters followed by a nonmatching character. - The agent invokes the documented regex tester with the attacker-controlled pattern and text.
re.compile()accepts the syntactically valid expression.compiled.finditer(text)performs uncontrolled backtracking without a timeout.- The process consumes excessive CPU and may remain unresponsiv ...[truncated 843 chars]
- An attacker submits a pathological pattern, such as
- Remediation
View remediation
Remediation Suggestions
- Run regular-expression evaluation in a dedicated worker process with a strict wall-clock timeout. Terminate the worker if the limit is exceeded.
- Apply operating-system resource limits to the worker, including CPU time and maximum address-space or memory limits.
- Enforce conservative maximum lengths for both the pattern and input text before compilation or matching.
- Reject or separately handle expressions containing risky nested quantifiers, ambiguous alternation, and repeated zero-width constructs. Treat heuristic validation as defense in depth rather than a replacement for timeouts.
- Consider a non-backtracking regular-expression engine where compatibility permits, or an engine that provides an enforceable per-operation timeout.
- Avoid eagerly materializing all matches with
list(). Iterate incrementally and stop after a configured maximum number of matches. - Limit the size of captured values and generated plain-text or JSON output to prevent secondary memory exhaustion.
- Return a distinct timeout or resource-limit error so callers can safely distinguish rejected workloads from invalid patterns and ordinary nonmatches.
