From 32afc9c0404ac2df45c9ed64aef041aa66bb4bdc Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 2 Jul 2025 22:23:10 +0000 Subject: [PATCH] chore(lib/challenge): refactor Validate to take ValidateInput Signed-off-by: Xe Iaso --- lib/anubis.go | 6 +++++- lib/challenge/challenge.go | 7 ++++++- lib/challenge/metarefresh/metarefresh.go | 5 +++-- lib/challenge/proofofwork/proofofwork.go | 7 +++++-- lib/challenge/proofofwork/proofofwork_test.go | 4 +++- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/anubis.go b/lib/anubis.go index 006bd0a3..f19ffd6b 100644 --- a/lib/anubis.go +++ b/lib/anubis.go @@ -385,8 +385,12 @@ func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) { } challengeStr := s.challengeFor(r, rule.Challenge.Difficulty) + in := &challenge.ValidateInput{ + Challenge: challengeStr, + Rule: rule, + } - if err := impl.Validate(r, lg, rule, challengeStr); err != nil { + if err := impl.Validate(r, lg, in); err != nil { failedValidations.WithLabelValues(rule.Challenge.Algorithm).Inc() var cerr *challenge.Error s.ClearCookie(w, CookieOpts{Path: cookiePath, Host: r.Host}) diff --git a/lib/challenge/challenge.go b/lib/challenge/challenge.go index cfe69e2b..3f427a2b 100644 --- a/lib/challenge/challenge.go +++ b/lib/challenge/challenge.go @@ -48,6 +48,11 @@ type IssueInput struct { OGTags map[string]string } +type ValidateInput struct { + Rule *policy.Bot + Challenge string +} + type Impl interface { // Setup registers any additional routes with the Impl for assets or API routes. Setup(mux *http.ServeMux) @@ -56,5 +61,5 @@ type Impl interface { Issue(r *http.Request, lg *slog.Logger, in *IssueInput) (templ.Component, error) // Validate a challenge, making sure that it passes muster. - Validate(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string) error + Validate(r *http.Request, lg *slog.Logger, in *ValidateInput) error } diff --git a/lib/challenge/metarefresh/metarefresh.go b/lib/challenge/metarefresh/metarefresh.go index 68a2ed0f..3cad0a66 100644 --- a/lib/challenge/metarefresh/metarefresh.go +++ b/lib/challenge/metarefresh/metarefresh.go @@ -9,7 +9,6 @@ import ( "github.com/TecharoHQ/anubis" "github.com/TecharoHQ/anubis/lib/challenge" "github.com/TecharoHQ/anubis/lib/localization" - "github.com/TecharoHQ/anubis/lib/policy" "github.com/TecharoHQ/anubis/web" "github.com/a-h/templ" ) @@ -45,7 +44,9 @@ func (i *Impl) Issue(r *http.Request, lg *slog.Logger, in *challenge.IssueInput) return component, nil } -func (i *Impl) Validate(r *http.Request, lg *slog.Logger, rule *policy.Bot, wantChallenge string) error { +func (i *Impl) Validate(r *http.Request, lg *slog.Logger, in *challenge.ValidateInput) error { + wantChallenge := in.Challenge + gotChallenge := r.FormValue("challenge") if subtle.ConstantTimeCompare([]byte(wantChallenge), []byte(gotChallenge)) != 1 { diff --git a/lib/challenge/proofofwork/proofofwork.go b/lib/challenge/proofofwork/proofofwork.go index 74a35514..179cb51d 100644 --- a/lib/challenge/proofofwork/proofofwork.go +++ b/lib/challenge/proofofwork/proofofwork.go @@ -9,9 +9,9 @@ import ( "strings" "github.com/TecharoHQ/anubis/internal" + "github.com/TecharoHQ/anubis/lib/challenge" chall "github.com/TecharoHQ/anubis/lib/challenge" "github.com/TecharoHQ/anubis/lib/localization" - "github.com/TecharoHQ/anubis/lib/policy" "github.com/TecharoHQ/anubis/web" "github.com/a-h/templ" ) @@ -39,7 +39,10 @@ func (i *Impl) Issue(r *http.Request, lg *slog.Logger, in *chall.IssueInput) (te return component, nil } -func (i *Impl) Validate(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string) error { +func (i *Impl) Validate(r *http.Request, lg *slog.Logger, in *challenge.ValidateInput) error { + rule := in.Rule + challenge := in.Challenge + nonceStr := r.FormValue("nonce") if nonceStr == "" { return chall.NewError("validate", "invalid response", fmt.Errorf("%w nonce", chall.ErrMissingField)) diff --git a/lib/challenge/proofofwork/proofofwork_test.go b/lib/challenge/proofofwork/proofofwork_test.go index 34dbcdfd..1b3e9901 100644 --- a/lib/challenge/proofofwork/proofofwork_test.go +++ b/lib/challenge/proofofwork/proofofwork_test.go @@ -124,6 +124,8 @@ func TestBasic(t *testing.T) { t.Run(cs.name, func(t *testing.T) { lg := slog.With() + i.Setup(http.NewServeMux()) + inp := &challenge.IssueInput{ Rule: bot, Challenge: cs.challengeStr, @@ -133,7 +135,7 @@ func TestBasic(t *testing.T) { t.Errorf("can't issue challenge: %v", err) } - if err := i.Validate(cs.req, lg, bot, cs.challengeStr); !errors.Is(err, cs.err) { + if err := i.Validate(cs.req, lg, &challenge.ValidateInput{Rule: bot, Challenge: cs.challengeStr}); !errors.Is(err, cs.err) { t.Errorf("got wrong error from Validate, got %v but wanted %v", err, cs.err) } })