commit-ai-guardian 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- commit_ai_guardian-0.2.0/.ai-review/.gitignore +3 -0
- commit_ai_guardian-0.2.0/.ai-review/config.yaml +48 -0
- commit_ai_guardian-0.2.0/.ai-review/example/hardcoded-password.md +57 -0
- commit_ai_guardian-0.2.0/.ai-review/example/npe.md +63 -0
- commit_ai_guardian-0.2.0/.ai-review/example/resource-leak.md +54 -0
- commit_ai_guardian-0.2.0/.ai-review/example/sql-injection.md +71 -0
- commit_ai_guardian-0.2.0/.ai-review/example/weak-hash.md +56 -0
- commit_ai_guardian-0.2.0/.ai-review/example/xss.md +55 -0
- commit_ai_guardian-0.2.0/.ai-review/prompts/diff_review.md +73 -0
- commit_ai_guardian-0.2.0/.ai-review/prompts/full_file_review.md +74 -0
- commit_ai_guardian-0.2.0/.ai-review/prompts/system_message.txt +16 -0
- commit_ai_guardian-0.2.0/.github/workflows/publish.yml +50 -0
- commit_ai_guardian-0.2.0/.gitignore +43 -0
- commit_ai_guardian-0.2.0/PKG-INFO +364 -0
- commit_ai_guardian-0.2.0/README.md +337 -0
- commit_ai_guardian-0.2.0/STUDY.md +643 -0
- commit_ai_guardian-0.2.0/TECHNICAL.md +1073 -0
- commit_ai_guardian-0.2.0/plan.md +18 -0
- commit_ai_guardian-0.2.0/pyproject.toml +73 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/__init__.py +12 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/__main__.py +6 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/ai_engine.py +2047 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/case_loader.py +417 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/case_validator.py +159 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/cli.py +718 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/config.py +372 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/diff_collector.py +362 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/file_collector.py +327 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/hook_installer.py +680 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/prompt_loader.py +312 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/result_formatter.py +381 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/templates/examples/js/mongoose-query-acceptable.md +81 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/templates/examples/js/redundant-null-union.md +71 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/templates/examples/js/vue-default-slot-redundant.md +44 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/templates/examples/js/vxe-size-redundant.md +88 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/templates/pre-commit-hook-template +39 -0
- commit_ai_guardian-0.2.0/src/commit_ai_guardian/utils.py +87 -0
- commit_ai_guardian-0.2.0/test_json_extract.py +149 -0
- commit_ai_guardian-0.2.0/tests/conftest.py +122 -0
- commit_ai_guardian-0.2.0/tests/test_ai_engine.py +419 -0
- commit_ai_guardian-0.2.0/tests/test_ai_engine_logs.py +108 -0
- commit_ai_guardian-0.2.0/tests/test_case_loader.py +556 -0
- commit_ai_guardian-0.2.0/tests/test_config.py +478 -0
- commit_ai_guardian-0.2.0/tests/test_diff_collector.py +703 -0
- commit_ai_guardian-0.2.0/tests/test_file_collector.py +644 -0
- commit_ai_guardian-0.2.0/tests/test_hook_installer.py +393 -0
- commit_ai_guardian-0.2.0/tests/test_json_fix_ai.py +271 -0
- commit_ai_guardian-0.2.0/tests/test_prompt_loader.py +489 -0
- commit_ai_guardian-0.2.0/tests/test_result_formatter.py +411 -0
- commit_ai_guardian-0.2.0/tests/test_utils.py +349 -0
- commit_ai_guardian-0.2.0/uv.lock +2208 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# 项目级别配置文件
|
|
2
|
+
# 只填写需要覆盖全局配置的项,留空则使用全局配置
|
|
3
|
+
# 全局配置位置: ~/.commit-ai-guardian/config.yaml
|
|
4
|
+
|
|
5
|
+
# AI API 密钥
|
|
6
|
+
api_key: ""
|
|
7
|
+
|
|
8
|
+
# API 地址
|
|
9
|
+
api_base: ""
|
|
10
|
+
|
|
11
|
+
# 模型名称
|
|
12
|
+
model: ""
|
|
13
|
+
|
|
14
|
+
# 审核报告语言 (zh-CN/en)
|
|
15
|
+
language: ""
|
|
16
|
+
|
|
17
|
+
# 是否启用 AI 审核(false=跳过,直接通过)
|
|
18
|
+
enabled: true
|
|
19
|
+
|
|
20
|
+
# 阻断级别 (info/warning/error/critical)
|
|
21
|
+
severity_threshold: ""
|
|
22
|
+
|
|
23
|
+
# diff 审核模式: full=完整文件(默认), diff=只审变更
|
|
24
|
+
diff_mode: ""
|
|
25
|
+
|
|
26
|
+
# 最大审核文件大小 (KB)
|
|
27
|
+
max_file_size: 0
|
|
28
|
+
|
|
29
|
+
# 缓存存活时间(1d=1天, 12h=12小时, 30m=30分钟)
|
|
30
|
+
cache_ttl: "1d"
|
|
31
|
+
|
|
32
|
+
# 日志存活时间(1h=1小时, 30m=30分钟, 0=不清理)
|
|
33
|
+
log_ttl: "1h"
|
|
34
|
+
|
|
35
|
+
# 要审核的目录/文件模式(glob,如 ["src/**", "app/**"])
|
|
36
|
+
include_patterns: ["*"]
|
|
37
|
+
|
|
38
|
+
# 忽略的文件模式(glob 格式)
|
|
39
|
+
ignore_patterns: ["*.lock", "*.md", "*.txt", "*.svg", "*.png", "*.jpg", "*.jpeg", "*.gif", "*.ico", "*.woff", "*.woff2", "*.ttf", "*.eot", "*.otf", "*.mp3", "*.mp4", "*.avi", "*.pdf", "*.doc", "*.docx", "*.zip", "*.tar", "*.gz", "*.rar", "*.7z", "*.exe", "*.dll", "*.so", "*.dylib", "*.class", "*.jar", "*.ear", "*.egg", "*.whl", "*.parquet", "*.pkl", "*.pickle", "*.model", "*.bin", "*.onnx", "*.pb"]
|
|
40
|
+
|
|
41
|
+
# API 超时 (秒)
|
|
42
|
+
timeout: 0
|
|
43
|
+
|
|
44
|
+
# AI 最大返回长度 (token 数,支持 4K/8K 写法)
|
|
45
|
+
max_tokens: 0
|
|
46
|
+
|
|
47
|
+
# HTTP 代理地址
|
|
48
|
+
proxy: ""
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 硬编码密码/密钥
|
|
3
|
+
severity: 9
|
|
4
|
+
level: critical
|
|
5
|
+
category: 安全漏洞
|
|
6
|
+
tags: [密码, 密钥, 配置]
|
|
7
|
+
languages: [python, java, javascript, go, rust, php]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 问题描述
|
|
11
|
+
|
|
12
|
+
密码、API Key、密钥等敏感信息直接写在代码中
|
|
13
|
+
|
|
14
|
+
## 为什么这是个问题
|
|
15
|
+
|
|
16
|
+
代码会被提交到 Git 仓库,所有有权限的人都能看到。一旦泄露,攻击者可以直接使用这些凭证访问系统。
|
|
17
|
+
|
|
18
|
+
## 不修复的后果
|
|
19
|
+
|
|
20
|
+
- 生产环境凭证泄露
|
|
21
|
+
- 数据库/第三方服务被非法访问
|
|
22
|
+
- 数据被窃取或篡改
|
|
23
|
+
|
|
24
|
+
## 参考
|
|
25
|
+
|
|
26
|
+
- [OWASP - Secrets Management](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 坏代码 ❌
|
|
31
|
+
|
|
32
|
+
### 明文密码
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
DB_PASSWORD = "MyP@ssw0rd123"
|
|
36
|
+
API_KEY = "sk-abc123xyz789"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 好代码 ✅
|
|
42
|
+
|
|
43
|
+
### 环境变量
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
DB_PASSWORD = os.environ.get("DB_PASSWORD")
|
|
47
|
+
API_KEY = os.environ.get("API_KEY")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 检查清单
|
|
53
|
+
|
|
54
|
+
- [ ] 代码中是否有明文密码、token、密钥?
|
|
55
|
+
- 搜索 `password`、`secret`、`token`、`key` 等关键词
|
|
56
|
+
- [ ] 是否使用了环境变量或配置中心?
|
|
57
|
+
- 生产环境配置应该与代码分离
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 空指针/None 引用
|
|
3
|
+
severity: 7
|
|
4
|
+
level: error
|
|
5
|
+
category: bug
|
|
6
|
+
tags: [空指针, NPE, 判空]
|
|
7
|
+
languages: [python, java, javascript, go]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 问题描述
|
|
11
|
+
|
|
12
|
+
未检查空值就直接使用对象属性或方法
|
|
13
|
+
|
|
14
|
+
## 为什么这是个问题
|
|
15
|
+
|
|
16
|
+
对象可能为 None/null 时,直接访问属性或调用方法会抛出异常,导致程序崩溃。
|
|
17
|
+
|
|
18
|
+
## 不修复的后果
|
|
19
|
+
|
|
20
|
+
- 程序运行时崩溃
|
|
21
|
+
- 用户体验差(500 错误)
|
|
22
|
+
- 可能丢失未保存的数据
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 坏代码 ❌
|
|
27
|
+
|
|
28
|
+
### 直接访问可能为 None 的属性
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
name = user.name # user 可能为 None
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 直接访问可能为空的列表
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
first = items[0] # items 可能为空
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 好代码 ✅
|
|
43
|
+
|
|
44
|
+
### 判空后使用
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
name = user.name if user else "anonymous"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 检查列表长度
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
first = items[0] if items else None
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 检查清单
|
|
59
|
+
|
|
60
|
+
- [ ] 使用对象前是否检查了 None/null?
|
|
61
|
+
- 关注函数返回值、数据库查询结果
|
|
62
|
+
- [ ] 列表/字典索引前是否检查了长度?
|
|
63
|
+
- 访问 `[0]` 前确认列表非空
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 资源泄漏
|
|
3
|
+
severity: 6
|
|
4
|
+
level: error
|
|
5
|
+
category: bug
|
|
6
|
+
tags: [资源, 连接, 文件]
|
|
7
|
+
languages: [python, java, javascript, go, rust]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 问题描述
|
|
11
|
+
|
|
12
|
+
文件、数据库连接、锁等未正确释放
|
|
13
|
+
|
|
14
|
+
## 为什么这是个问题
|
|
15
|
+
|
|
16
|
+
打开的资源如果不关闭,会持续占用系统资源。高并发时可能导致资源耗尽,系统无法响应。
|
|
17
|
+
|
|
18
|
+
## 不修复的后果
|
|
19
|
+
|
|
20
|
+
- 文件句柄耗尽,无法打开新文件
|
|
21
|
+
- 数据库连接池耗尽
|
|
22
|
+
- 内存泄漏
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 坏代码 ❌
|
|
27
|
+
|
|
28
|
+
### 手动 open/close(异常时不关闭)
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
f = open("data.txt")
|
|
32
|
+
data = f.read()
|
|
33
|
+
f.close() # 如果上面抛异常,这里不会执行
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 好代码 ✅
|
|
39
|
+
|
|
40
|
+
### with 语句自动关闭
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
with open("data.txt") as f:
|
|
44
|
+
data = f.read()
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## 检查清单
|
|
50
|
+
|
|
51
|
+
- [ ] 文件打开是否用了 `with`/`try-finally`?
|
|
52
|
+
- 搜索 `open(` 但不包含 `with` 的地方
|
|
53
|
+
- [ ] 数据库连接是否正确关闭?
|
|
54
|
+
- 检查 `conn.close()` 是否在 `finally` 中
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: SQL 注入
|
|
3
|
+
severity: 9
|
|
4
|
+
level: critical
|
|
5
|
+
category: 安全漏洞
|
|
6
|
+
tags: [SQL, 注入, 数据库]
|
|
7
|
+
languages: [python, java, javascript, go, php]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 问题描述
|
|
11
|
+
|
|
12
|
+
用户输入直接拼接到 SQL 语句中
|
|
13
|
+
|
|
14
|
+
## 为什么这是个问题
|
|
15
|
+
|
|
16
|
+
攻击者可以通过构造特殊的输入来改变 SQL 语句的语义,从而绕过认证、读取敏感数据、甚至删除数据库。
|
|
17
|
+
|
|
18
|
+
## 不修复的后果
|
|
19
|
+
|
|
20
|
+
- 数据泄露(用户信息、密码、交易记录)
|
|
21
|
+
- 数据被篡改或删除
|
|
22
|
+
- 系统被完全控制
|
|
23
|
+
|
|
24
|
+
## 参考
|
|
25
|
+
|
|
26
|
+
- [OWASP - SQL Injection](https://owasp.org/www-community/attacks/SQL_Injection)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 坏代码 ❌
|
|
31
|
+
|
|
32
|
+
### f-string 拼接
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
query = f"SELECT * FROM users WHERE id = {user_id}"
|
|
36
|
+
cursor.execute(query)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### format 方法
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
query = "SELECT * FROM users WHERE id = {}".format(user_id)
|
|
43
|
+
cursor.execute(query)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 好代码 ✅
|
|
49
|
+
|
|
50
|
+
### 参数化查询(推荐)
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### ORM 方式
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
User.objects.filter(id=user_id).first()
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 检查清单
|
|
65
|
+
|
|
66
|
+
- [ ] 是否有字符串拼接构建 SQL?
|
|
67
|
+
- 搜索 `+`、`format`、`f-string`、`%` 拼接 SQL 的地方
|
|
68
|
+
- [ ] 是否使用了参数化查询?
|
|
69
|
+
- 确认 `execute()` 的第二个参数传了元组/列表
|
|
70
|
+
- [ ] ORM 的 `raw()` / `execute()` 是否传了用户输入?
|
|
71
|
+
- 检查 Django ORM 的 `.raw()`、SQLAlchemy 的 `.execute()`
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 弱哈希算法
|
|
3
|
+
severity: 8
|
|
4
|
+
level: error
|
|
5
|
+
category: 安全漏洞
|
|
6
|
+
tags: [哈希, 密码, 加密]
|
|
7
|
+
languages: [python, java, javascript, go, php]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 问题描述
|
|
11
|
+
|
|
12
|
+
使用 MD5/SHA1 等不安全的哈希算法
|
|
13
|
+
|
|
14
|
+
## 为什么这是个问题
|
|
15
|
+
|
|
16
|
+
MD5 和 SHA1 已经被证明不安全,可以通过彩虹表或暴力破解。密码哈希必须使用带盐的慢哈希算法。
|
|
17
|
+
|
|
18
|
+
## 不修复的后果
|
|
19
|
+
|
|
20
|
+
- 密码被破解,用户账号被盗
|
|
21
|
+
- 数据库泄露后密码直接被还原
|
|
22
|
+
|
|
23
|
+
## 参考
|
|
24
|
+
|
|
25
|
+
- [OWASP - Password Storage](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 坏代码 ❌
|
|
30
|
+
|
|
31
|
+
### MD5 哈希密码
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import hashlib
|
|
35
|
+
password_hash = hashlib.md5(password.encode()).hexdigest()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 好代码 ✅
|
|
41
|
+
|
|
42
|
+
### bcrypt 带盐哈希
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import bcrypt
|
|
46
|
+
password_hash = bcrypt.hashpw(password, bcrypt.gensalt())
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 检查清单
|
|
52
|
+
|
|
53
|
+
- [ ] 是否使用了 `md5()` 或 `sha1()`?
|
|
54
|
+
- 搜索 `md5`、`sha1`、`hashlib.md5`
|
|
55
|
+
- [ ] 密码是否用了 `bcrypt`/`argon2`/`scrypt`?
|
|
56
|
+
- 确认是慢哈希算法,不是快速哈希
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: XSS 跨站脚本攻击
|
|
3
|
+
severity: 9
|
|
4
|
+
level: critical
|
|
5
|
+
category: 安全漏洞
|
|
6
|
+
tags: [XSS, HTML, 前端]
|
|
7
|
+
languages: [javascript, python, java, php]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 问题描述
|
|
11
|
+
|
|
12
|
+
用户输入未经转义直接输出到 HTML 页面
|
|
13
|
+
|
|
14
|
+
## 为什么这是个问题
|
|
15
|
+
|
|
16
|
+
攻击者可以注入恶意脚本,当其他用户浏览页面时执行,窃取 Cookie、伪造请求等。
|
|
17
|
+
|
|
18
|
+
## 不修复的后果
|
|
19
|
+
|
|
20
|
+
- 用户 Cookie 被盗,账号被劫持
|
|
21
|
+
- 页面内容被篡改
|
|
22
|
+
- 钓鱼攻击
|
|
23
|
+
|
|
24
|
+
## 参考
|
|
25
|
+
|
|
26
|
+
- [OWASP - XSS](https://owasp.org/www-community/attacks/xss/)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 坏代码 ❌
|
|
31
|
+
|
|
32
|
+
### innerHTML 插入用户输入
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
element.innerHTML = userInput;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 好代码 ✅
|
|
41
|
+
|
|
42
|
+
### textContent 代替 innerHTML
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
element.textContent = userInput;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 检查清单
|
|
51
|
+
|
|
52
|
+
- [ ] `innerHTML`、`document.write` 是否插入了用户输入?
|
|
53
|
+
- 搜索 `innerHTML =` 和 `document.write`
|
|
54
|
+
- [ ] 模板渲染是否使用了 `|safe` 或类似标记?
|
|
55
|
+
- 检查是否绕过自动转义
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
你是一位资深代码审核专家。请对以下代码变更进行严格审核。
|
|
2
|
+
|
|
3
|
+
## 审核维度(通用规则)
|
|
4
|
+
1. **Bug 检测**: 逻辑错误、空指针、边界条件、资源泄漏、并发问题等
|
|
5
|
+
2. **安全漏洞**: SQL注入、XSS、敏感信息泄露、硬编码密码、不安全的反序列化等
|
|
6
|
+
3. **代码风格**: 命名规范、代码格式、注释质量、代码组织
|
|
7
|
+
4. **性能问题**: 算法复杂度、内存泄漏、不必要的计算、大数据量处理
|
|
8
|
+
5. **最佳实践**: 设计模式、代码复用、错误处理、日志规范
|
|
9
|
+
6. **文档完整**: 函数文档、参数说明、返回值说明、复杂逻辑注释
|
|
10
|
+
|
|
11
|
+
## 🚨 空指针检测规则(避免误判)
|
|
12
|
+
|
|
13
|
+
**原则:不明确的不假设,明确的正常审。**
|
|
14
|
+
|
|
15
|
+
### 1. 来源不明确的参数 —— 不报
|
|
16
|
+
对于外部传入、上下文无法确定类型的变量(如函数参数 `row`、`me`、`options`):
|
|
17
|
+
- **视为合法传入的值**,不做 null/undefined/None 假设
|
|
18
|
+
- **不要**报"可能为空"、"缺少 null 检查"之类的问题
|
|
19
|
+
|
|
20
|
+
### 2. 以下明确情况 —— 正常审核并报
|
|
21
|
+
| 情况 | 示例 |
|
|
22
|
+
|------|------|
|
|
23
|
+
| 显式 null 赋值 | `let x = null`、`const y = undefined` |
|
|
24
|
+
| null 判断但未处理分支 | `if (x) { ... }` 但 else 分支仍使用 x |
|
|
25
|
+
| 调用链中已知可能返回 null | `obj.a.b.c` 其中 `obj.a` 可能为 null(代码中有相关判断或文档说明) |
|
|
26
|
+
| 可选链/空值合并使用不当 | 已用 `?.` 但仍直接访问属性等矛盾用法 |
|
|
27
|
+
| **函数调用时明显未传参** | `function b(a) {}` 被调用为 `b()`(a 明确为 undefined) |
|
|
28
|
+
|
|
29
|
+
以上情况**正常报问题**,不要放过。
|
|
30
|
+
|
|
31
|
+
### 3. 怎么区分"来源不明确"和"明确未传参"
|
|
32
|
+
- `b(x)` → x 来源不明确 → **不报**
|
|
33
|
+
- `b()` → 明显未传参,函数定义需要参数但没给 → **报**
|
|
34
|
+
|
|
35
|
+
## 严重级别定义
|
|
36
|
+
- **critical**: 必须修复,会导致系统崩溃或严重安全漏洞
|
|
37
|
+
- **error**: 应该修复,明确的 Bug 或安全问题
|
|
38
|
+
- **warning**: 建议修复,风格或最佳实践问题
|
|
39
|
+
- **info**: 仅供参考,轻微改进建议
|
|
40
|
+
|
|
41
|
+
## 代码信息
|
|
42
|
+
- 文件: {{filename}}
|
|
43
|
+
- 语言: {{language_display}}
|
|
44
|
+
- 变更类型: {{status}}
|
|
45
|
+
|
|
46
|
+
## 代码变更内容
|
|
47
|
+
```{{language}}
|
|
48
|
+
{{diff_content}}
|
|
49
|
+
```
|
|
50
|
+
{{cases_text}}
|
|
51
|
+
## 🚨 输出格式(不遵守会导致审核失败)
|
|
52
|
+
|
|
53
|
+
【必须】把 JSON 包裹在 <result></result> 标签中,除此之外不要有任何其他文字:
|
|
54
|
+
|
|
55
|
+
✅ 正确示例(无问题):
|
|
56
|
+
<result>{"summary":"总体评价(2-3句话)","passed":true,"issues":[]}</result>
|
|
57
|
+
|
|
58
|
+
✅ 正确示例(有问题):
|
|
59
|
+
<result>{"summary":"...","passed":false,"issues":[{"severity":"warning","category":"style","line_number":15,"message":"问题描述","suggestion":"修复建议","code_snippet":"相关代码"}]}</result>
|
|
60
|
+
|
|
61
|
+
❌ 错误示例(不要这样输出):
|
|
62
|
+
- 直接输出裸 JSON: {"passed":false,...}
|
|
63
|
+
- 用 ```json 包裹: ```json
|
|
64
|
+
{"passed":false,...}
|
|
65
|
+
```
|
|
66
|
+
- <result> 标签外还有解释文字
|
|
67
|
+
|
|
68
|
+
severity 只能是 critical/error/warning/info,category 只能是 bug/security/style/performance/best-practice/documentation
|
|
69
|
+
- 如无问题,issues 为空数组,passed 为 true
|
|
70
|
+
- line_number 为代码左侧标注的行号(如 " 145 | +const x = ..." 中的 145)
|
|
71
|
+
- 只关注本次变更引入的问题,不要审核已有代码
|
|
72
|
+
{{cases_note}}
|
|
73
|
+
- 尽量给出具体的修复建议,不要泛泛而谈
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
你是一位资深代码审核专家。请对以下完整代码文件进行全面审核。
|
|
2
|
+
|
|
3
|
+
## 审核维度(通用规则)
|
|
4
|
+
1. **Bug 检测**: 逻辑错误、空指针、边界条件、资源泄漏、并发问题等
|
|
5
|
+
2. **安全漏洞**: SQL注入、XSS、敏感信息泄露、硬编码密码、不安全的反序列化等
|
|
6
|
+
3. **代码风格**: 命名规范、代码格式、注释质量、代码组织
|
|
7
|
+
4. **性能问题**: 算法复杂度、内存泄漏、不必要的计算、大数据量处理
|
|
8
|
+
5. **最佳实践**: 设计模式、代码复用、错误处理、日志规范
|
|
9
|
+
6. **文档完整**: 函数文档、参数说明、返回值说明、复杂逻辑注释
|
|
10
|
+
|
|
11
|
+
## 🚨 空指针检测规则(避免误判)
|
|
12
|
+
|
|
13
|
+
**原则:不明确的不假设,明确的正常审。**
|
|
14
|
+
|
|
15
|
+
### 1. 来源不明确的参数 —— 不报
|
|
16
|
+
对于外部传入、上下文无法确定类型的变量(如函数参数 `row`、`me`、`options`):
|
|
17
|
+
- **视为合法传入的值**,不做 null/undefined/None 假设
|
|
18
|
+
- **不要**报"可能为空"、"缺少 null 检查"之类的问题
|
|
19
|
+
|
|
20
|
+
### 2. 以下明确情况 —— 正常审核并报
|
|
21
|
+
| 情况 | 示例 |
|
|
22
|
+
|------|------|
|
|
23
|
+
| 显式 null 赋值 | `let x = null`、`const y = undefined` |
|
|
24
|
+
| null 判断但未处理分支 | `if (x) { ... }` 但 else 分支仍使用 x |
|
|
25
|
+
| 调用链中已知可能返回 null | `obj.a.b.c` 其中 `obj.a` 可能为 null(代码中有相关判断或文档说明) |
|
|
26
|
+
| 可选链/空值合并使用不当 | 已用 `?.` 但仍直接访问属性等矛盾用法 |
|
|
27
|
+
| **函数调用时明显未传参** | `function b(a) {}` 被调用为 `b()`(a 明确为 undefined) |
|
|
28
|
+
|
|
29
|
+
以上情况**正常报问题**,不要放过。
|
|
30
|
+
|
|
31
|
+
### 3. 怎么区分"来源不明确"和"明确未传参"
|
|
32
|
+
- `b(x)` → x 来源不明确 → **不报**
|
|
33
|
+
- `b()` → 明显未传参,函数定义需要参数但没给 → **报**
|
|
34
|
+
|
|
35
|
+
## 严重级别定义
|
|
36
|
+
- **critical**: 必须修复,会导致系统崩溃或严重安全漏洞
|
|
37
|
+
- **error**: 应该修复,明确的 Bug 或安全问题
|
|
38
|
+
- **warning**: 建议修复,风格或最佳实践问题
|
|
39
|
+
- **info**: 仅供参考,轻微改进建议
|
|
40
|
+
|
|
41
|
+
## 代码信息
|
|
42
|
+
- 文件: {{filename}}
|
|
43
|
+
- 语言: {{language_display}}
|
|
44
|
+
- 总行数: {{line_count}}
|
|
45
|
+
{{truncation_note}}
|
|
46
|
+
|
|
47
|
+
## 完整代码内容
|
|
48
|
+
```{{language}}
|
|
49
|
+
{{content}}
|
|
50
|
+
```
|
|
51
|
+
{{cases_text}}
|
|
52
|
+
## 🚨 输出格式(不遵守会导致审核失败)
|
|
53
|
+
|
|
54
|
+
【必须】把 JSON 包裹在 <result></result> 标签中,除此之外不要有任何其他文字:
|
|
55
|
+
|
|
56
|
+
✅ 正确示例(无问题):
|
|
57
|
+
<result>{"summary":"总体评价(2-3句话)","passed":true,"issues":[]}</result>
|
|
58
|
+
|
|
59
|
+
✅ 正确示例(有问题):
|
|
60
|
+
<result>{"summary":"...","passed":false,"issues":[{"severity":"warning","category":"style","line_number":15,"message":"问题描述","suggestion":"修复建议","code_snippet":"相关代码"}]}</result>
|
|
61
|
+
|
|
62
|
+
❌ 错误示例(不要这样输出):
|
|
63
|
+
- 直接输出裸 JSON: {"passed":false,...}
|
|
64
|
+
- 用 ```json 包裹: ```json
|
|
65
|
+
{"passed":false,...}
|
|
66
|
+
```
|
|
67
|
+
- <result> 标签外还有解释文字
|
|
68
|
+
|
|
69
|
+
severity 只能是 critical/error/warning/info,category 只能是 bug/security/style/performance/best-practice/documentation
|
|
70
|
+
- 如无问题,issues 为空数组,passed 为 true
|
|
71
|
+
- line_number 为代码左侧标注的行号(如 "145 | const x = ..." 中的 145)
|
|
72
|
+
- 对整个文件进行全面审核,不限于变更部分
|
|
73
|
+
{{cases_note}}
|
|
74
|
+
- 尽量给出具体的修复建议,不要泛泛而谈
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
你是一位专业的代码审核专家,擅长发现代码中的问题并给出改进建议。
|
|
2
|
+
|
|
3
|
+
🚨🚨🚨 输出格式规则(违反会导致解析失败,审核结果作废)🚨🚨🚨
|
|
4
|
+
|
|
5
|
+
【规则1 - 最高优先级】最终输出必须且只能包含一对 <result> 标签,JSON 必须包裹其中:
|
|
6
|
+
✅ 正确: <result>{"summary":"...","passed":...,"issues":[...]}</result>
|
|
7
|
+
❌ 错误: {"summary":"...","passed":...} (缺少 <result> 标签)
|
|
8
|
+
❌ 错误: ```json {...}``` (用了代码块标记)
|
|
9
|
+
❌ 错误: <result>{...}</result> 之外还有任何其他文字 (有额外文字)
|
|
10
|
+
|
|
11
|
+
【规则2】可以先用 <think> 标签写思考过程,但 <think> 和 <result> 必须分开:
|
|
12
|
+
✅ 正确: <think>思考...</think>
|
|
13
|
+
<result>{...}</result>
|
|
14
|
+
❌ 错误: 把 JSON 放在 <think> 标签内
|
|
15
|
+
|
|
16
|
+
【规则3】不要添加任何解释、前言、总结——<result> 标签外除了 <think> 之外不要有任何文字。
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*.*.*" # 打 v1.2.3 标签时触发
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
name: Build distribution 📦
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
with:
|
|
15
|
+
persist-credentials: false
|
|
16
|
+
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.x"
|
|
20
|
+
|
|
21
|
+
- name: Install uv
|
|
22
|
+
uses: astral-sh/setup-uv@v4
|
|
23
|
+
|
|
24
|
+
- name: Build package
|
|
25
|
+
run: uv build
|
|
26
|
+
|
|
27
|
+
- name: Upload distributions
|
|
28
|
+
uses: actions/upload-artifact@v4
|
|
29
|
+
with:
|
|
30
|
+
name: dist
|
|
31
|
+
path: dist/
|
|
32
|
+
|
|
33
|
+
publish-to-pypi:
|
|
34
|
+
name: Publish to PyPI 🚀
|
|
35
|
+
needs: [build]
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
environment:
|
|
38
|
+
name: pypi
|
|
39
|
+
url: https://pypi.org/p/commit-ai-guardian
|
|
40
|
+
permissions:
|
|
41
|
+
id-token: write # Trusted Publishing (OIDC) 必须
|
|
42
|
+
steps:
|
|
43
|
+
- name: Download distributions
|
|
44
|
+
uses: actions/download-artifact@v4
|
|
45
|
+
with:
|
|
46
|
+
name: dist
|
|
47
|
+
path: dist/
|
|
48
|
+
|
|
49
|
+
- name: Publish to PyPI
|
|
50
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
|
|
23
|
+
# Virtual environments
|
|
24
|
+
.venv
|
|
25
|
+
venv/
|
|
26
|
+
ENV/
|
|
27
|
+
env/
|
|
28
|
+
|
|
29
|
+
# IDE
|
|
30
|
+
.idea/
|
|
31
|
+
.vscode/
|
|
32
|
+
*.swp
|
|
33
|
+
*.swo
|
|
34
|
+
*~
|
|
35
|
+
|
|
36
|
+
# Testing
|
|
37
|
+
.pytest_cache/
|
|
38
|
+
.coverage
|
|
39
|
+
htmlcov/
|
|
40
|
+
|
|
41
|
+
# OS
|
|
42
|
+
.DS_Store
|
|
43
|
+
Thumbs.db
|