Fix non-constant-time string != comparison
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -17,6 +17,7 @@ package securecookie
|
|||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -143,7 +144,7 @@ func Verify(signedValue, secret string) (string, error) {
|
|||||||
return "", fmt.Errorf("cannot sign value: %w", err)
|
return "", fmt.Errorf("cannot sign value: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if signedValue != expectedSignedValue {
|
if subtle.ConstantTimeCompare([]byte(signedValue), []byte(expectedSignedValue)) != 1 {
|
||||||
return "", ErrInvalidSignature
|
return "", ErrInvalidSignature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package securetoken
|
|||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -83,7 +84,7 @@ func Verify(signedValue, secret string) (string, error) {
|
|||||||
return "", fmt.Errorf("cannot sign value: %w", err)
|
return "", fmt.Errorf("cannot sign value: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if signedValue != expectedSignedValue {
|
if subtle.ConstantTimeCompare([]byte(signedValue), []byte(expectedSignedValue)) != 1 {
|
||||||
return "", ErrInvalidSignature
|
return "", ErrInvalidSignature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package statelesstoken
|
|||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -133,7 +134,7 @@ func ValidateToken[T any](secret string, tokenType string, tokenString string) (
|
|||||||
h.Write([]byte(encodedPayload))
|
h.Write([]byte(encodedPayload))
|
||||||
expectedSignature := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
|
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"}
|
return nil, &ErrInvalidToken{message: "invalid token signature"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user