Files
user-system/.workbuddy/memory/2026-03-21.md

62 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 2026-03-21 工作日志
## 安全与质量优化
完成以下 10 个建议级问题的修复:
### 后端 (Go)
1. **SanitizeSQL/SanitizeXSS 方法** - `internal/security/validator.go`
- 使用正则表达式替代简单字符串替换
- 添加 SQL 注入模式检测UNION, DROP TABLE, xp_, sp_ 等)
- 添加 XSS 攻击模式检测script, iframe, event handlers 等)
2. **IP 验证正则** - `internal/security/validator.go`
- 使用 `net.ParseIP` 替代手动正则
- 支持所有 IPv6 格式(包括压缩格式如 ::1, fe80::1
- 添加 ValidateIPv4 和 ValidateIPv6 专用方法
3. **OAuth 用户名生成冲突** - `internal/service/auth.go`
- 添加 `sanitizeUsername` 函数处理昵称
- 添加 `generateUniqueUsername` 方法检查数据库唯一性
- 最多 100 次重试,添加数字后缀避免冲突
4. **LIKE 搜索特殊字符** - `internal/repository/user.go`
- 添加 `escapeLikePattern` 函数转义 % 和 _
- 修改 Search 和 AdvancedSearch 方法使用转义后的关键字
5. **权限检查 N+1 查询** - `internal/api/middleware/auth.go`
- 添加 `role.GetByIDs()` 批量查询方法
- 添加 `permission.GetByIDs()` 批量查询方法
- 添加 `rolePermission.GetPermissionIDsByRoleIDs()` 批量查询方法
- 重构 `loadUserRolesAndPerms` 方法使用批量查询
6. **JWT JTI 使用 math/rand** - `internal/auth/jwt.go`
- 改用 `crypto/rand` 生成 16 字节密码学安全随机数
- 添加降级处理以应对极端情况
### 前端 (React)
7. **HTTP 请求超时** - `frontend/admin/src/lib/http/client.ts`
- 添加 `DEFAULT_TIMEOUT = 30 * 1000` 常量
- 使用 `AbortController` 实现超时控制
- 添加超时错误处理和友好的错误消息
8. **App.tsx 模板代码** - `frontend/admin/src/`
- 删除 `src/App.tsx` Vite 模板文件
- 删除 `src/App.css` 未使用样式
9. **CSRF Token 保护** - `frontend/admin/src/lib/http/csrf.ts`
- 创建完整的 CSRF Token 管理模块
- 自动为 POST/PUT/DELETE/PATCH 请求添加 X-CSRF-Token 头
- 在 AuthProvider 中集成初始化和清除逻辑
## 测试验证
- ✅ Go 编译通过 (`go build ./...`)
- ✅ Go 代码检查通过 (`go vet ./...`)
- ✅ Go 单元测试全部通过 (100+ tests)
- ✅ 前端 TypeScript 编译通过
- ✅ 前端构建成功 (npm run build)
- ✅ 前端单元测试通过 (5 tests)