ReDoS Linter
A Python linter that detects Regular Expression Denial of Service (ReDoS) vulnerabilities in your code. ReDoS attacks occur when malicious input causes exponential backtracking in regular expressions, leading to denial of service.
Features
- Scans Python files for string literals that look like regular expressions
- Detects vulnerable regex patterns using the recheck engine
- Provides detailed attack vectors when vulnerabilities are found
- Supports both file and directory scanning
- Clean, colored output for better readability
- Support for ignore comments to exclude specific regexes from analysis
Installation
pip install redos-linter
Usage
Command Line
Check specific files or directories:
# Check a single file
redos-linter myfile.py
# Check multiple files
redos-linter file1.py file2.py
# Check a directory (recursively scans all .py files)
redos-linter src/
# Check multiple directories
redos-linter src/ tests/
Python Module
You can also run it as a Python module:
python -m redos_linter src/
Exit status
Both redos-linter and python -m redos_linter return:
0when every checked regex is safe, or no regexes are found.1when vulnerabilities are found, with locations, patterns, and source context in the output.2when the checker fails, returns an incomplete or invalid response, or cannot classify every regex.
Invalid arguments also return 2. File access and Python parsing errors terminate with a nonzero status.
Starting with version 0.3.2, findings fail the command; CI users who want an advisory check should configure their job to allow failure.
Output
The linter provides clear output indicating:
- ✅ Safe: No ReDoS vulnerabilities detected
- ❌ Vulnerable: ReDoS vulnerability found with attack vector details
When vulnerabilities are detected, the output includes:
- The vulnerable regular expression
- File location (line and column)
- Source code context
- Attack string that can trigger the ReDoS
- Pump strings for the attack
Examples of Vulnerable Patterns
import re
# Exponential backtracking due to nested quantifiers
vulnerable_1 = re.compile(r"^(a+)+$")
# Exponential backtracking due to overlapping quantifiers
vulnerable_2 = re.compile(r"(a|aa)+")
# Complex nested pattern
vulnerable_3 = re.compile(r"([a-z]+)+$")
# Real-world example
vulnerable_4 = re.compile(r"^(name|email|phone),([a-zA-Z0-9_]+,)*([a-zA-Z0-9_]+)$")
Examples of Safe Patterns
import re
# Simple safe regex
safe_1 = re.compile(r"^[a-zA-Z0-9_]+$")
# Email pattern (properly structured)
safe_2 = re.compile(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
# Non-overlapping alternation
safe_3 = re.compile(r"^(cat|dog)$")
Ignoring Specific Regexes
You can exclude specific regexes from analysis by adding # redos-linter: ignore on the same line:
import re
# This vulnerable regex will be ignored
vulnerable = re.compile(r"(a+)+") # redos-linter: ignore
# This vulnerable regex will be detected
also_vulnerable = re.compile(r"([a-z]+)+$")
This is useful when:
- You've reviewed a regex and determined it's safe despite being flagged
- You want to temporarily ignore a warning while working on a fix
- You have a regex that's intentionally complex for a specific reason
Development
Install in development mode:
# Clone the repository
git clone <repository-url>
cd redos-linter
# Install in development mode
uv sync
# Run tests
uv run pytest
# Run linter on test file
uv run python -m redos_linter tests/test.py
Test Structure
The tests are organized as follows:
test_attack_string_limit.py- Tests for attack string length limitingtest_ignore_comments.py- Tests for ignore comments functionalitytest_integration.py- Integration tests for the command-line interfacetest_main_function.py- Tests for the main linter functionalitytest_regex_extractor.py- Tests for regex extraction from Python source codetest.py- Sample Python file with various regex patterns for testing
How It Works
- AST Analysis: Scans string literals in Python source code and identifies those that look like regular expressions based on the presence of regex metacharacters. Literals used directly in
in/not incomparisons are treated as text and skipped, so checks such asassert 'schedule: "0 2 * * *"' in contentdo not need an ignore comment. Strings in assignments, containers, and nested calls (including regex calls inside comparisons) are still scanned. - ReDoS Detection: Uses the recheck engine to analyze each potential regex for potential exponential backtracking
- Attack Generation: When vulnerabilities are found, generates specific attack strings that demonstrate the issue
- Reporting: Provides clear, actionable output with source context and attack vectors
Requirements
- Python 3.10+
- Deno runtime (automatically managed via the deno Python package)
Metadata
Release files for redos-linter 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| redos_linter-0.3.2.tar.gz | 755.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| redos_linter-0.3.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.5 MB
Release files / redos_linter-0.3.2.tar.gz
| Download URL | redos_linter-0.3.2.tar.gz |
|---|---|
| Size | 755.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6a5ab53ad2f840d1e7e1f06d682119775b4e7f1ccd523c1d94c59ed3fcf5af44
|
|
BLAKE2b-256 checksum How to use checksums |
469ad2139346ffb35c8eb98741022f28a34f26a666aab5955086639e6db5d2d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / redos_linter-0.3.2-py3-none-any.whl
| Download URL | redos_linter-0.3.2-py3-none-any.whl |
|---|---|
| Size | 754.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee323d85bb63769f81da0ce2d9e9dddf16e10d51604df611f480d376562f9d86
|
|
BLAKE2b-256 checksum How to use checksums |
29c40ecfef083c849aef4f087dfb8b1f5a7fea4de5831eba76090676abb54cde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|