Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions agent/web/http_passphrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
50 changes: 50 additions & 0 deletions agent/web/http_passphrase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ 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"
"cattlecloud.net/onlylogins/pkg/encrypt"
"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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
2 changes: 2 additions & 0 deletions agent/web/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down