aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Yastrebov <yastrebov.alex@gmail.com>2025-03-21 20:46:43 +0100
committerGitHub <noreply@github.com>2025-03-21 15:46:43 -0400
commit194e55088bfd90130f3044c4e3fb5b0f0b8ec2ed (patch)
tree38f734f5608842aba503f2d120f62cfb2f3169f1
parent4ec4dc3624a9115cb767bb21809059f8052b5763 (diff)
downloadanubis-194e55088bfd90130f3044c4e3fb5b0f0b8ec2ed.tar.xz
anubis-194e55088bfd90130f3044c4e3fb5b0f0b8ec2ed.zip
cmd/anubis: do not return error from sha256 (#57)
hash.Write never returns error so removing it from the results simplifies usage and eliminates dead error handling. Signed-off-by: Alexander Yastrebov <yastrebov.alex@gmail.com>
-rw-r--r--cmd/anubis/main.go28
-rw-r--r--cmd/anubis/policy.go2
2 files changed, 7 insertions, 23 deletions
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) {