T09 · Insecure Skill Coding Practices
- Location
scripts/intent_classifier.py:33- Finding
Unrestricted Regular Expressions Can Cause Denial of Service
- Content
View full analysis
Vulnerability Details
File Location:
scripts/intent_classifier.py, lines 33-67
Vulnerability Type: Regular Expression Denial of Service (ReDoS)
Risk Level: MediumVulnerable Code
python def add_intent(self, name: str, patterns: List[str], keywords: Optional[List[str]] = None): """ 添加意图 Args: name: 意图名称 patterns: 匹配模式 (支持正则) keywords: 关键词列表 """ self.intents[name] = { 'patterns': patterns, 'keywords': keywords or [] } self.patterns[name] = patterns self.keywords[name] = keywords or [] def classify(self, text: str) -> Dict: """ 分类文本意图 Returns: {'intent': str, 'confidence': float, 'entities': dict} """ text = text.lower() scores = {} for intent_name, config in self.intents.items(): score = 0.0 entities = {} # 模式匹配 for pattern in config['patterns']: if re.search(pattern, text, re.IGNORECASE): score += 0.5 # 提取实体 (简化版) matches = re.findall(pattern, text, re.IGNORECASE) if matches: entities['match'] = matches[0]Technical Analysis
The classifier accepts unrestricted regular-expression patterns through
add_intent()and also supports loading these patterns from JSON configuration. It evaluates each configured expression against message text using Python's backtrackingreengine.A malicious or accidentally pathological expression, such as one containing nested ambiguous quantifiers, can require exponential processing time for a carefully constructed nonmatching input. The implementation compounds this risk by evaluating a successful pattern twice, first with
re.search()and then withre.findall().The exploitability depends on whether an attacker can create or influence intent configurations. Even when configuration access is restricted to administrators, a malform ...[truncated 1173 chars]
- Remediation
View remediation
Remediation Suggestions
- Restrict intent-pattern creation and configuration loading to trusted administrators.
- Validate expressions before accepting them and reject nested, ambiguous quantifiers and other known catastrophic-backtracking constructs.
- Enforce conservative maximum lengths for both patterns and classified messages.
- Use a regular-expression implementation that supports strict execution timeouts, or run matching in a separately constrained process.
- Prefer literal, token-based, or finite-state matching when full regular-expression capabilities are unnecessary.
- Avoid evaluating the same expression twice. Retain the match object returned by a single operation and extract the required match from it.
- Add automated tests containing pathological expressions and long adversarial inputs.
- Apply request timeouts, CPU limits, concurrency limits, and rate limiting as defense-in-depth controls.
