From 6e5c06b98439f5370831fc9fa68abd00bedfa428 Mon Sep 17 00:00:00 2001 From: donald Date: Thu, 4 Dec 2025 00:46:02 +0800 Subject: [PATCH] Cleanup and dependency fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Removed redundant documentation files - Fixed Python dependencies for .venv environment - Upgraded packages for Python 3.13 compatibility - Updated Claude Code settings 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .claude/settings.local.json | 11 +- 安全修復完成報告.md | 233 ------------------------------------ 2 files changed, 10 insertions(+), 234 deletions(-) delete mode 100644 安全修復完成報告.md diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 2fb027c..fc201b7 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -16,7 +16,16 @@ "Bash(git init:*)", "Bash(git config:*)", "Bash(git remote add:*)", - "Bash(git add:*)" + "Bash(git add:*)", + "Bash(git commit:*)", + "Bash(git push:*)", + "Bash(where:*)", + "Bash(.venv\\Scripts\\python.exe:*)", + "Bash(\".venv/Scripts/python.exe\" -m pip install -r requirements.txt)", + "Bash(\".venv/Scripts/python.exe\" --version)", + "Bash(\".venv/Scripts/python.exe\" -m pip install --upgrade lxml beautifulsoup4 httpx fastapi uvicorn sqlalchemy pydantic pydantic-settings python-dotenv)", + "Bash(\".venv/Scripts/python.exe\" -m pip install pymysql cryptography alembic python-jose[cryptography] passlib[bcrypt] ldap3 google-generativeai openai apscheduler aiosmtplib weasyprint email-validator tenacity pytest pytest-asyncio black isort python-multipart)", + "Bash(\".venv/Scripts/python.exe\" run.py)" ], "deny": [], "ask": [] diff --git a/安全修復完成報告.md b/安全修復完成報告.md deleted file mode 100644 index d1cbfb9..0000000 --- a/安全修復完成報告.md +++ /dev/null @@ -1,233 +0,0 @@ -# 安全修復完成報告 - -> **修復日期:** 2025-01-27 -> **修復狀態:** ✅ 主要安全問題已修復 - ---- - -## ✅ 已完成的修復項目 - -### 🔴 立即修復(上線前必須)- 已完成 - -#### 1. ✅ 建立 `.gitignore` 檔案 -- **檔案:** `.gitignore` -- **狀態:** ✅ 已完成 -- **內容:** 排除所有敏感檔案(`.env`、`*.db`、`__pycache__/`、`*.log` 等) - -#### 2. ✅ 建立 `.env.example` 檔案 -- **檔案:** `.env.example` -- **狀態:** ⚠️ 檔案被 globalignore 阻擋,需要手動建立 -- **說明:** 請手動建立此檔案,內容請參考 `security-fixes.md` 第 154-214 行 - -#### 3. ✅ 修復硬編碼密鑰問題 -- **檔案:** `app/core/config.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 使用 `Field()` 標註預設值說明 - - 新增 `validate_secrets()` 函數驗證生產環境密鑰 - - 在 `main.py` 啟動時自動驗證 - -#### 4. ✅ 修復 Debug 模式問題 -- **檔案:** `app/core/config.py`, `app/main.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - `debug` 預設值改為 `False` - - 生產環境強制關閉 Debug - - API 文件在生產環境自動關閉 - -#### 5. ✅ 修正 CORS 設定 -- **檔案:** `app/core/config.py`, `app/main.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 新增 `cors_origins` 設定項 - - 生產環境禁止使用 `*` - - 限制允許的 HTTP 方法和標頭 - ---- - -### 🟡 高優先級修復 - 已完成 - -#### 6. ✅ 建立 logging 系統並替換所有 print() -- **檔案:** - - `app/core/logging_config.py` (新建) - - `app/main.py` - - `app/core/security.py` - - `app/services/notification_service.py` - - `app/services/crawler_service.py` - - `app/services/scheduler_service.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 建立完整的 logging 系統 - - 實作敏感資訊過濾器 - - 所有 `print()` 已替換為 `logger.info/warning/error()` - -#### 7. ✅ 修復 LDAP 注入風險 -- **檔案:** `app/core/security.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 使用 `escape_filter_chars()` 轉義用戶輸入 - - 防止 LDAP 注入攻擊 - -#### 8. ✅ 加強檔案上傳安全 -- **檔案:** `app/api/v1/endpoints/settings.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 檔案大小限制(5MB) - - 檢查檔案類型(content_type + 副檔名) - - 使用 Magic Number 驗證真實檔案類型 - - 使用 hash 產生安全檔案名稱 - - 使用絕對路徑,防止路徑遍歷 - - 驗證檔案路徑在允許目錄內 - -#### 9. ✅ Email XSS 防護 -- **檔案:** `app/services/notification_service.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 使用 `html.escape()` 轉義所有用戶輸入 - - 防止 Email 內容中的 XSS 攻擊 - -#### 10. ✅ 輸入驗證加強 -- **檔案:** `app/api/v1/endpoints/users.py` -- **狀態:** ✅ 已完成 -- **修改內容:** - - 搜尋輸入長度限制 - - 轉義 SQL 萬用字元(`%` 和 `_`) - ---- - -## 📋 待完成項目(建議後續處理) - -### 中優先級(建議上線後盡快修復) - -1. **速率限制(Rate Limiting)** - - 需要安裝 `slowapi` - - 在登入端點實作速率限制 - - 參考:`security-fixes.md` 第 755-788 行 - -2. **密碼強度檢查** - - 建立 `app/utils/password_validator.py` - - 在用戶建立/修改密碼時驗證強度 - - 參考:`security-fixes.md` 第 685-751 行 - -3. **依賴項安全掃描** - - 執行 `pip-audit -r requirements.txt` - - 修復已知漏洞 - - 參考:`security-fixes.md` 第 891-916 行 - -### 低優先級(持續改進) - -1. **AD 用戶自動建立審核機制** - - 參考:`security-fixes.md` 第 921-966 行 - -2. **報告發布時間驗證** - - 參考:`security-fixes.md` 第 970-1007 行 - ---- - -## 📝 修復檔案清單 - -### 新建檔案 -- ✅ `.gitignore` -- ✅ `app/core/logging_config.py` -- ✅ `ui-preview.html` (UI 預覽頁面) - -### 修改檔案 -- ✅ `app/core/config.py` - 密鑰驗證、Debug 設定、CORS 設定 -- ✅ `app/main.py` - Debug 檢查、CORS 設定、logging 初始化 -- ✅ `app/core/security.py` - LDAP 注入修復、logging -- ✅ `app/services/notification_service.py` - XSS 防護、logging -- ✅ `app/services/crawler_service.py` - logging -- ✅ `app/services/scheduler_service.py` - logging -- ✅ `app/api/v1/endpoints/settings.py` - 檔案上傳安全加強 -- ✅ `app/api/v1/endpoints/users.py` - 輸入驗證加強 - ---- - -## ⚠️ 重要提醒 - -### 必須手動完成的項目 - -1. **建立 `.env.example` 檔案** - - 由於檔案被 globalignore 阻擋,請手動建立 - - 內容請參考 `security-fixes.md` 或使用以下命令: - ```bash - # 複製並修改 - cp .env.example .env - # 然後填入實際的環境變數值 - ``` - -2. **產生強隨機密鑰** - ```python - import secrets - print(f"SECRET_KEY={secrets.token_urlsafe(32)}") - print(f"JWT_SECRET_KEY={secrets.token_urlsafe(32)}") - ``` - -3. **設定生產環境變數** - - 確保 `APP_ENV=production` - - 確保 `DEBUG=false` - - 設定強隨機的 `SECRET_KEY` 和 `JWT_SECRET_KEY` - - 設定正確的 `CORS_ORIGINS`(不能是 `*`) - -4. **檢查 Git 歷史** - ```bash - # 檢查是否有敏感檔案被提交 - git log --all --full-history -- .env - git log --all --full-history -- "*.db" - ``` - ---- - -## 🎨 UI 預覽 - -已建立 `ui-preview.html` 檔案,包含以下頁面預覽: - -1. **儀表板** - 統計資訊和待審核報告列表 -2. **報告管理** - 報告審核、編輯、發布功能 -3. **群組管理** - 群組和關鍵字管理 -4. **用戶管理** - 用戶列表和管理 -5. **系統設定** - LLM、PDF、SMTP 設定 - -**使用方式:** -- 直接在瀏覽器開啟 `ui-preview.html` -- 點擊上方標籤頁切換不同頁面 -- 所有資料為模擬資料,不連接資料庫 - ---- - -## ✅ 修復檢查清單 - -在部署到生產環境前,請確認: - -- [x] 已建立 `.gitignore` 並排除所有敏感檔案 -- [ ] 已手動建立 `.env.example` 檔案 -- [ ] 已從 Git 歷史中移除所有敏感資訊(如已提交) -- [ ] 生產環境的 `SECRET_KEY` 和 `JWT_SECRET_KEY` 已設定為強隨機值(至少 32 字元) -- [x] 生產環境的 `DEBUG=false`(已強制檢查) -- [x] 生產環境的 CORS 設定已明確指定允許的來源(不是 `*`) -- [x] 所有 `print()` 已替換為 `logging` -- [x] LDAP 查詢已使用 `escape_filter_chars` -- [x] 檔案上傳功能已加強安全檢查 -- [x] Email 內容已進行 XSS 防護 -- [x] 輸入驗證已加強 -- [ ] 已執行依賴項安全掃描並修復已知漏洞 -- [ ] 已進行滲透測試 - ---- - -## 📊 修復進度 - -- **已完成:** 10/10 個主要安全修復項目 -- **待完成:** 3 個中優先級項目(建議上線後處理) -- **總體進度:** 約 80% 完成 - ---- - -**修復完成時間:** 2025-01-27 - - - - - - -