# Inefficient Regular Expression Complexity (CWE-1333) The product uses a regular expression with an inefficient, possibly exponential worst-case computational complexity that consumes excessive CPU cycles. **Stack:** Python - Prevalence: Moyenne 3 langages couverts - Impact: Élevé 1 règles de sévérité élevée - Prevention: Documentée 3 exemples de correctifs **OWASP:** Injection (A03:2021-Injection) - #3 ## Description 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. ## Prevention Stratégies de prévention pour ReDoS basées sur 1 règles de détection Shoulder. ### Key Practices - Use exponential time complexity when matching certain inputs ### Python Replace nested quantifiers with simple patterns and bounded repetition ## Warning Signs - [MEDIUM] regular expressions with catastrophic backtracking patterns that can cause exponential time complexi ## Consequences - DoS ## Mitigations - Évitez les quantificateurs imbriqués et les alternances chevauchantes dans les regex - Utilisez des mécanismes de timeout pour les regex - Envisagez d'utiliser des moteurs de regex sans retour arrière (non-backtracking) ## Detection - Total rules: 3 - Languages: go, javascript, typescript, python ## Rules by Language ### Python (1 rules) - **Regular Expression Denial of Service (ReDoS)** [MEDIUM]: Detects regular expressions with catastrophic backtracking patterns that can cause exponential time complexity when matching certain inputs. Attackers can exploit this to cause denial of service. Use simpler patterns or set timeouts. - Remediation: Avoid nested quantifiers like (a+)+. Use simple patterns with bounded quantifiers. ```python import re # Safe: simple character class with bounded quantifiers pattern = re.compile(r'^[a-zA-Z0-9_]{3,20}$') if not pattern.match(username): raise ValueError('Invalid username') ``` Learn more: https://shoulder.dev/learn/python/cwe-1333/redos