(cherry-pick) Use subtle.ConstantTimeCompare instead of compare directly (#18710)

Use subtle.ConstantTimeCompare instead of compare directly

Signed-off-by: stonezdj <daojunz@vmware.com>
This commit is contained in:
stonezdj(Daojun Zhang) 2023-05-23 21:02:25 +08:00 committed by GitHub
parent 1e616ad393
commit 31547ec593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 2 deletions

View File

@ -15,6 +15,7 @@
package api package api
import ( import (
"crypto/subtle"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -66,7 +67,7 @@ func (sa *SecretAuthenticator) DoAuth(req *http.Request) error {
} }
expectedSecret := config.GetUIAuthSecret() expectedSecret := config.GetUIAuthSecret()
if expectedSecret != secret { if subtle.ConstantTimeCompare([]byte(expectedSecret), []byte(secret)) == 0 {
return errors.New("unauthorized") return errors.New("unauthorized")
} }

View File

@ -15,6 +15,7 @@
package auth package auth
import ( import (
"crypto/subtle"
"errors" "errors"
"net/http" "net/http"
"strings" "strings"
@ -54,7 +55,7 @@ func (s *secretHandler) AuthorizeRequest(req *http.Request) error {
secInReq := strings.TrimPrefix(auth, HarborSecret) secInReq := strings.TrimPrefix(auth, HarborSecret)
for _, v := range s.secrets { for _, v := range s.secrets {
if secInReq == v { if subtle.ConstantTimeCompare([]byte(secInReq), []byte(v)) == 1 {
return nil return nil
} }
} }