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는 전체 코드베이스에서 취약한 패턴을 찾아냅니다.