fix: use constant-time comparison for API key validation
This commit is contained in:
@@ -2,6 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/subtle"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -178,7 +179,7 @@ func Auth(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if t == nil || t.APIKey != apiKey {
|
if t == nil || subtle.ConstantTimeCompare([]byte(t.APIKey), []byte(apiKey)) != 1 {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
"error": "Unauthorized: invalid API key",
|
"error": "Unauthorized: invalid API key",
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user