Skip to main content

Python package security scanner โ€” detects typosquats, CVEs, malicious code, and supply-chain risks before install

Project description

๐Ÿ›ก safepip

Python package security scanner โ€” catches attacks before anything installs.

PyPI version Python License: MIT Tests No API Key CI


What is safepip?

safepip scans Python packages for security risks before any code downloads or runs. It catches typosquatting attacks, malicious packages, CVEs, and supply-chain risks in real time โ€” with no API key, no cloud service, and no changes to your existing workflow.

pip install safepip-scanner
safepip watch enable        # intercepts every pip install system-wide

After that, pip install X is automatically scanned in CMD, PowerShell, and Admin CMD.


Features

Feature Description
๐Ÿ” Typosquat Detection Levenshtein + keyboard proximity against 8,000+ real packages
๐Ÿฆ  Known Threat DB 70+ confirmed malicious packages, auto-updated from live feed
๐Ÿ“‹ CVE Analysis Real-time OSV.dev lookups โ€” critical / high / medium / low
๐Ÿ”ฌ Static Code Analysis Scans wheel contents for exec chains, credential leaks, shells
๐Ÿ“ˆ Release Anomaly Burst patterns, dormancy breaks, maintainer reputation
๐Ÿ“ฆ SBOM Generation CycloneDX 1.4 โ€” accepted by GitHub, FOSSA, Snyk
๐ŸŽฏ Watch Mode Intercepts every pip install system-wide (all terminal types)
๐Ÿ“Š Dashboard Local browser UI at localhost:7676 with charts and history
โšก Zero API Key Fully functional without any paid plan or cloud account
๐Ÿค– AI Upgrade Set ANTHROPIC_API_KEY for Claude-powered deep analysis

Installation

pip install safepip-scanner

Or from source:

git clone https://github.com/busupallinaveen-hash/safepip-scanner-v1.4.1
cd safepip
pip install -e .

Quick Start

# Scan before installing
safepip scan requests
safepip scan langchian          # โœ• BLOCK โ€” typosquat of langchain
safepip scan requirements.txt   # scan a whole file

# Scan and install if safe
safepip install flask

# System-wide interception
safepip watch enable            # every pip install X is now scanned
safepip watch status            # check CMD + PowerShell coverage

# Generate SBOM
safepip sbom requests numpy flask --output sbom.json

Example Outputs

โœ… Safe Package

$ safepip scan requests

[safepip] requests v2.34.2 | score=0 | LOW | INSTALL | 0.87s
  [LOW] Info: 'requests' is a well-known, widely-trusted PyPI package
  RULE: allowlist: 'requests' is trusted

โš  Warning โ€” CVEs Found

$ safepip scan pycrypto

[safepip] pycrypto v2.6.1 | score=52 | MEDIUM | WARN | 5.1s
  [HIGH] Code: 1 critical CVE โ€” CVE-2018-6594, CVE-2013-7459
  [HIGH] Code: Install hook abuse in setup.py: os.system("chmod 0755 configure")
  [HIGH] Code: exec/eval on non-constant argument in setup.py
  Decision: โš  WARN  โ€” review findings before installing

โœ• Blocked โ€” Typosquat Attack

$ safepip scan langchian

[safepip] langchian v0.2.5 | score=100 | HIGH | BLOCK | 3.4s
  [HIGH] Supply Chain: Confirmed threat: typosquat targeting 'langchain'
  [HIGH] Code: Outbound network call in splitter.py
  [MED]  Code: High release velocity: 1.2 releases/day (4 total)
  Decision: โœ• BLOCK

๐Ÿ“ฆ SBOM Generation

$ safepip sbom requests flask numpy --output sbom.json

safepip sbom โ€” scanning 3 package(s)
  requests โ†’ score 0 INSTALL
  flask    โ†’ score 0 INSTALL
  numpy    โ†’ score 0 INSTALL
โœ“ SBOM written to sbom.json
  CycloneDX SBOM โ€” 3 components, 0 blocked, 0 warned

Commands

# Scanning
safepip scan <package> [packages...]    # scan one or more packages
safepip scan requirements.txt           # auto-reads requirements files
safepip scan sklearn cv2 PIL            # resolves aliases automatically
safepip scan-file requirements.txt      # explicit file scan shorthand
safepip install <package>               # scan then install if safe
safepip batch requirements.txt          # batch with summary table
safepip audit                           # scan all installed packages

# SBOM
safepip sbom <packages> -o sbom.json    # CycloneDX 1.4 SBOM

# Watch mode
safepip watch enable                    # intercept all pip install calls
safepip watch disable                   # remove all shims and aliases
safepip watch status                    # show CMD + PowerShell coverage

# Dashboard
safepip dashboard                       # open at http://localhost:7676

# Maintenance
safepip update                          # refresh live threat feed
safepip history                         # view scan history
safepip history --verdict HIGH          # filter by verdict
safepip history --stats                 # aggregate stats

Package Aliases

These common import names are resolved automatically:

Import PyPI package
sklearn scikit-learn
cv2 opencv-python
PIL pillow
bs4 beautifulsoup4
yaml pyyaml
dotenv python-dotenv
jwt pyjwt
safepip scan sklearn cv2 PIL    # automatically resolved

Watch Mode โ€” All Terminals

safepip watch enable installs interception in three layers:

Layer Covers Method
PowerShell alias PS5, PS7, Windows Terminal function pip {} in profile
pip.bat shim CMD, Windows Terminal (CMD) Written beside pip.exe
UAC elevation Administrator CMD, Admin PS UAC prompt โ†’ system-level shim
safepip watch enable
# Open a new terminal (CMD or PowerShell)
pip install colourama    # โœ• BLOCK โ€” intercepted automatically
pip install requests     # โœ“ INSTALL โ€” scanned and cleared
pip list                 # โ†’ passes through unchanged

Scoring

Score Verdict Action
0โ€“30 ๐ŸŸข LOW INSTALL
31โ€“65 ๐ŸŸก MEDIUM WARN
66โ€“100 ๐Ÿ”ด HIGH BLOCK

Key contributors:

  • +55โ€“85 โ€” Not on PyPI or known threat database
  • +40 โ€” Likely typosquat (โ‰ฅ75% name similarity)
  • +20/12/6 โ€” CVE severity (critical/high/medium) per CVE
  • +30 โ€” Critical static code finding (exec+base64, reverse shell)
  • +30 โ€” Latest release yanked
  • +15 โ€” Package age < 3 months
  • โ€“20 โ€” Trusted package (numpy, requests, django, flaskโ€ฆ)

Detection Pipeline

Each scan runs 7 stages before any code executes:

1. PyPI metadata      โ†’ version, age, author, deps, license
2. Typosquat check    โ†’ Levenshtein + keyboard proximity + homoglyph
3. CVE lookup         โ†’ OSV.dev realtime (critical/high/medium/low)
4. Download stats     โ†’ pypistats.org reputation tier
5. Release anomaly    โ†’ burst patterns, dormancy breaks, velocity
6. Code scan          โ†’ wheel/sdist inspection (no install)
7. Risk scoring       โ†’ rule-based (or Claude AI if key is set)

CI/CD Integration

Exit codes

Flag Exit 0 Exit 1
(default) All INSTALL Any WARN or BLOCK
--fail-on-high INSTALL + WARN Any BLOCK
--fail-on-warn INSTALL only Any WARN or BLOCK
# Strict: fail build on any WARN or BLOCK
safepip scan-file requirements.txt --fail-on-warn

# Lenient: fail build only on confirmed HIGH-risk packages
safepip scan-file requirements.txt --fail-on-high

# Generate SBOM artifact
safepip sbom -r requirements.txt --output sbom.json

# SARIF for GitHub Security tab
safepip batch requirements.txt --sarif results.sarif

# HTML report for download artifact
safepip scan-file requirements.txt --html security-report.html

# JSON output for custom tooling
safepip scan requests --json | jq '{pkg: .package, ok: (.decision == "INSTALL")}'

GitHub Actions example:

name: Dependency Security Scan

on: [push, pull_request]

jobs:
  safepip:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install safepip
        run: pip install safepip-scanner

      - name: Update threat database
        run: safepip update-db

      - name: Scan dependencies (fail on HIGH)
        run: safepip scan-file requirements.txt --fail-on-high

      - name: Generate HTML report
        if: always()   # run even if scan fails
        run: safepip scan-file requirements.txt --html security-report.html --output plain

      - name: Upload HTML report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: security-report
          path: security-report.html

      - name: Generate SBOM
        run: safepip sbom -r requirements.txt --output sbom.json

      - name: Upload SBOM
        uses: actions/upload-artifact@v4
        with:
          name: sbom
          path: sbom.json

      - name: Upload SARIF to GitHub Security tab
        if: always()
        run: safepip batch requirements.txt --sarif results.sarif
      - uses: github/codeql-action/upload-sarif@v3
        if: always()
        with:
          sarif_file: results.sarif

Configuration

Create .safepip.yml in your project root:

block_on_high: true
warn_new_packages: true
new_package_months: 6

allowlist:
  - requests
  - numpy

blocklist:
  - colourama
  - setup-tools

Claude AI Mode

For enhanced AI-powered analysis:

export ANTHROPIC_API_KEY=sk-ant-...
safepip scan requests    # "Engine: Claude AI" shown in header

Without a key, the local rule-based scorer handles everything.


Architecture

safe_pip/
โ”œโ”€โ”€ scanner.py           Core 7-stage scan pipeline
โ”œโ”€โ”€ typosquat.py         Name similarity engine
โ”œโ”€โ”€ osv.py              CVE lookups (OSV.dev)
โ”œโ”€โ”€ code_scanner.py      Static analysis of wheel/sdist
โ”œโ”€โ”€ release_analyzer.py  Release velocity + maintainer reputation
โ”œโ”€โ”€ local_scorer.py      Rule-based risk scoring
โ”œโ”€โ”€ threat_feed.py       Live threat DB (GitHub-sourced, 24h cache)
โ”œโ”€โ”€ sbom.py             CycloneDX 1.4 SBOM generation
โ”œโ”€โ”€ dashboard.py         Local HTTP dashboard
โ”œโ”€โ”€ watch.py            System-wide pip interceptor
โ”œโ”€โ”€ policy.py           Configurable rules (.safepip.yml)
โ”œโ”€โ”€ display.py          Rich terminal output
โ”œโ”€โ”€ db.py               SQLite scan history
โ””โ”€โ”€ cli.py              Click CLI entry point

Contributing

git clone https://github.com/busupallinaveen-hash/safepip-scanner-v1.4.1
cd safepip
pip install -e ".[dev]"
pytest tests/

See CONTRIBUTING.md for guidelines.


License

MIT โ€” see LICENSE


Changelog

See CHANGELOG.md for release history.

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

safepip_scanner-1.4.1.tar.gz (158.2 kB view details)

Uploaded Source

Built Distribution

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

safepip_scanner-1.4.1-py3-none-any.whl (111.5 kB view details)

Uploaded Python 3

File details

Details for the file safepip_scanner-1.4.1.tar.gz.

File metadata

  • Download URL: safepip_scanner-1.4.1.tar.gz
  • Upload date:
  • Size: 158.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for safepip_scanner-1.4.1.tar.gz
Algorithm Hash digest
SHA256 4446f54bcdb0a0f7a701f96e730fa8312f21e9323032e33f5b3fdc24e981d373
MD5 18616394d1974c2edc9b3c1e47cc02ae
BLAKE2b-256 d019d796146a26798ecc35441ecb9e7760ff2ca13af0c11182849e8803e6bd46

See more details on using hashes here.

File details

Details for the file safepip_scanner-1.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for safepip_scanner-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64afbfb0af52d77f9508812bffdc281a0cc5cd94bec53757c2c54bd39ca26381
MD5 ab903422f63e9e3a63e384a861b95aad
BLAKE2b-256 c4aa6ae7ff02a643ba478611601821c75e375552b5227ba676c08a11336421d7

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