@gulu9527/code-trust 0.2.0 → 0.3.0

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.
package/README-CN.md ADDED
@@ -0,0 +1,256 @@
1
+ # CodeTrust
2
+
3
+ > AI 代码信任验证工具 — 用确定性算法审查 AI 生成的代码,不用 LLM 审查 LLM。
4
+
5
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D20-brightgreen)](https://nodejs.org)
6
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
7
+ [![npm](https://img.shields.io/npm/v/@gulu9527/code-trust)](https://www.npmjs.com/package/@gulu9527/code-trust)
8
+
9
+ **[English](./README.md) | 中文**
10
+
11
+ ![CodeTrust 扫描示例](./docs/screenshot.png)
12
+
13
+ CodeTrust 是一个 **完全本地运行** 的 CLI 工具,专为验证 AI 生成代码(Cursor、Copilot、ChatGPT 等)的质量而设计。它不使用 LLM 来审查 LLM,而是通过 **确定性的静态分析算法** 检测 AI 代码中常见的幻觉模式和质量问题。
14
+
15
+ ## 特性
16
+
17
+ - **幻觉检测** — 幻影导入、未使用导入、缺失 await、不必要的 try-catch、过度防御性编码、死逻辑分支
18
+ - **安全扫描** — 硬编码密钥、eval 使用、SQL 注入、XSS 漏洞检测
19
+ - **结构分析** — 圈复杂度、认知复杂度、函数长度、嵌套深度、参数数量
20
+ - **风格一致性** — 命名风格混用检测(camelCase / snake_case)
21
+ - **覆盖分析** — 检测文件是否有对应的测试文件
22
+ - **自动修复** — `codetrust fix` 自动修复安全问题(未使用导入、debugger、宽松等于、未使用变量),支持预演模式
23
+ - **五维度评分** — 安全、逻辑、结构、风格、覆盖,加权计算信任总分
24
+ - **完全本地** — 代码不上云,零外部请求
25
+ - **双语支持** — 中文/英文自动切换
26
+
27
+ ## 安装
28
+
29
+ ```bash
30
+ npm install -g @gulu9527/code-trust
31
+ ```
32
+
33
+ 安装后可以使用 `code-trust` 或 `codetrust` 命令(两个别名均可用)。
34
+
35
+ ## 快速开始
36
+
37
+ ```bash
38
+ # 初始化配置文件
39
+ codetrust init
40
+
41
+ # 扫描 git staged 文件
42
+ codetrust scan --staged
43
+
44
+ # 扫描与 main 分支的差异
45
+ codetrust scan --diff origin/main
46
+
47
+ # 扫描指定文件
48
+ codetrust scan src/foo.ts src/bar.ts
49
+
50
+ # JSON 输出(用于 CI/CD)
51
+ codetrust scan --staged --format json
52
+
53
+ # 设置最低分数门槛(低于此分数则退出码为 1)
54
+ codetrust scan --staged --min-score 70
55
+
56
+ # 查看所有规则
57
+ codetrust rules list
58
+
59
+ # 安装 pre-commit hook
60
+ codetrust hook install
61
+
62
+ # 自动修复问题(默认预演模式)
63
+ codetrust fix src/
64
+
65
+ # 应用修复
66
+ codetrust fix src/ --apply
67
+
68
+ # 仅修复指定规则
69
+ codetrust fix src/ --apply --rule logic/type-coercion
70
+ ```
71
+
72
+ ## 信任评分
73
+
74
+ CodeTrust 从五个维度评估代码,加权计算总分(0-100):
75
+
76
+ | 维度 | 权重 | 说明 |
77
+ |------|------|------|
78
+ | 安全 | 30% | 硬编码密钥、eval、SQL 注入、XSS |
79
+ | 逻辑 | 25% | 幻觉检测:死逻辑、未使用变量、重复条件 |
80
+ | 结构 | 20% | 复杂度、函数长度、嵌套深度 |
81
+ | 覆盖 | 15% | 测试文件覆盖 |
82
+ | 风格 | 10% | 命名一致性 |
83
+
84
+ ### 等级
85
+
86
+ | 分数 | 等级 | 含义 |
87
+ |------|------|------|
88
+ | >= 90 | ✅ 高信任 | 可安全合并 |
89
+ | >= 70 | ⚠️ 建议审查 | 建议审查后合并 |
90
+ | >= 50 | ⚠️ 低信任 | 需仔细审查 |
91
+ | < 50 | ❌ 不可信 | 不应合并 |
92
+
93
+ ## 内置规则(29 条)
94
+
95
+ ### 幻觉检测(逻辑维度)
96
+ | 规则 ID | 严重度 | 说明 |
97
+ |---------|--------|------|
98
+ | `logic/phantom-import` | high | 导入不存在的相对路径模块(AI 幻觉) |
99
+ | `logic/missing-await` | medium | async 函数调用缺少 await |
100
+ | `logic/any-type-abuse` | medium | 滥用 `any` 类型绕过类型检查 |
101
+ | `logic/type-coercion` | medium | 宽松等于(`==`)导致隐式类型转换 |
102
+ | `logic/no-nested-ternary` | medium | 嵌套三元表达式降低可读性 |
103
+ | `logic/unnecessary-try-catch` | medium | AI 常生成包裹简单语句的 try-catch |
104
+ | `logic/dead-branch` | medium | 始终 true/false 的条件、不可达代码 |
105
+ | `logic/duplicate-condition` | medium | if-else 链中重复的条件 |
106
+ | `logic/empty-catch` | medium | 空 catch 块或仅原样 re-throw |
107
+ | `logic/identical-branches` | medium | if/else 两个分支代码完全一样 |
108
+ | `logic/no-non-null-assertion` | medium | 非空断言 (!) 可能导致运行时崩溃 |
109
+ | `logic/no-self-compare` | medium | 自比较 (x === x) 始终为 true/false |
110
+ | `logic/no-return-assign` | medium | return 语句中使用赋值 (=),可能误用了 === |
111
+ | `logic/promise-void` | medium | 浮动 Promise — async 调用未 await 或 return |
112
+ | `logic/unused-import` | low | 导入了但从未使用的模块 |
113
+ | `logic/over-defensive` | low | 过度的 null/undefined 守卫 |
114
+ | `logic/unused-variables` | low | 声明但未使用的变量 |
115
+ | `logic/redundant-else` | low | return/throw 后不必要的 else |
116
+ | `logic/magic-number` | low | 未解释的魔术数字,应提取为命名常量 |
117
+ | `logic/duplicate-string` | low | 相同字符串字面量重复出现 3 次以上 |
118
+ | `logic/no-reassign-param` | low | 重新赋值函数参数 |
119
+ | `logic/no-async-without-await` | low | async 函数内部未使用 await |
120
+ | `logic/no-useless-constructor` | low | 空构造函数或仅调用 super() 的构造函数 |
121
+ | `logic/console-in-code` | info | 遗留的 console.log 调试语句 |
122
+
123
+ ### 安全规则
124
+ | 规则 ID | 严重度 | 说明 |
125
+ |---------|--------|------|
126
+ | `security/hardcoded-secret` | high | 硬编码的 API 密钥、密码、token |
127
+ | `security/eval-usage` | high | eval()、new Function() 等危险调用 |
128
+ | `security/sql-injection` | high | SQL 查询中的字符串拼接 |
129
+ | `security/no-debugger` | high | 遗留的 debugger 语句 |
130
+ | `security/dangerous-html` | medium | innerHTML / dangerouslySetInnerHTML |
131
+
132
+ ## 自动修复
133
+
134
+ `codetrust fix` 可以自动修复特定的安全问题。默认以 **预演模式** 运行 — 不会修改任何文件,直到你传入 `--apply`。
135
+
136
+ ### 可修复规则
137
+
138
+ | 规则 ID | 修复动作 |
139
+ |---------|----------|
140
+ | `security/no-debugger` | 删除 `debugger` 所在行 |
141
+ | `logic/unused-import` | 删除未使用的导入行 |
142
+ | `logic/type-coercion` | 将 `==` 替换为 `===`,`!=` 替换为 `!==` |
143
+ | `logic/unused-variables` | 删除未使用的变量声明 |
144
+
145
+ ```bash
146
+ # 预览修复(预演模式,不修改文件)
147
+ codetrust fix src/
148
+
149
+ # 应用修复
150
+ codetrust fix src/ --apply
151
+
152
+ # 仅修复指定规则
153
+ codetrust fix src/ --apply --rule logic/type-coercion
154
+ ```
155
+
156
+ ## 配置
157
+
158
+ 运行 `codetrust init` 生成 `.codetrust.yml`:
159
+
160
+ ```yaml
161
+ version: 1
162
+
163
+ include:
164
+ - "src/**/*.ts"
165
+ - "src/**/*.js"
166
+ exclude:
167
+ - "**/*.test.ts"
168
+ - "**/node_modules/**"
169
+
170
+ weights:
171
+ security: 0.30
172
+ logic: 0.25
173
+ structure: 0.20
174
+ style: 0.10
175
+ coverage: 0.15
176
+
177
+ thresholds:
178
+ min-score: 70
179
+ max-function-length: 40
180
+ max-cyclomatic-complexity: 10
181
+ max-cognitive-complexity: 20
182
+ max-nesting-depth: 4
183
+ max-params: 5
184
+
185
+ rules:
186
+ disabled: []
187
+ overrides: {}
188
+ ```
189
+
190
+ ## CI/CD 集成
191
+
192
+ ### GitHub Action(可复用)
193
+
194
+ ```yaml
195
+ name: CodeTrust
196
+ on:
197
+ pull_request:
198
+ branches: [main]
199
+
200
+ jobs:
201
+ trust-scan:
202
+ runs-on: ubuntu-latest
203
+ steps:
204
+ - uses: actions/checkout@v4
205
+ with:
206
+ fetch-depth: 0
207
+ - uses: GuLu9527/CodeTrust@main
208
+ with:
209
+ min-score: 70
210
+ ```
211
+
212
+ 或手动安装:
213
+
214
+ ```yaml
215
+ - uses: actions/setup-node@v4
216
+ with:
217
+ node-version: '20'
218
+ - run: npm install -g @gulu9527/code-trust
219
+ - run: codetrust scan --diff origin/main --min-score 70
220
+ ```
221
+
222
+ ### Git Pre-commit Hook
223
+
224
+ ```bash
225
+ codetrust hook install
226
+ ```
227
+
228
+ 安装后每次 `git commit` 会自动运行 CodeTrust 扫描。使用 `git commit --no-verify` 跳过。
229
+
230
+ ## 语言切换
231
+
232
+ CodeTrust 自动检测系统语言。手动切换:
233
+
234
+ ```bash
235
+ # 强制中文
236
+ CODETRUST_LANG=zh codetrust scan --staged
237
+
238
+ # 强制英文
239
+ CODETRUST_LANG=en codetrust scan --staged
240
+ ```
241
+
242
+ ## 技术栈
243
+
244
+ - **语言**: TypeScript 5.x
245
+ - **运行时**: Node.js 20+
246
+ - **AST 解析**: @typescript-eslint/typescript-estree
247
+ - **CLI**: Commander.js
248
+ - **Git**: simple-git
249
+ - **终端 UI**: picocolors + cli-table3
250
+ - **配置**: cosmiconfig
251
+ - **测试**: Vitest
252
+ - **构建**: tsup
253
+
254
+ ## License
255
+
256
+ [Apache-2.0](LICENSE)
package/README.md CHANGED
@@ -15,10 +15,11 @@ CodeTrust is a **fully local** CLI tool designed to verify the quality of AI-gen
15
15
  ## Features
16
16
 
17
17
  - **Hallucination Detection** — Phantom imports, unused imports, missing `await`, unnecessary try-catch, over-defensive coding, dead logic branches
18
- - **Security Scanning** — Hardcoded secrets, eval usage, SQL injection, XSS vulnerabilities
18
+ - **Security Scanning** — Hardcoded secrets, eval usage, query-like SQL injection, XSS vulnerabilities
19
19
  - **Structure Analysis** — Cyclomatic/cognitive complexity, function length, nesting depth, parameter count
20
20
  - **Style Consistency** — Mixed naming convention detection (camelCase / snake_case)
21
21
  - **Coverage Analysis** — Detect files missing corresponding test files
22
+ - **Auto-fix** — `codetrust fix` automatically fixes safe issues (unused imports, debugger, loose equality, unused variables) with dry-run preview
22
23
  - **Five-Dimension Scoring** — Security, Logic, Structure, Style, Coverage, weighted into a trust score (0-100)
23
24
  - **Fully Local** — No cloud uploads, zero external requests
24
25
  - **Bilingual** — Automatic Chinese/English output based on system locale
@@ -57,6 +58,15 @@ codetrust rules list
57
58
 
58
59
  # Install pre-commit hook
59
60
  codetrust hook install
61
+
62
+ # Auto-fix issues (dry-run by default)
63
+ codetrust fix src/
64
+
65
+ # Apply fixes
66
+ codetrust fix src/ --apply
67
+
68
+ # Fix only a specific rule
69
+ codetrust fix src/ --apply --rule logic/type-coercion
60
70
  ```
61
71
 
62
72
  ## Trust Score
@@ -114,11 +124,35 @@ CodeTrust evaluates code across five dimensions, weighted into a total score (0-
114
124
  | Rule ID | Severity | Description |
115
125
  |---------|----------|-------------|
116
126
  | `security/hardcoded-secret` | high | Hardcoded API keys, passwords, tokens |
117
- | `security/eval-usage` | high | eval(), new Function() and similar |
118
- | `security/sql-injection` | high | String concatenation in SQL queries |
127
+ | `security/eval-usage` | high | Executable eval(), new Function() and string-based timers; ignores regex/pattern definitions and plain string mentions |
128
+ | `security/sql-injection` | high | Interpolation or concatenation in query-like SQL construction/execution contexts |
119
129
  | `security/no-debugger` | high | Debugger statements left in code |
120
130
  | `security/dangerous-html` | medium | innerHTML / dangerouslySetInnerHTML |
121
131
 
132
+ ## Auto-fix
133
+
134
+ `codetrust fix` can automatically fix certain safe issues. It runs in **dry-run mode by default** — no files are modified until you pass `--apply`.
135
+
136
+ ### Fixable Rules
137
+
138
+ | Rule ID | Fix Action |
139
+ |---------|------------|
140
+ | `security/no-debugger` | Delete the `debugger` line |
141
+ | `logic/unused-import` | Delete the unused import line |
142
+ | `logic/type-coercion` | Replace `==` with `===`, `!=` with `!==` |
143
+ | `logic/unused-variables` | Delete the unused variable declaration |
144
+
145
+ ```bash
146
+ # Preview fixes (dry-run, no file changes)
147
+ codetrust fix src/
148
+
149
+ # Apply fixes to files
150
+ codetrust fix src/ --apply
151
+
152
+ # Fix only a specific rule
153
+ codetrust fix src/ --apply --rule logic/type-coercion
154
+ ```
155
+
122
156
  ## Configuration
123
157
 
124
158
  Run `codetrust init` to generate `.codetrust.yml`:
@@ -153,9 +187,14 @@ rules:
153
187
  overrides: {}
154
188
  ```
155
189
 
190
+ ## Detection Notes
191
+
192
+ - `security/eval-usage` is intentionally scoped to executable usage. It still flags `eval(...)`, `new Function(...)`, and string-based `setTimeout` / `setInterval`, but it avoids false positives from detector metadata such as `pattern: /.../` and from plain string literals that merely mention `eval(`.
193
+ - `security/sql-injection` requires both SQL keywords and query-like context such as `query`, `sql`, `statement`, `stmt`, or calls like `.query(...)` / `.execute(...)`. This keeps real query construction findings while reducing noise from non-query metadata or fingerprint assembly.
194
+
156
195
  ## CI/CD Integration
157
196
 
158
- ### GitHub Action
197
+ ### GitHub Action (Reusable)
159
198
 
160
199
  ```yaml
161
200
  name: CodeTrust
@@ -170,6 +209,14 @@ jobs:
170
209
  - uses: actions/checkout@v4
171
210
  with:
172
211
  fetch-depth: 0
212
+ - uses: GuLu9527/CodeTrust@main
213
+ with:
214
+ min-score: 70
215
+ ```
216
+
217
+ Or install manually:
218
+
219
+ ```yaml
173
220
  - uses: actions/setup-node@v4
174
221
  with:
175
222
  node-version: '20'