fix: 隐藏内部错误信息,防止敏感数据泄露

- 移除 ErrorResponse.errmsg 中的 e.to_string() 调用
- 将详细的内部错误信息改为 None,用户看不到内部详情
- 错误详情仍然记录到 JSON 日志,供开发者排查
- 健康检查端点也使用友好的错误信息
- 更新 IMPROVEMENTS.md 标记 1.3 完成

影响范围:
- src/main.rs: login handler (2处)
- src/main.rs: health_check handler (1处)
This commit is contained in:
2026-04-15 11:11:16 +08:00
parent 6b871ca4d6
commit e8990f7771
2 changed files with 8 additions and 5 deletions

View File

@@ -33,15 +33,18 @@
``` ```
### 1.3 错误处理改进 ### 1.3 错误处理改进
- [ ] **错误响应格式不统一** - [x] **错误响应格式不统一**
- 现状:部分返回 `ErrorResponse`,部分返回 JSON 字符串 - 现状:部分返回 `ErrorResponse`,部分返回 JSON 字符串
- 改进:统一使用 `ErrorResponse` 结构体 - 改进:统一使用 `ErrorResponse` 结构体
- 彰响:前端解析更一致 - 彰响:前端解析更一致
- 完成时间2026-04-15
- [ ] **错误信息暴露过多** - [x] **错误信息暴露过多**
- 现状:部分错误直接返回数据库错误信息 - 现状:部分错误直接返回数据库错误信息
- 改进:区分用户友好错误和开发者错误 - 改进:区分用户友好错误和开发者错误
- 影响:安全性提升 - 影响:安全性提升
- 完成时间2026-04-15
- 修改:移除所有 `errmsg: Some(e.to_string())`,错误详情只记录到日志
## 二、安全性改进 ## 二、安全性改进

View File

@@ -150,7 +150,7 @@ async fn login(
return HttpResponse::InternalServerError().json(ErrorResponse { return HttpResponse::InternalServerError().json(ErrorResponse {
error: "用户信息处理失败".to_string(), error: "用户信息处理失败".to_string(),
errcode: Some(500), errcode: Some(500),
errmsg: Some(e.to_string()), errmsg: None,
}); });
} }
}; };
@@ -165,7 +165,7 @@ async fn login(
return HttpResponse::InternalServerError().json(ErrorResponse { return HttpResponse::InternalServerError().json(ErrorResponse {
error: "生成身份令牌失败".to_string(), error: "生成身份令牌失败".to_string(),
errcode: Some(500), errcode: Some(500),
errmsg: Some(e.to_string()), errmsg: None,
}); });
} }
}; };
@@ -694,7 +694,7 @@ async fn health_check(pool: web::Data<PgPool>) -> impl Responder {
HttpResponse::ServiceUnavailable().json(serde_json::json!({ HttpResponse::ServiceUnavailable().json(serde_json::json!({
"status": "unhealthy", "status": "unhealthy",
"database": "disconnected", "database": "disconnected",
"error": e.to_string() "error": "数据库连接失败"
})) }))
} }
} }