fix: 全量代码审计修复 — 30项
Some checks failed
Deploy Backend / deploy (push) Has been cancelled

P0 - Panic 风险修复:
  - payment.rs: unwrap() → let-else safe handling
  - payment.rs: get_jwt_secret() expect → Result/AppError
  - auth.rs: openid 切片添加 len >= 8 守卫
  - main.rs: 启动时 expect → unwrap_or_else 描述性 panic
  - main.rs: Directive::from_str 添加 fallback

P1 - 逻辑/安全修复:
  - payment.rs: urlencoding() + 解码 bug 修复 (移除 had_escape)
  - payment.rs: Mock 支付添加 check_mock_payment_allowed 检查
  - db.rs: 永久会员 NULL → 2099-12-31 一致化
  - user.rs: 维护模式添加安全说明注释
  - 自动清理 unused_variables 警告 (_is_mobile)

P2 - 错误吞没修复:
  - main.rs: 3 处定时任务 let _ = → if let Err = tracing::error!
  - db.rs + admin.rs: 7 处通知/审计日志 let _ = → tracing::warn!
  - auth.rs: refresh token 保存 add warn 日志

P3 - 死代码清理:
  - models.rs: 移除 TokenResponse (dead)
  - models.rs: 移除 AppState 中 5 个未使用字段 (env var 直接读取)
  - error.rs: 移除 3 个 dead ErrorResponse 方法
  - rate_limiter.rs: extract_client_ip_from_header → #[cfg(test)]
  - models.rs: 注释 typo fix (user_ytpe → user_type)
  - db.rs: RefreshToken 添加 deserialization 注释

Shell 脚本修复:
  - deploy.sh: run_migrations 移到 restart_service 之前
  - test.sh: 移除 EXIT trap 覆盖; heredoc 引号修复; 维护模式添加 restart
  - common.sh: mock_key 添加 sed 转义 (防 / & 注入)

验证: cargo check 0 warnings, 8 tests passed
This commit is contained in:
2026-07-23 12:40:32 +08:00
parent 217e7f8a55
commit 2ce97243ab
12 changed files with 96 additions and 124 deletions

View File

@@ -155,7 +155,8 @@ deploy_service_file() {
local has_mock; has_mock=$(remote "grep -c 'MOCK_LOGIN_ENABLED' /etc/systemd/system/${SERVICE_NAME} 2>/dev/null || echo 0")
if [ "$has_mock" = "0" ]; then
local mock_key; mock_key=$(openssl rand -base64 12 2>/dev/null | tr -d '\n' || echo "dev-mock-$(date +%s)")
remote "sed -i '/^\[Service\]/a Environment=MOCK_LOGIN_ENABLED=true\nEnvironment=MOCK_LOGIN_KEY=${mock_key}' /etc/systemd/system/${SERVICE_NAME}"
local escaped_key; escaped_key=$(echo "$mock_key" | sed 's/[\/&]/\\&/g')
remote "sed -i '/^\[Service\]/a Environment=MOCK_LOGIN_ENABLED=true\nEnvironment=MOCK_LOGIN_KEY=${escaped_key}' /etc/systemd/system/${SERVICE_NAME}"
log_info "自动配置 MOCK_LOGIN_ENABLED=true, MOCK_LOGIN_KEY=${mock_key}"
fi
fi

View File

@@ -186,16 +186,6 @@ test_migration_status() {
test_payment_flow() {
log_info "开始支付链路测试..."
# 清理 trap测试失败时清理
local test_cleanup_done=false
cleanup_payment_test() {
if [ "$test_cleanup_done" = false ]; then
cleanup_test_user
test_cleanup_done=true
fi
}
trap cleanup_payment_test EXIT
# 阶段 1检查订单到期时间计算与套餐天数是否一致
# 通过 SQL 直接创建测试订单(避免 JWT 依赖),验证 order.expires_at 间隔
local uuid_prefix
@@ -319,7 +309,7 @@ test_payment_dedup() {
local all_ok=true
# 1. 在服务器上生成 SQL 文件(避免 stdin 管道问题)
remote "cat > ${sql_file} << 'SQLEOF'
remote "cat > ${sql_file} << SQLEOF
INSERT INTO payment_orders (user_id, order_no, package_type, amount, status, expires_at) VALUES (${user_id}, '${test_no1}', 'monthly', 590, 'pending', NOW() + INTERVAL '30 days');
INSERT INTO payment_orders (user_id, order_no, package_type, amount, status, expires_at) VALUES (${user_id}, '${test_no2}', 'monthly', 590, 'pending', NOW() + INTERVAL '30 days');
SQLEOF
@@ -357,7 +347,7 @@ test_payment_maintenance() {
# 启用维护模式
remote "sed -i '/^Environment=PAYMENT_MAINTENANCE_MODE/d' /etc/systemd/system/${SERVICE_NAME}
sed -i '/^\[Service\]/a Environment=PAYMENT_MAINTENANCE_MODE=true' /etc/systemd/system/${SERVICE_NAME}
systemctl daemon-reload && sleep 1" 2>/dev/null || true
systemctl daemon-reload && systemctl restart ${SERVICE_NAME} && sleep 3" 2>/dev/null || true
# 测试 quota 返回 unlimited
local quota_resp