diff --git a/agent/web/http_passphrase.go b/agent/web/http_passphrase.go index bdb7cb9..bc524f1 100644 --- a/agent/web/http_passphrase.go +++ b/agent/web/http_passphrase.go @@ -12,14 +12,17 @@ import ( "cattlecloud.net/go/forms" "cattlecloud.net/go/scope" "cattlecloud.net/go/ulog" + "cattlecloud.net/go/webtools" "cattlecloud.net/go/webtools/htmx" "cattlecloud.net/go/webtools/middles" "cattlecloud.net/go/webtools/middles/identity" + "cattlecloud.net/go/webtools/ratelimit" "cattlecloud.net/onlylogins/agent/web/pages" "cattlecloud.net/onlylogins/database" "cattlecloud.net/onlylogins/pkg/format" "cattlecloud.net/onlylogins/pkg/sessions/cookies" "cattlecloud.net/onlylogins/pkg/users" + "github.com/VictoriaMetrics/metrics" "github.com/shoenig/go-conceal" ) @@ -28,6 +31,8 @@ type passphraseHTTP struct { store database.Store session middles.Sessions[users.Identity] html *template.Template + metrics *metrics.Set + limiter *ratelimit.Limiter } func (h *passphraseHTTP) Path() string { @@ -90,6 +95,12 @@ func (h *passphraseHTTP) post(ctx scope.C, w http.ResponseWriter, r *http.Reques return htmx.SetRedirect(w, "/login") } + // reject abusive bursts before doing any expensive work + if !h.limiter.Allow(webtools.Origins(r).From()) { + h.count("ol_passphrase_limited_total") + return problem(w, ErrManyAttempts) + } + request := h.requestFrom(r) if err := request.Validate(); err != nil { @@ -160,3 +171,8 @@ func (h *passphraseHTTP) description() string { Change the passphrase used to encrypt your data.` return format.PageDescription(text) } + +// count increments the named counter metric. +func (h *passphraseHTTP) count(metric string) { + h.metrics.GetOrCreateCounter(metric).Inc() +} diff --git a/agent/web/http_passphrase_test.go b/agent/web/http_passphrase_test.go index c4c130a..645b010 100644 --- a/agent/web/http_passphrase_test.go +++ b/agent/web/http_passphrase_test.go @@ -12,6 +12,7 @@ import ( "cattlecloud.net/go/scope" "cattlecloud.net/go/ulog" + "cattlecloud.net/go/webtools/ratelimit" "cattlecloud.net/onlylogins/database" "cattlecloud.net/onlylogins/hack/mocks" "cattlecloud.net/onlylogins/hack/mocks/mockdb" @@ -19,6 +20,7 @@ import ( "cattlecloud.net/onlylogins/pkg/sitecontent" "cattlecloud.net/onlylogins/pkg/users" "cattlecloud.net/onlylogins/ui" + "github.com/VictoriaMetrics/metrics" "github.com/shoenig/go-conceal" "github.com/shoenig/test/must" ) @@ -34,6 +36,8 @@ func newPassphraseHandler(t *testing.T, store database.Store) (*mocks.Sessions, store: store, session: scache, html: ui.Load("passphrase.html"), + metrics: metrics.NewSet(), + limiter: ratelimit.New(33, time.Hour), } return scache, session(handler, scache) @@ -281,3 +285,49 @@ func TestPassphrase_POST_not_logged_in(t *testing.T) { must.Eq(t, http.StatusFound, w.Code) must.Eq(t, "/login", w.Header().Get("HX-Redirect")) } + +func TestPassphrase_RateLimited(t *testing.T) { + t.Parallel() + + const ip = "203.0.113.7" + + // exhaust the single allowed attempt so the next one is rejected + limiter := ratelimit.New(1, time.Hour) + must.True(t, limiter.Allow(ip)) + + store := mockdb.Open(t) + scache := mocks.NewSessions() + metset := metrics.NewSet() + handler := session(&passphraseHTTP{ + log: ulog.New("test"), + store: store, + session: scache, + html: ui.Load("passphrase.html"), + metrics: metset, + limiter: limiter, + }, scache) + + const oldPass = "correct horse battery staple" + const newPass = "brand new battery horse staple" + + uid, _ := newPassphraseUser(t, store, "alice", oldPass) + + cookie := scache.Create(uid, time.Minute) + request := requestPost(t, "/profile/passphrase", cookie, url.Values{ + "previous": {oldPass}, + "passphrase": {newPass}, + "confirm": {newPass}, + }) + request.RemoteAddr = ip + ":1234" + + w := httptest.NewRecorder() + handler.ServeHTTP(w, request) + + // the generic message is rendered and reveals nothing about the account + body := w.Body.String() + must.StrContains(t, body, ErrManyAttempts.Error()) + must.StrNotContains(t, body, "passphrase has been updated") + + // the rejection is counted + must.Eq(t, uint64(1), metset.GetOrCreateCounter("ol_passphrase_limited_total").Get()) +} diff --git a/agent/web/routing.go b/agent/web/routing.go index 6a3e97a..a18a673 100644 --- a/agent/web/routing.go +++ b/agent/web/routing.go @@ -206,6 +206,8 @@ func (a *Application) createRoutes(router *mux.Router) { store: a.store, session: a.scache, html: ui.Load("passphrase.html"), + metrics: a.metrics, + limiter: a.limiter, }) a.handleNormal(router, &aboutHTTP{