基于 backend-code-audit 审计报告,修复剩余 P0/P1/P2 共 34 项问题: P0 生产 Bug: - 修复 time.Since(time.Now()) 计时逻辑错误 (P0-03) - generateRandomID 改用 crypto/rand 替代固定索引 (P0-04) - IncrementQuotaUsed 重写为 Ent 原子操作消除 TOCTOU 竞态 (P0-05) 安全加固: - gateway/openai handler 错误响应替换为泛化消息,防止内部信息泄露 (P1-14) - usage_log_repo dateFormat 参数改用白名单映射,防止 SQL 注入 (P1-16) - 默认配置安全加固:sslmode=prefer、response_headers=true、mode=release (P1-18/19, P2-15) 性能优化: - gateway handler 循环内 defer 替换为显式 releaseWait 闭包 (P1-02) - group_repo/promo_code_repo Count 前 Clone 查询避免状态污染 (P1-03) - usage_log_repo 四个查询添加 LIMIT 10000 防止 OOM (P1-07) - GetBatchUsageStats 添加时间范围参数,默认最近 30 天 (P1-10) - ip.go CIDR 预编译为包级变量 (P1-11) - BatchUpdateCredentials 重构为先验证后更新 (P1-13) 缓存一致性: - billing_cache 添加 jitteredTTL 防止缓存雪崩 (P2-10) - DeductUserBalance/UpdateSubscriptionUsage 错误传播修复 (P2-12) - UserService.UpdateBalance 成功后异步失效 billingCache (P2-13) 代码质量: - search 截断改为按 rune 处理,支持多字节字符 (P2-01) - TLS Handshake 改为 HandshakeContext 支持 context 取消 (P2-07) - CORS 预检添加 Access-Control-Max-Age: 86400 (P2-16) 测试覆盖: - 新增 user_service_test.go(UpdateBalance 缓存失效 6 个用例) - 新增 batch_update_credentials_test.go(fail-fast + 类型验证 7 个用例) - 新增 response_transformer_test.go、ip_test.go、usage_log_repo_unit_test.go、search_truncate_test.go - 集成测试:IncrementQuotaUsed 并发测试、billing_cache 错误传播测试 - config_test.go 补充 server.mode/sslmode 默认值断言 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
195 lines
5.8 KiB
Go
195 lines
5.8 KiB
Go
//go:build unit
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAdminAuthJWTValidatesTokenVersion(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
cfg := &config.Config{JWT: config.JWTConfig{Secret: "test-secret", ExpireHour: 1}}
|
|
authService := service.NewAuthService(nil, nil, nil, cfg, nil, nil, nil, nil, nil)
|
|
|
|
admin := &service.User{
|
|
ID: 1,
|
|
Email: "admin@example.com",
|
|
Role: service.RoleAdmin,
|
|
Status: service.StatusActive,
|
|
TokenVersion: 2,
|
|
Concurrency: 1,
|
|
}
|
|
|
|
userRepo := &stubUserRepo{
|
|
getByID: func(ctx context.Context, id int64) (*service.User, error) {
|
|
if id != admin.ID {
|
|
return nil, service.ErrUserNotFound
|
|
}
|
|
clone := *admin
|
|
return &clone, nil
|
|
},
|
|
}
|
|
userService := service.NewUserService(userRepo, nil, nil)
|
|
|
|
router := gin.New()
|
|
router.Use(gin.HandlerFunc(NewAdminAuthMiddleware(authService, userService, nil)))
|
|
router.GET("/t", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
})
|
|
|
|
t.Run("token_version_mismatch_rejected", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion - 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusUnauthorized, w.Code)
|
|
require.Contains(t, w.Body.String(), "TOKEN_REVOKED")
|
|
})
|
|
|
|
t.Run("token_version_match_allows", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
})
|
|
|
|
t.Run("websocket_token_version_mismatch_rejected", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion - 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Upgrade", "websocket")
|
|
req.Header.Set("Connection", "Upgrade")
|
|
req.Header.Set("Sec-WebSocket-Protocol", "sub2api-admin, jwt."+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusUnauthorized, w.Code)
|
|
require.Contains(t, w.Body.String(), "TOKEN_REVOKED")
|
|
})
|
|
|
|
t.Run("websocket_token_version_match_allows", func(t *testing.T) {
|
|
token, err := authService.GenerateToken(&service.User{
|
|
ID: admin.ID,
|
|
Email: admin.Email,
|
|
Role: admin.Role,
|
|
TokenVersion: admin.TokenVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/t", nil)
|
|
req.Header.Set("Upgrade", "websocket")
|
|
req.Header.Set("Connection", "Upgrade")
|
|
req.Header.Set("Sec-WebSocket-Protocol", "sub2api-admin, jwt."+token)
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
})
|
|
}
|
|
|
|
type stubUserRepo struct {
|
|
getByID func(ctx context.Context, id int64) (*service.User, error)
|
|
}
|
|
|
|
func (s *stubUserRepo) Create(ctx context.Context, user *service.User) error {
|
|
panic("unexpected Create call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetByID(ctx context.Context, id int64) (*service.User, error) {
|
|
if s.getByID == nil {
|
|
panic("GetByID not stubbed")
|
|
}
|
|
return s.getByID(ctx, id)
|
|
}
|
|
|
|
func (s *stubUserRepo) GetByEmail(ctx context.Context, email string) (*service.User, error) {
|
|
panic("unexpected GetByEmail call")
|
|
}
|
|
|
|
func (s *stubUserRepo) GetFirstAdmin(ctx context.Context) (*service.User, error) {
|
|
panic("unexpected GetFirstAdmin call")
|
|
}
|
|
|
|
func (s *stubUserRepo) Update(ctx context.Context, user *service.User) error {
|
|
panic("unexpected Update call")
|
|
}
|
|
|
|
func (s *stubUserRepo) Delete(ctx context.Context, id int64) error {
|
|
panic("unexpected Delete call")
|
|
}
|
|
|
|
func (s *stubUserRepo) List(ctx context.Context, params pagination.PaginationParams) ([]service.User, *pagination.PaginationResult, error) {
|
|
panic("unexpected List call")
|
|
}
|
|
|
|
func (s *stubUserRepo) ListWithFilters(ctx context.Context, params pagination.PaginationParams, filters service.UserListFilters) ([]service.User, *pagination.PaginationResult, error) {
|
|
panic("unexpected ListWithFilters call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateBalance(ctx context.Context, id int64, amount float64) error {
|
|
panic("unexpected UpdateBalance call")
|
|
}
|
|
|
|
func (s *stubUserRepo) DeductBalance(ctx context.Context, id int64, amount float64) error {
|
|
panic("unexpected DeductBalance call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateConcurrency(ctx context.Context, id int64, amount int) error {
|
|
panic("unexpected UpdateConcurrency call")
|
|
}
|
|
|
|
func (s *stubUserRepo) ExistsByEmail(ctx context.Context, email string) (bool, error) {
|
|
panic("unexpected ExistsByEmail call")
|
|
}
|
|
|
|
func (s *stubUserRepo) RemoveGroupFromAllowedGroups(ctx context.Context, groupID int64) (int64, error) {
|
|
panic("unexpected RemoveGroupFromAllowedGroups call")
|
|
}
|
|
|
|
func (s *stubUserRepo) UpdateTotpSecret(ctx context.Context, userID int64, encryptedSecret *string) error {
|
|
panic("unexpected UpdateTotpSecret call")
|
|
}
|
|
|
|
func (s *stubUserRepo) EnableTotp(ctx context.Context, userID int64) error {
|
|
panic("unexpected EnableTotp call")
|
|
}
|
|
|
|
func (s *stubUserRepo) DisableTotp(ctx context.Context, userID int64) error {
|
|
panic("unexpected DisableTotp call")
|
|
}
|