diff --git a/cmd/anubis/main.go b/cmd/anubis/main.go index 3982e37..8c0327b 100644 --- a/cmd/anubis/main.go +++ b/cmd/anubis/main.go @@ -266,13 +266,10 @@ func metricsServer(ctx context.Context, done func()) { } } -func sha256sum(text string) (string, error) { +func sha256sum(text string) string { hash := sha256.New() - _, err := hash.Write([]byte(text)) - if err != nil { - return "", err - } - return hex.EncodeToString(hash.Sum(nil)), nil + hash.Write([]byte(text)) + return hex.EncodeToString(hash.Sum(nil)) } func (s *Server) challengeFor(r *http.Request, difficulty int) string { @@ -287,8 +284,7 @@ func (s *Server) challengeFor(r *http.Request, difficulty int) string { fp, difficulty, ) - result, _ := sha256sum(data) - return result + return sha256sum(data) } func New(target, policyFname string) (*Server, error) { @@ -522,13 +518,7 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request) { } calcString := fmt.Sprintf("%s%d", challenge, nonce) - calculated, err := sha256sum(calcString) - if err != nil { - lg.Error("failed to calculate sha256sum", "path", r.URL.Path, "err", err) - clearCookie(w) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + calculated := sha256sum(calcString) if subtle.ConstantTimeCompare([]byte(claims["response"].(string)), []byte(calculated)) != 1 { lg.Debug("invalid response", "path", r.URL.Path) @@ -635,13 +625,7 @@ func (s *Server) passChallenge(w http.ResponseWriter, r *http.Request) { } calcString := fmt.Sprintf("%s%d", challenge, nonce) - calculated, err := sha256sum(calcString) - if err != nil { - clearCookie(w) - lg.Debug("can't parse shasum", "err", err) - templ.Handler(base("Oh noes!", errorPage("failed to calculate sha256sum")), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r) - return - } + calculated := sha256sum(calcString) if subtle.ConstantTimeCompare([]byte(response), []byte(calculated)) != 1 { clearCookie(w) diff --git a/cmd/anubis/policy.go b/cmd/anubis/policy.go index 4e594fc..5de5e72 100644 --- a/cmd/anubis/policy.go +++ b/cmd/anubis/policy.go @@ -50,7 +50,7 @@ func (b Bot) Hash() (string, error) { userAgentRex = b.UserAgent.String() } - return sha256sum(fmt.Sprintf("%s::%s::%s", b.Name, pathRex, userAgentRex)) + return sha256sum(fmt.Sprintf("%s::%s::%s", b.Name, pathRex, userAgentRex)), nil } func parseConfig(fin io.Reader, fname string, defaultDifficulty int) (*ParsedConfig, error) {