Skip to content

Commit 68bed96

Browse files
tillfriedrichg
andauthored
Fix(auth): use crypto/subtle to compare strings (#39)
* Fix(auth): use crypto/subtle to compare strings Related: #37 Signed-off-by: till <[email protected]> * Remove empty line --------- Signed-off-by: till <[email protected]> Co-authored-by: Friedrich Gonzalez <[email protected]>
1 parent 6aa2583 commit 68bed96

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

gateway/middleware.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gateway
22

33
import (
4+
"crypto/subtle"
45
"net/http"
56

67
"github.com/cortexproject/auth-gateway/middleware"
@@ -53,14 +54,20 @@ func (tenant *Tenant) basicAuth(w http.ResponseWriter, r *http.Request) bool {
5354
return false
5455
}
5556

56-
if tenant.Username == username {
57-
if tenant.Password == password {
58-
r.Header.Set("X-Scope-OrgID", tenant.ID)
59-
return true
60-
} else {
61-
return false
62-
}
57+
if !tenant.saveCompare(username, password) {
58+
return false
6359
}
6460

61+
r.Header.Set("X-Scope-OrgID", tenant.ID)
62+
return true
63+
}
64+
65+
// attempt to mitigate timing attacks
66+
func (tenant *Tenant) saveCompare(username, password string) bool {
67+
userNameCheck := subtle.ConstantTimeCompare([]byte(tenant.Username), []byte(username))
68+
passwordCheck := subtle.ConstantTimeCompare([]byte(tenant.Password), []byte(password))
69+
if userNameCheck == 1 && passwordCheck == 1 {
70+
return true
71+
}
6572
return false
6673
}

0 commit comments

Comments
 (0)