Skip to main content

Security guard against typosquatting in Python dependencies

Project description

typo-shield ๐Ÿ›ก๏ธ

PyPI version Python versions License: Apache 2.0 Tests

Security guard against typosquatting in Python dependencies

A CLI tool and pre-commit hook that detects suspicious package names and imports in your git diff to protect against supply chain attacks.

๐Ÿš€ Quick Start

# Install
pip install typo-shield

# Scan staged changes
typo-shield scan

# Scan commit range
typo-shield scan --diff-range main...feature

# JSON output for CI
typo-shield scan --format json

๐Ÿ“‹ Features

  • โœ… Detects typosquatting in dependencies (Levenshtein distance)
  • โœ… Scans Python imports (AST-based)
  • โœ… Parses requirements.txt and pyproject.toml (PEP 621 + Poetry)
  • โœ… Identifies missing dependencies
  • โœ… Pre-commit hook support
  • โœ… JSON output for CI/CD
  • โœ… Configurable via .typo-shield.toml

๐Ÿ“ฆ Installation

Requirements: Python 3.10 or newer

pip install typo-shield

Development installation:

git clone https://github.com/kszmigiel/typo-shield
cd typo-shield
pip install -e ".[dev]"

๐Ÿ” Usage

Basic scanning

# Scan staged changes (default)
typo-shield scan

# Scan specific commit range
typo-shield scan --diff-range main...feature

# Strict mode (fail on unknown imports)
typo-shield scan --strict-imports

# Fail on warnings too
typo-shield scan --fail-on warn

Output formats

# Human-readable text (default)
typo-shield scan

# JSON for CI/CD
typo-shield scan --format json

Exclude patterns

# Exclude test files
typo-shield scan --exclude "tests/**" --exclude "*.pyc"

๐Ÿช Pre-commit Hook

Integrate typo-shield with pre-commit to automatically check your dependencies on every commit.

Add to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/kszmigiel/typo-shield
    rev: v0.1.0  # Use the latest version
    hooks:
      - id: typo-shield

For stricter checking (fail on warnings too):

repos:
  - repo: https://github.com/kszmigiel/typo-shield
    rev: v0.1.0
    hooks:
      - id: typo-shield-strict

Then install the pre-commit hook:

pre-commit install

Now typo-shield will run automatically before each commit! ๐ŸŽ‰

Note: Pre-commit hooks run on staged changes only. If you want to scan a specific commit range, use the CLI directly.

โš™๏ธ Configuration

Create .typo-shield.toml in your repository root:

[policy]
fail_on = "fail"
strict_imports = false

[allow]
deps = ["internal-lib", "private-package"]
modules = ["internalpkg"]

[exclude]
paths = ["tests/**", "docs/**"]

๐Ÿšฆ Exit Codes

  • 0 - No issues found
  • 1 - Security issues detected (FAIL or WARN based on --fail-on)
  • 2 - Tool error (git not found, invalid config, etc.)

๐Ÿ“Š Example Output

๐Ÿ” typo-shield scan

Summary: 1 FAIL, 2 WARN, 5 INFO

โŒ FAILURES (1)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[TS001] Suspected typosquat
  File: requirements.txt:12
  Package: reqeusts
  Reason: Very similar to popular package "requests" (distance: 1)
  Suggestion: Did you mean "requests"?

โš ๏ธ  WARNINGS (2)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[TS101] Import without declared dependency
  File: app.py:5
  Module: numpy
  Suggestion: Add "numpy" to your dependencies

Result: FAILED (exit code 1)

๐Ÿ› ๏ธ Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov

# Linting
ruff check .

# Type checking
mypy typo_shield/

๐Ÿ“œ License

Apache License 2.0 - see LICENSE file for details.

๐Ÿค Contributing

Contributions welcome! Please read CONTRIBUTING.md for guidelines.

๐Ÿ”— Links

โš ๏ธ Status

Alpha (v0.1.0) - Under active development. API may change.


Made with โค๏ธ to protect the Python ecosystem from supply chain attacks.

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

typo_shield-0.1.1.tar.gz (61.8 kB view details)

Uploaded Source

Built Distribution

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

typo_shield-0.1.1-py3-none-any.whl (47.5 kB view details)

Uploaded Python 3

File details

Details for the file typo_shield-0.1.1.tar.gz.

File metadata

  • Download URL: typo_shield-0.1.1.tar.gz
  • Upload date:
  • Size: 61.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for typo_shield-0.1.1.tar.gz
Algorithm Hash digest
SHA256 780f65c8d1167b3c12c2c44a085dcd62ece7d28bc8b704ac903be090f0026dde
MD5 8506e2e7e0798c4625d578d0ceaba300
BLAKE2b-256 af314e990ea7dc7edf788b894fdc50e148729755bb677543e9de35bf1847fa3e

See more details on using hashes here.

File details

Details for the file typo_shield-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: typo_shield-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 47.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for typo_shield-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 041d0588c6e385fe2ea42690ed51f4392235b130f306ed95258bce0b693ad146
MD5 b4053f2356f57758193070511a095f25
BLAKE2b-256 47aa24a39ff1b13ae2b416a2213ab52a34a4034a11ec99faa1f718ce22031abc

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