From e3b493e3166cb4a2819a37094153f5df76d7eecc Mon Sep 17 00:00:00 2001 From: Mao Meng <1707647656@qq.com> Date: Sun, 19 Apr 2026 00:02:31 +0800 Subject: [PATCH] fix: use constant-time comparison for API key validation --- internal/middleware/auth.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go index e318b637..15c7fcec 100644 --- a/internal/middleware/auth.go +++ b/internal/middleware/auth.go @@ -2,6 +2,7 @@ package middleware import ( "context" + "crypto/subtle" "errors" "fmt" "log" @@ -178,7 +179,7 @@ func Auth( return } - if t == nil || t.APIKey != apiKey { + if t == nil || subtle.ConstantTimeCompare([]byte(t.APIKey), []byte(apiKey)) != 1 { c.JSON(http.StatusUnauthorized, gin.H{ "error": "Unauthorized: invalid API key", })