diff --git a/backend/internal/middleware/jwt/jwt.go b/backend/internal/middleware/jwt/jwt.go index 5640d47..83d58fc 100644 --- a/backend/internal/middleware/jwt/jwt.go +++ b/backend/internal/middleware/jwt/jwt.go @@ -34,6 +34,15 @@ func JWTAuth(accountRepo *account.AccountRepository, cache *rediscache.Client) g claims, err := auth.ParseToken(tokenString) if err != nil { + // 服务端未正确配置密钥:这是 500 级故障,必须打 FATAL 日志, + // 绝不能伪装成 401,否则会出现静默误诊。 + if errors.Is(err, auth.ErrSecretNotConfigured) { + log.Printf("FATAL: JWT secret not configured; %s %s rejected", + c.Request.Method, c.Request.URL.Path) + c.AbortWithStatusJSON(http.StatusInternalServerError, + gin.H{"error": "server auth misconfigured"}) + return + } c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"}) return } @@ -59,6 +68,15 @@ func SoftJWTAuth(accountRepo *account.AccountRepository, cache *rediscache.Clien claims, err := auth.ParseToken(tokenString) if err != nil { + // 服务端未正确配置密钥:这是 500 级故障,必须打 FATAL 日志, + // 绝不能伪装成 401,否则会出现静默误诊。 + if errors.Is(err, auth.ErrSecretNotConfigured) { + log.Printf("FATAL: JWT secret not configured; %s %s rejected", + c.Request.Method, c.Request.URL.Path) + c.AbortWithStatusJSON(http.StatusInternalServerError, + gin.H{"error": "server auth misconfigured"}) + return + } c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"}) return } diff --git a/backend/internal/worker/ssehub.go b/backend/internal/worker/ssehub.go index d5a8862..13dff55 100644 --- a/backend/internal/worker/ssehub.go +++ b/backend/internal/worker/ssehub.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" "sync" "time" @@ -90,6 +91,13 @@ func (h *SSEHub) SSERequireAuth() gin.HandlerFunc { } claims, err := auth.ParseToken(token) if err != nil { + // 与 middleware/jwt 保持一致:密钥未配置属服务端故障(500),不是客户端凭证问题。 + if errors.Is(err, auth.ErrSecretNotConfigured) { + log.Printf("FATAL: JWT secret not configured; SSE %s rejected", c.Request.URL.Path) + c.AbortWithStatusJSON(http.StatusInternalServerError, + gin.H{"error": "server auth misconfigured"}) + return + } c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"}) return }