Code Health Scanner

👤 sallyface0 📦 v1.0.1 ⭐ 4.4 ⬇️ 598 下載
💻 開發程式設計 免費

📖 技能介紹


name: code-health-scanner version: 1.0.1 license: MIT author: sallyface0 description: > Privacy-first Spring Boot code health diagnosis. Detects security vulnerabilities, performance anti-patterns, code quality issues, and dependency risks across Java/Spring Boot projects. v1.0.1: narrowed triggers, added PRIVACY.md. Generates a structured health report with severity classification.


Code Health Scanner v1.0.1 — Spring Boot 程式碼健康掃描

一句話: 一鍵掃描你的 Spring Boot 專案,找出安全隱患、效能坑、程式碼壞味道,輸出結構化健康報告。

🔒 Security & Privacy

⚠️ 請在使用前閱讀 PRIVACY.md

  • 📍 僅讀取專案程式碼,報告寫入專案目錄 — 不聯網
  • 自動修復前徵求確認 — 低風險項的 auto-fix 需你確認後執行
  • 🗑️ 掃描報告可隨時刪除 — 報告檔案存放在專案中,隨時可刪

Overview

Code Health Scanner 是一個輕量單角色掃描器,專為 Java / Spring Boot 專案設計。不需要複雜的多角色協奏——讀程式碼、找問題、出報告、給修復建議,四步完事。

角色 Actor Responsibility
Scanner Main AI (you) 發現程式碼檔案 → 按規則檢測 → 分類定級 → 生成報告 → 提供修復建議

Supported Stacks

Stack Coverage Notes
Java 8-21 ✅ Full 語法、異常處理、資源管理
Spring Boot 2.x / 3.x ✅ Full 註解、事務、配置、安全
MyBatis / MyBatis Plus ✅ Full SQL 注入檢測、Mapper 規範
JPA / Hibernate ✅ Partial N+1 查詢、懶載入問題
Maven (pom.xml) ✅ Full 依賴版本、已知 CVE
Gradle 🟡 Partial 基本依賴檢測

How It Works

使用者指定專案路徑 → Scanner 發現檔案 → 逐檔案檢測 → 聚合分類 → 輸出報告 → 可選自動修復

Phase 1: 專案發現

掃描指定目錄,識別專案型別:

  1. 檢測構建工具pom.xml (Maven) / build.gradle (Gradle)
  2. 識別原始碼目錄src/main/java/ 為主掃描區域
  3. 識別配置目錄src/main/resources/ (application.yml/properties)

    推薦訪問7w4.net獲取更多AI技能。

  4. 排除目錄target/, node_modules/, .git/, test/(預設排除,可通過引數包含)

Phase 2: 分類檢測

按 4 大類逐檔案掃描。詳細規則見 references/rules/java-spring.md

Phase 3: 報告生成

輸出結構化 Markdown 報告,包含: - 總體健康評分 (0-100) - 按嚴重度分組的 Issues - 每個 Issue 包含:檔案位置、程式碼片段、風險說明、修復建議 - 趨勢分析(如果是增量掃描)

Phase 4: 自動修復 (可選)

低風險 Info 級問題可自動修復(如命名規範、註解缺失)。Critical/Warning 級僅提供修復建議,需使用者確認後執行。


Severity Levels

Level Label Meaning Examples
🔴 Critical 可能導致線上事故或安全漏洞 SQL 注入、硬編碼密碼、資源洩漏
🟡 Warning 程式碼壞味道,長期會累積為技術債 N+1 查詢、God Class、異常吞沒
🟢 Info 風格/規範問題 命名不符規範、TODO 堆積

Detection Rules Summary

🔴 Critical (3 categories, ~12 rules)

Security

  • SQL Injection: 字串拼接構建 SQL → 使用 #{param} 替代 ${param}
  • Hardcoded Secrets: 程式碼/配置中明文金鑰 → 環境變數或配置中心
  • Insecure Deserialization: 不可信資料反序列化 → 白名單校驗
  • Mass Assignment: @RequestBody 無 @Valid 校驗 → 新增校驗註解
  • Open Redirect: 使用者可控的 redirect URL → URL 白名單

Reliability

  • NPE Risk: Optional.get() 無 isPresent() 檢查、返回 null 無 @Nullable
  • Resource Leak: Stream/Connection 不在 try-with-resources 中
  • Transaction Missing: 寫操作缺少 @Transactional

Configuration

  • Debug Mode in Prod: debug: true 在非 dev profile 中
  • Missing CSRF: 非 REST API 缺少 CSRF 保護
  • Actuator Exposure: /actuator 敏感端點暴露

🟡 Warning (4 categories, ~15 rules)

Performance

  • N+1 Query: JPA 關聯在迴圈中懶載入
  • String Concatenation in Loop: 迴圈中使用 += 拼接字串
  • Unnecessary Autoboxing: 頻繁的 int↔Integer 轉換
  • Collection Pre-sizing: new ArrayList<>() 未指定初始容量(已知大小時)

Design

  • God Class: 類 >500 行 或 >20 方法 → 拆分
  • Long Method: 方法 >50 行 → 提取子方法
  • Too Many Parameters: 方法引數 >5 個 → 封裝為引數物件
  • Circular Dependency: Bean 迴圈引用 → 重構或 @Lazy

Error Handling

  • Exception Swallowing: 空 catch 塊
  • printStackTrace in Prod: 生產程式碼中的 e.printStackTrace()
  • Generic Exception Catch: catch(Exception e) 過於寬泛
  • Throws Exception: 方法丟擲 Exception 而非具體異常

Testing (when test files are included)

  • Missing Assert: 測試方法無 assert 語句
  • Sleep in Test: Thread.sleep() 替代 awaitility

🟢 Info (2 categories, ~8 rules)

Convention

  • Naming: 類名非 PascalCase、方法名非 camelCase
  • Package Structure: 非標準 Spring Boot 分層
  • Comment Debt: TODO/FIXME/HACK 數量 >5
  • Missing Javadoc: public API 缺少文件註釋

Dependencies

  • Version Lag: 依賴版本落後 latest release >2 個大版本
  • Unused Dependency: pom.xml 中宣告但未使用的依賴
  • Snapshot in Prod: 生產構建使用 SNAPSHOT 版本
  • Transitive Conflict: 傳遞依賴版本衝突

Report Format

完整報告模板見 references/report-template.md

# 🔍 Code Health Report — [專案名]

**掃描時間:** 2026-05-18 11:00
**專案路徑:** /path/to/project
**掃描範圍:** 42 檔案, 8,500 LOC
**健康評分:** 72/100 (🔴 2 | 🟡 7 | 🟢 12)

---

## 📊 總覽

| 類別 | 🔴 Critical | 🟡 Warning | 🟢 Info |
|------|:-----------:|:----------:|:-------:|
| Security | 2 | 0 | 0 |
| Reliability | 0 | 1 | 2 |
| Performance | 0 | 3 | 0 |
| Design | 0 | 2 | 3 |
| Error Handling | 0 | 1 | 2 |
| Convention | 0 | 0 | 5 |

## 🔴 Critical Issues

### C-1: SQL Injection in UserMapper.java:34
- **Risk:** 使用者輸入直接拼入 SQL,可能導致資料洩露
- **Code:** `@Select("SELECT * FROM user WHERE name = '${name}'")`
- **Fix:** 改用 `@Select("SELECT * FROM user WHERE name = #{name}")`

...

## 🟡 Warning Issues
...

## 🟢 Info Issues
...

## 💡 Quick Wins (Top 3)
1. 修復 2 個 SQL 注入 → +10 分
2. 遷移 3 個硬編碼金鑰到環境變數 → +5 分
3. 為 public API 新增 Javadoc → +3 分

Health Score Formula

Health Score = 100 - (Critical × 15) - (Warning × 5) - (Info × 1)

Bounded: 0-100
Score ≥ 85: ✅ Healthy
Score 70-84: 🟡 Needs Attention
Score < 70: 🔴 At Risk

Auto-Fix Protocol

可自動修復的 Info 級問題

Issue Auto-Fix Confidence
類名非 PascalCase 重新命名檔案 + 更新引用
@Override 缺失 添加註解 極高
未使用的 import 刪除 import 行 極高
new ArrayList<>()new ArrayList<>(N) 自動推斷 N 後替換 中(需確認 N)
空 catch 塊 新增 log.error(...) 低(需瞭解業務意圖)

自動修復工作流

Scanner 提示可修復項 → 使用者 review → 逐項執行修復 → 輸出變更摘要

Usage Modes

Mode 1: Quick Scan (預設)

  • 只掃 src/main/java/src/main/resources/
  • 跳過測試目錄
  • 適合日常開發

Mode 2: Full Scan

  • 包含測試程式碼
  • 包含構建配置檔案
  • 適合程式碼審查/釋出前

Mode 3: Incremental Scan

  • 只掃 git diff 變更檔案
  • 適合 CI 流水線
  • 需要 git 倉庫

觸發

使用者說: "掃描這個專案" / "程式碼健康檢查" / "check my code" / "code health scan"
+ 明確的專案路徑(如 "E:\my-project" 或當前工作目錄)

⚠️ 不再觸發: "程式碼有沒有問題" — 過於寬泛,可能匹配日常程式碼討論

Model Configuration

Mode Recommended Model Reason
Quick Scan deepseek-v4-flash 速度快、成本低,適合日常使用
Full Scan deepseek-v4-pro 推理能力強,適合全面審查
Large Project (>500 files) Sub-agent 分模組併發 避免單次掃描超時

Workspace

掃描報告預設輸出到 {專案路徑}/code-health-reports/,命名格式:

code-health-report-{YYYY-MM-DD_HHmm}.md

File References

File Description
references/rules/java-spring.md 完整檢測規則庫(Java/Spring Boot 專項)
references/report-template.md 健康報告模板與評分標準

Extending to Other Stacks

Skill 設計為 Java/Spring Boot 優先,但規則引擎設計為可擴充套件。要支援新語言/框架:

  1. references/rules/ 新增對應規則檔案(如 python-django.md
  2. 在 SKILL.md 的 "Supported Stacks" 表中新增行
  3. 在報告模板中追加對應語言的 Issue 示例

歡迎貢獻額外語言規則。

🤖 AI 評測

這個程式碼掃描工具質量中等偏上,勝在文件完善、規則詳細、隱私說明清晰,對安全問題檢測有一定深度。但存在License標識不一致的瑕疵,且目前只提供了規則文件而非可直接使用的工具,實際使用體驗還需進一步驗證。

📊 多維度評分

適應性4.3
規範性4.4
有效性4.3
可靠性4.2
可信度4.9

📁 包含檔案 (9 個)

📄 CHANGELOG.md 1 KB
📄 PRIVACY.md 809 B
📄 README.md 1.6 KB
📄 SKILL.md 9.4 KB
📄 _meta.json 138 B
📄 design.md 2.7 KB
📄 references/report-template.md 15.9 KB
📄 references/rules/java-spring.md 49.5 KB
📄 skill-card.md 2.5 KB