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 件の Shoulder 検出ルールに基づく ReDoS の予防策。
Avoid nested quantifiers in regex; use specific character classes instead
- re := regexp.MustCompile("(a+)+b") + re := regexp.MustCompile("^[a-z]+b$")
Avoid nested quantifiers in regex and validate input length before matching
- 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); }
Replace nested quantifiers with simple patterns and bounded repetition
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
コードの脆弱性を見つける
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)
コードレビューで注目すべき点
これらのパターンはInefficient Regular Expression Complexityの潜在的な脆弱性を示しています。コードレビューとセキュリティ監査中に探してください。
コードベースをスキャン: Inefficient Regular Expression Complexity
Shoulder CLI はコードベース全体から脆弱なパターンを見つけます。