Skip to content
项目

rubyruby1512-cmd/pre-push-secret-scanner

在 GitHub 查看

关于

A lightweight pre-push git hook that scans staged files for exposed API keys, tokens, passwords, and sensitive files before they reach the remote. Zero dependencies — Python only.

安全与隐私

这些数字如何统计

可以做什么

  • 20+ secret patterns — AWS, GCP, GitHub/GitLab tokens, Slack, Discord, Stripe, JWT, private key blocks, DB connection strings, generic passwords, and more
  • Sensitive filename check — warns if .env, credentials.json, id_rsa, etc. are staged but not in .gitignore
  • Value masking — shows only the first/last few characters so you know what was found without printing the full secret
  • Smart skipping — ignores node_modules, .git, binary files, lock files

实测星标增长

24小时
0
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

pre-push-secret-scanner

Scans staged files before every git push to catch exposed API keys, tokens, hard-coded passwords, and sensitive files missing from .gitignore.


The Problem

Developers accidentally commit .env files, API keys, and passwords to public repos every day. Bots crawl GitHub and harvest leaked credentials within minutes. This tool intercepts git push and blocks it the moment anything suspicious is detected.


Features

  • 20+ secret patterns — AWS, GCP, GitHub/GitLab tokens, Slack, Discord, Stripe, JWT, private key blocks, DB connection strings, generic passwords, and more
  • Sensitive filename check — warns if .env, credentials.json, id_rsa, etc. are staged but not in .gitignore
  • Value masking — shows only the first/last few characters so you know what was found without printing the full secret
  • Smart skipping — ignores node_modules, .git, binary files, lock files
  • Zero dependencies — standard library only, requires Python 3.10+
  • Exit code 1 — blocks the push when issues are found, allowing the hook to do its job

Installation

The hook is a pure Python script — no bash or shell required.

Windows (PowerShell)

# 1. Clone this tool
git clone https://github.com/yourname/pre-push-secret-scanner
cd pre-push-secret-scanner

# 2a. PowerShell installer (recommended on Windows)
.\install.ps1                        # installs into current repo
.\install.ps1 -RepoPath C:\my-app   # installs into a specific repo

# 2b. Or use the Python installer
py -3 install.py
py -3 install.py C:\my-app

macOS / Linux

git clone https://github.com/yourname/pre-push-secret-scanner
cd pre-push-secret-scanner
python3 install.py
python3 install.py /path/to/your-project

The installer:

  • Copies hooks/pre-push (a Python script, not a shell script) → .git/hooks/pre-push
  • Copies scanner.py to the repo root so the hook can call it
  • Runs git config core.hooksPath .git/hooks so Git finds the hook correctly

Manual Usage

# Windows PowerShell
py -3 scanner.py              # scan staged files (default)
py -3 scanner.py --all        # scan entire working tree
py -3 scanner.py src\config.py .env.local   # scan specific files
py -3 scanner.py --no-fail    # warn only, don't block
# macOS / Linux
python3 scanner.py
python3 scanner.py --all

Example Output

════════════════════════════════════════════════════════════
  🔍  PRE-PUSH SECRET SCANNER
════════════════════════════════════════════════════════════
  Files scanned: 4  |  Skipped: 1

  ⛔  SENSITIVE FILES NOT IN .GITIGNORE:
      • .env.local

  ⚠️   2 POTENTIAL SECRET(S) DETECTED:

  📄 src/api/client.py
      Line   12  [AWS Access Key]  AKIA...WXYZ
      Line   13  [Generic Secret]  sk-l...9a3f

════════════════════════════════════════════════════════════

  Push blocked. Fix the issues above and try again.
  To push anyway (not recommended): git push --no-verify

Patterns Covered

Type Example match
AWS Access Key AKIA...
AWS Secret Key aws_secret_key = "..."
GCP API Key AIza...
GitHub Token ghp_... / github_pat_...
GitLab Token glpat-...
Slack Token xoxb-...
Slack Webhook hooks.slack.com/services/...
Discord Token long base64 token
Telegram Bot Token 123456789:AAF...
Stripe Key sk_live_... / pk_live_...
JWT eyJ...
Private Key Block -----BEGIN RSA PRIVATE KEY-----
DB Connection String postgres://user:pass@host
Generic password password = "hunter2"
Generic API key api_key = "abc123..."
Bearer token Authorization: Bearer xxx

Customizing Patterns

Open scanner.py and edit the PATTERNS dict:

PATTERNS = {
    "My Custom Pattern": r"MY_TOKEN_[A-Z0-9]{32}",
    # ...
}

To skip additional file extensions:

SKIP_EXTENSIONS = {".snap", ".min.js", ...}

Bypassing When Necessary

git push --no-verify

Use only when you are certain of a false positive. Do not make it a habit.


Requirements

  • Python 3.10+
  • Git 2.x (Git for Windows works fine — no Git Bash needed)
  • Windows PowerShell 5+ / PowerShell Core 7+, macOS, or Linux

License

MIT

探索相关项目

加载中…