Security guard against typosquatting in Python dependencies
Project description
typo-shield ๐ก๏ธ
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.txtandpyproject.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 found1- 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
- Repository: https://github.com/kszmigiel/typo-shield
- Issues: https://github.com/kszmigiel/typo-shield/issues
- PyPI: https://pypi.org/project/typo-shield/
โ ๏ธ Status
Alpha (v0.1.0) - Under active development. API may change.
Made with โค๏ธ to protect the Python ecosystem from supply chain attacks.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
780f65c8d1167b3c12c2c44a085dcd62ece7d28bc8b704ac903be090f0026dde
|
|
| MD5 |
8506e2e7e0798c4625d578d0ceaba300
|
|
| BLAKE2b-256 |
af314e990ea7dc7edf788b894fdc50e148729755bb677543e9de35bf1847fa3e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
041d0588c6e385fe2ea42690ed51f4392235b130f306ed95258bce0b693ad146
|
|
| MD5 |
b4053f2356f57758193070511a095f25
|
|
| BLAKE2b-256 |
47aa24a39ff1b13ae2b416a2213ab52a34a4034a11ec99faa1f718ce22031abc
|