From 685e9d2e69cbb1c7780f63310232f1ae2b71b96d Mon Sep 17 00:00:00 2001 From: Bryan Frimin Date: Sun, 19 Apr 2026 15:00:32 +0200 Subject: [PATCH] Fix non-constant-time string != comparison Signed-off-by: Bryan Frimin --- pkg/securecookie/securecookie.go | 3 ++- pkg/securetoken/securetoken.go | 3 ++- pkg/statelesstoken/statelesstoken.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/securecookie/securecookie.go b/pkg/securecookie/securecookie.go index 6704f162f..1491bbbb6 100644 --- a/pkg/securecookie/securecookie.go +++ b/pkg/securecookie/securecookie.go @@ -17,6 +17,7 @@ package securecookie import ( "crypto/hmac" "crypto/sha256" + "crypto/subtle" "encoding/base64" "errors" "fmt" @@ -143,7 +144,7 @@ func Verify(signedValue, secret string) (string, error) { return "", fmt.Errorf("cannot sign value: %w", err) } - if signedValue != expectedSignedValue { + if subtle.ConstantTimeCompare([]byte(signedValue), []byte(expectedSignedValue)) != 1 { return "", ErrInvalidSignature } diff --git a/pkg/securetoken/securetoken.go b/pkg/securetoken/securetoken.go index 14f5b20ab..ddfea0cc2 100644 --- a/pkg/securetoken/securetoken.go +++ b/pkg/securetoken/securetoken.go @@ -17,6 +17,7 @@ package securetoken import ( "crypto/hmac" "crypto/sha256" + "crypto/subtle" "encoding/base64" "errors" "fmt" @@ -83,7 +84,7 @@ func Verify(signedValue, secret string) (string, error) { return "", fmt.Errorf("cannot sign value: %w", err) } - if signedValue != expectedSignedValue { + if subtle.ConstantTimeCompare([]byte(signedValue), []byte(expectedSignedValue)) != 1 { return "", ErrInvalidSignature } diff --git a/pkg/statelesstoken/statelesstoken.go b/pkg/statelesstoken/statelesstoken.go index e7ae2a749..15f7802fc 100644 --- a/pkg/statelesstoken/statelesstoken.go +++ b/pkg/statelesstoken/statelesstoken.go @@ -17,6 +17,7 @@ package statelesstoken import ( "crypto/hmac" "crypto/sha256" + "crypto/subtle" "encoding/base64" "encoding/json" "fmt" @@ -133,7 +134,7 @@ func ValidateToken[T any](secret string, tokenType string, tokenString string) ( h.Write([]byte(encodedPayload)) expectedSignature := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) - if providedSignature != expectedSignature { + if subtle.ConstantTimeCompare([]byte(providedSignature), []byte(expectedSignature)) != 1 { return nil, &ErrInvalidToken{message: "invalid token signature"} }