Skip to main content

Add your description here

Project description

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 all 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/

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 limiting
  • test_ignore_comments.py - Tests for ignore comments functionality
  • test_integration.py - Integration tests for the command-line interface
  • test_main_function.py - Tests for the main linter functionality
  • test_regex_extractor.py - Tests for regex extraction from Python source code
  • test.py - Sample Python file with various regex patterns for testing

How It Works

  1. AST Analysis: Scans all string literals in Python source code and identifies those that look like regular expressions based on the presence of regex metacharacters
  2. ReDoS Detection: Uses the recheck engine to analyze each potential regex for potential exponential backtracking
  3. Attack Generation: When vulnerabilities are found, generates specific attack strings that demonstrate the issue
  4. Reporting: Provides clear, actionable output with source context and attack vectors

Requirements

  • Python 3.10+
  • Deno runtime (automatically managed via the deno Python package)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redos_linter-0.2.0.tar.gz (753.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redos_linter-0.2.0-py3-none-any.whl (753.0 kB view details)

Uploaded Python 3

File details

Details for the file redos_linter-0.2.0.tar.gz.

File metadata

  • Download URL: redos_linter-0.2.0.tar.gz
  • Upload date:
  • Size: 753.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

File hashes

Hashes for redos_linter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c2d7eda8feb0859f73e43b90195357c84ddba980b7a490b018534686076959a6
MD5 ecf7578a34a0f0a3760147c6c0324589
BLAKE2b-256 6d96dd8cf6b4e8848154bc048fdc77a8d484a59b6f79a80fae98a82f40e338c7

See more details on using hashes here.

File details

Details for the file redos_linter-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: redos_linter-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 753.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

File hashes

Hashes for redos_linter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fec6bec6a365a5cfc6ca699ebbd785980776670a4965c5390e05dbb5732b50d
MD5 6c31d7450f00ff03e726722d3b0ef8d8
BLAKE2b-256 346634fe5ac68bfc8417cb04fb5e6d1d145a2780738a35215e19de360f3670de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page