测试版 Shoulder 目前处于测试阶段 — 结果有时可能不正确。您的反馈塑造我们接下来要修复的内容。 分享反馈
🔄

Inefficient Regular Expression Complexity

🛡️ 3 条规则检测到此问题

Inefficient Regular Expression Complexity

The product uses a regular expression with an inefficient, possibly exponential worst-case computational complexity that consumes excessive CPU cycles.

Certain regular expression patterns can take exponential time to evaluate on certain inputs (ReDoS). Attackers can craft inputs that cause the regex engine to consume excessive CPU time, leading to denial of service.

普遍性
覆盖 3 种语言
影响
1 条严重级别为高的规则
预防
已记录
3 个修复示例
2 预防
2 预防

如何修复此漏洞

基于 3 条 Shoulder 检测规则的 ReDoS 预防策略。

Regular Expression Denial of Service MEDIUM

Avoid nested quantifiers in regex; use specific character classes instead

+1 -1 go
- re := regexp.MustCompile("(a+)+b")
+ re := regexp.MustCompile("^[a-z]+b$")
  
Regular Expression Denial of Service (ReDoS) HIGH

Avoid nested quantifiers in regex and validate input length before matching

+6 -2 javascript
- const emailRegex = /^([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}$/;
- if (emailRegex.test(req.body.email)) {
+ const validator = require('validator');
+ 
+ if (req.body.email.length > 254) {
+   return res.status(400).json({ error: 'Input too long' });
+ }
+ if (validator.isEmail(req.body.email)) {
    processEmail(req.body.email);
  }
  
Regular Expression Denial of Service (ReDoS) MEDIUM

Replace nested quantifiers with simple patterns and bounded repetition

+1 -1 python
  import re
  
- email_pattern = re.compile(r'^([a-zA-Z0-9._-]+)+@[a-zA-Z0-9.-]+$')
+ email_pattern = re.compile(r'^[a-zA-Z0-9._-]{1,64}@[a-zA-Z0-9.-]{1,255}$')
  
  def validate_email(email):
      return email_pattern.match(email)
  

关键实践

  • Use exponential time complexity when matching certain inputs
3 检测
3 检测

查找代码中的漏洞

使用Shoulder扫描代码中的Inefficient Regular Expression Complexity模式。 3 规则.

终端
# Scan with Shoulder CLI
npx @shoulderdev/cli trust --cwe=1333

# Or scan entire project
npx @shoulderdev/cli trust .

检测规则 (3)

4 警告信号
4 警告信号

代码审查中需要关注的内容

这些模式表明潜在的Inefficient Regular Expression Complexity漏洞。在代码审查和安全审计中注意查找。

🟠
potentially catastrophic regular expressions that could lead to ReDoS attacks javascript-regex-dos
🟡
regular expressions with catastrophic backtracking patterns that can cause exponential time complexi python-redos
🔍

扫描你的代码库: Inefficient Regular Expression Complexity

Shoulder CLI 在整个代码库中找到易受攻击的模式。