Secret Detection
What This Does
This skill provides a git pre‑commit hook that scans staged files for common secret patterns (API keys, passwords, tokens) and blocks the commit if any are found. It helps prevent accidental leakage of secrets to public repositories.
Inputs: Git staged files (automatically scanned by the hook) or manual file paths.
Outputs: Detection report with line numbers; non‑zero exit code if secrets found.
When To Use
Use this skill when:
- - You work with repositories that may contain sensitive credentials
- You want to prevent accidental commits of secrets
- You need a lightweight, local secret scanner for git workflows
- You want to enforce security checks before pushing to remote
Usage
Installation
CODEBLOCK0
Manual Scan
CODEBLOCK1
Hook Behavior
- - The hook runs automatically on INLINECODE0
- If secrets are detected, the commit is blocked
- The script prints the detected secrets with file names and line numbers
- Exit code 0 = no secrets found; exit code 1 = secrets found
Examples
Example 1: Installing the Hook
CODEBLOCK2
Example 2: Secret Detection Blocking a Commit
CODEBLOCK3
Example 3: Manual Scan
CODEBLOCK4
Requirements
- - Git (for hook installation)
- Python 3.6+ (for the scanner)
- No external API keys or services needed
Limitations
- - Only detects common secret patterns (AWS keys, GitHub tokens, passwords, etc.)
- May produce false positives (e.g., long random strings that aren't actually secrets)
- Does not scan binary files
- Requires manual installation per repository
- Does not replace comprehensive secret‑management solutions
- Prints first 20 characters of detected secrets to console for identification purposes
秘密检测
功能说明
该技能提供一个Git预提交钩子,用于扫描暂存文件中常见的秘密模式(API密钥、密码、令牌),并在发现任何秘密时阻止提交。它有助于防止意外将秘密泄露到公共仓库。
输入:Git暂存文件(由钩子自动扫描)或手动指定的文件路径。
输出:带有行号的检测报告;如果发现秘密则返回非零退出码。
使用场景
在以下情况下使用此技能:
- - 处理可能包含敏感凭据的仓库时
- 需要防止意外提交秘密时
- 需要为Git工作流提供轻量级本地秘密扫描器时
- 希望在推送到远程前强制执行安全检查时
使用方法
安装
bash
在Git仓库中安装钩子
./scripts/main.py install
手动扫描
bash
扫描特定文件
./scripts/main.py scan --file path/to/file
扫描所有暂存文件(与钩子行为一致)
./scripts/main.py scan --staged
钩子行为
- - 钩子在执行git commit时自动运行
- 如果检测到秘密,提交将被阻止
- 脚本会打印检测到的秘密及其文件名和行号
- 退出码0表示未发现秘密;退出码1表示发现秘密
示例
示例1:安装钩子
bash
$ ./scripts/main.py install
✓ 预提交钩子已安装至 .git/hooks/pre-commit
✓ 钩子将在每次提交时扫描秘密
示例2:秘密检测阻止提交
bash
$ git commit -m 添加配置
⚠️ 在 config.yaml 第12行检测到秘密:AWS
ACCESSKEY_ID=AKIA...
⚠️ 在 .env 第3行检测到秘密:PASSWORD=secret123
✗ 提交被阻止:发现2个秘密
示例3:手动扫描
bash
$ ./scripts/main.py scan --staged
正在扫描3个暂存文件...
✓ config.yaml:干净
✓ .env:干净
✓ src/main.py:干净
✓ 未发现秘密
系统要求
- - Git(用于安装钩子)
- Python 3.6+(用于扫描器)
- 无需外部API密钥或服务
局限性
- - 仅检测常见秘密模式(AWS密钥、GitHub令牌、密码等)
- 可能产生误报(例如,实际并非秘密的长随机字符串)
- 不扫描二进制文件
- 需要为每个仓库手动安装
- 不能替代全面的秘密管理解决方案
- 会在控制台打印检测到的秘密的前20个字符用于识别