Skip to content

Commit

Permalink
Use constant-time comparison to prevent timing attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bouk committed Jan 31, 2020
1 parent bde2d86 commit f6d0792
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nanoauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package nanoauth

import (
"crypto/subtle"
"crypto/tls"
"errors"
"net"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (self *Auth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
auth = req.FormValue(self.Header)
}

if auth != self.Token {
if subtle.ConstantTimeCompare([]byte(auth), []byte(self.Token)) == 0 {
rw.WriteHeader(http.StatusUnauthorized)
return
}
Expand Down

0 comments on commit f6d0792

Please sign in to comment.