joemunene-by/securecommit
GitHub: joemunene-by/securecommit
SecureCommit 是一款 pre-commit 安全钩子与 AI 代码审查工具,在密钥和代码漏洞进入仓库前自动拦截。
Stars: 0 | Forks: 0
# SecureCommit
**Pre-commit 安全钩子与 AI 代码审查工具** —— 在敏感信息和安全漏洞进入仓库之前将其捕获。
[](https://github.com/joemunene/securecommit/actions/workflows/ci.yml)
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
## 功能介绍
SecureCommit 会扫描你的代码,检测以下内容:
- **敏感信息和凭证**:AWS 密钥、GitHub/GitLab token、私钥、数据库连接字符串、Stripe/Twilio/SendGrid 密钥、JWT、Slack webhook 等
- **安全反模式**:SQL 注入、XSS、命令注入、不安全的反序列化、硬编码密码、弱加密算法、路径遍历、SSRF
- **高熵字符串**:使用 Shannon 熵分析,捕获在 secret 相关关键字附近分配的随机 token
它可以作为 **pre-commit 钩子**、**GitHub Action** 或独立的 **CLI 工具**使用。
## 快速开始
### 安装
```
pip install securecommit
```
### 扫描目录
```
securecommit scan .
```
### 扫描单个文件
```
securecommit scan path/to/file.py
```
### 输出格式
```
securecommit scan . --format json
securecommit scan . --format sarif --output results.sarif
securecommit scan . --format markdown
```
## Pre-commit 钩子设置
### 选项 1:pre-commit 框架
将其添加到你的 `.pre-commit-config.yaml` 中:
```
repos:
- repo: https://github.com/joemunene/securecommit
rev: v0.1.0
hooks:
- id: securecommit
```
然后运行:
```
pre-commit install
pre-commit run --all-files
```
### 选项 2:直接使用 git 钩子
```
# 直接安装为 git hook
securecommit hook
```
或者手动将其添加到 `.git/hooks/pre-commit`:
```
#!/bin/sh
securecommit hook
```
## GitHub Action 设置
将其添加到 `.github/workflows/security.yml` 中:
```
name: Security Scan
on:
pull_request:
branches: [main]
permissions:
contents: read
pull-requests: write
security-events: write
jobs:
securecommit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: joemunene/securecommit@v0.1.0
with:
severity_threshold: high
sarif_output: securecommit.sarif
```
这将执行以下操作:
1. 扫描 PR diff 中的安全问题
2. 在 PR 上发布包含检查结果的评论
3. 将 SARIF 结果上传到 GitHub Code Scanning
## 配置
在你的项目根目录下创建一个 `.securecommit.yaml`:
```
severity_threshold: high # block on high + critical
detectors:
secrets: true
patterns: true
entropy: true
allowlist:
- "test_*.py" # skip test files for some checks
- "*.md" # skip markdown
custom_patterns:
- name: "internal_token"
pattern: "INTERNAL_[A-Z0-9]{32}"
severity: critical
description: "Internal service token detected"
```
### 行内忽略
在任意行添加 `# securecommit:ignore` 即可忽略该行的扫描结果:
```
TEST_KEY = "AKIAIOSFODNN7EXAMPLE" # securecommit:ignore
```
### 白名单文件
创建一个 `.securecommit-allowlist` 文件:
```
# 跳过所有测试文件
file:test_*.py
# 全局抑制特定规则
rule:SC-S013
# 通过 hash 将特定片段加入允许列表
hash:abc123def456
```
## 输出示例
### 控制台
```
SecureCommit Scan Results
+----------+--------+--------------------------+-----------+------+
| Severity | Rule | Title | File | Line |
+----------+--------+--------------------------+-----------+------+
| CRITICAL | SC-S001| AWS Access Key ID | config.py | 12 |
| CRITICAL | SC-P005| Command Injection via... | utils.py | 45 |
| HIGH | SC-P010| Hardcoded Password | auth.py | 8 |
| MEDIUM | SC-P011| Insecure Hash (MD5) | crypto.py | 22 |
+----------+--------+--------------------------+-----------+------+
4 finding(s) | 12 file(s) scanned | Status: FAILED
```
### SARIF (GitHub Code Scanning)
SecureCommit 会生成与 GitHub Code Scanning 兼容的 SARIF v2.1.0 输出:
```
securecommit scan . --format sarif --output results.sarif
```
使用 `github/codeql-action/upload-sarif@v3` 上传结果,即可在 Security 标签页中查看结果。
## 检测器
### 敏感信息检测器 (`SC-S*`)
| 规则 | 描述 | 严重性 |
|----------|------------------------------|----------|
| SC-S001 | AWS Access Key ID | 严重 |
| SC-S002 | AWS Secret Access Key | 严重 |
| SC-S003 | GCP Service Account Key | 严重 |
| SC-S004 | Azure Secret / Connection | 高 |
| SC-S005 | GitHub PAT (ghp_) | 严重 |
| SC-S006 | GitLab PAT (glpat-) | 严重 |
| SC-S007 | 通用 API Key 赋值 | 高 |
| SC-S008 | 私钥 (PEM) | 严重 |
| SC-S009 | 数据库连接字符串 | 高 |
| SC-S010 | JWT Token | 中 |
| SC-S011 | Slack Webhook URL | 高 |
| SC-S012 | Stripe Secret Key | 严重 |
| SC-S013 | Stripe Publishable Key | 低 |
| SC-S014 | Twilio Auth Token | 高 |
| SC-S015 | SendGrid API Key | 高 |
| SC-S016 | URL 中的密码 | 高 |
### 模式检测器 (`SC-P*`)
| 规则 | 描述 | 严重性 |
|----------|--------------------------------------|----------|
| SC-P001 | SQL 注入(字符串拼接) | 高 |
| SC-P002 | SQL 注入(使用 + 进行拼接) | 高 |
| SC-P003 | 通过 innerHTML 引发的 XSS | 高 |
| SC-P004 | 通过 dangerouslySetInnerHTML 引发的 XSS | 中 |
| SC-P005 | 通过 os.system 引发的命令注入 | 严重 |
| SC-P006 | subprocess 使用 shell=True | 高 |
| SC-P007 | exec/eval 使用 | 高 |
| SC-P008 | 不安全的反序列化 (pickle) | 严重 |
| SC-P009 | 不安全的 YAML 加载 | 高 |
| SC-P010 | 硬编码密码 / Secret | 高 |
| SC-P011 | 不安全的哈希算法 (MD5) | 中 |
| SC-P012 | 不安全的哈希算法 (SHA1) | 中 |
| SC-P013 | 使用 ECB 模式 | 高 |
| SC-P014 | 路径遍历 | 高 |
| SC-P015 | SSRF 指标 | 高 |
| SC-P016 | 禁用 TLS 验证 | 中 |
### 熵检测器 (`SC-E*`)
| 规则 | 描述 | 严重性 |
|----------|--------------------------|----------|
| SC-E001 | 高熵字符串 | 高 |
## 开发
```
git clone https://github.com/joemunene/securecommit.git
cd securecommit
make dev # install with dev dependencies
make test # run test suite
make lint # run linter
make scan # self-scan the project
```
## 许可证
MIT License。版权所有 (c) 2026 Joe Munene。
标签:LLM应用, Pre-commit, StruQ, 前端框架, 开发安全, 逆向工具, 错误基检测, 静态代码分析