diff --git a/agent/agent.go b/agent/agent.go index 7f8d49a..eedd5de 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -12,6 +12,7 @@ import ( "cattlecloud.net/go/webtools/httpclient" "cattlecloud.net/go/webtools/middles/oauth" "cattlecloud.net/go/webtools/middles/oauth/nonces" + "cattlecloud.net/go/webtools/ratelimit" "cattlecloud.net/go/webtools/runtime" "cattlecloud.net/onlylogins/agent/cronjobs" "cattlecloud.net/onlylogins/agent/web" @@ -27,6 +28,13 @@ import ( "github.com/shoenig/ignore" ) +// loginRateLimit and loginRateWindow configure the fixed-window rate limit +// applied to the login endpoint. +const ( + loginRateLimit = 33 + loginRateWindow = time.Hour +) + type OnlyLogins struct { log *ulog.Log config *Config @@ -85,6 +93,9 @@ func (l *OnlyLogins) Start(parent scope.C) error { // stripe payments client stripec := payments.New(l.config.Stripe) + // create rate limit mechanism + limits := ratelimit.New(loginRateLimit, loginRateWindow) + // startup http web application webapp := web.New(l.config.HTTP, &web.Objects{ Environment: env, @@ -97,6 +108,7 @@ func (l *OnlyLogins) Start(parent scope.C) error { Mint: mint, Cypher: cypher, Payments: stripec, + Limiter: limits, }) go webapp.Start(parent) diff --git a/agent/web/abort.go b/agent/web/abort.go index ef2e542..bf89800 100644 --- a/agent/web/abort.go +++ b/agent/web/abort.go @@ -19,6 +19,7 @@ var ( ErrPageProblem = errors.New("problem loading page") ErrMissingCypher = errors.New("no encryption key cookie") ErrInvalidCypher = errors.New("invalid encryption key cookie") + ErrManyAttempts = errors.New("too many attempts; try again later") ) func login(log *ulog.Log, w http.ResponseWriter, r *http.Request) error { diff --git a/agent/web/http_login.go b/agent/web/http_login.go index 705042d..78f4cd6 100644 --- a/agent/web/http_login.go +++ b/agent/web/http_login.go @@ -17,6 +17,7 @@ import ( "cattlecloud.net/go/webtools" "cattlecloud.net/go/webtools/htmx" "cattlecloud.net/go/webtools/middles" + "cattlecloud.net/go/webtools/ratelimit" "cattlecloud.net/go/webtools/runtime" "cattlecloud.net/onlylogins/database" "cattlecloud.net/onlylogins/pkg/encrypt" @@ -33,6 +34,7 @@ type loginHTTP struct { environment runtime.Environment html *template.Template metrics *metrics.Set + limiter *ratelimit.Limiter } func (h *loginHTTP) Path() string { @@ -134,6 +136,12 @@ func (*loginHTTP) loginFields(r *http.Request) *users.LoginRequest { func (h *loginHTTP) login(ctx scope.C, w http.ResponseWriter, r *http.Request) error { request := h.loginFields(r) + // reject abusive bursts before doing any expensive work + if !h.limiter.Allow(request.IP) { + h.count("ol_logins_limited_total") + return problem(w, ErrManyAttempts) + } + if err := request.Validate(); err != nil { go func() { h.audit(request, false) }() return problem(w, err) diff --git a/agent/web/http_login_test.go b/agent/web/http_login_test.go index 004bb24..cae70db 100644 --- a/agent/web/http_login_test.go +++ b/agent/web/http_login_test.go @@ -8,8 +8,10 @@ import ( "net/http/httptest" "net/url" "testing" + "time" "cattlecloud.net/go/ulog" + "cattlecloud.net/go/webtools/ratelimit" "cattlecloud.net/go/webtools/runtime" "cattlecloud.net/onlylogins/database" "cattlecloud.net/onlylogins/hack/mocks" @@ -52,6 +54,7 @@ func newSignupRouter(t *testing.T) (*mux.Router, database.Store) { environment: env, html: ui.Load("login.html"), metrics: metrics.NewSet(), + limiter: ratelimit.New(33, time.Hour), }) return routes, store @@ -264,3 +267,38 @@ func TestSignup_MismatchedPassphrases_ReturnsError(t *testing.T) { must.NoError(t, cerr) must.Eq(t, baseline, count) } + +func TestLogin_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)) + + metset := metrics.NewSet() + h := &loginHTTP{ + log: ulog.New("test"), + metrics: metset, + limiter: limiter, + } + + r := makeLoginRequest(t, url.Values{ + "username": {"alice"}, + "passphrase": {"correct horse battery staple"}, + }) + r.RemoteAddr = ip + ":1234" + + w := httptest.NewRecorder() + err := h.login(t.Context(), w, r) + must.NoError(t, err) + + // 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, database.ErrIncorrectCredentials.Error()) + + // the rejection is counted + must.Eq(t, uint64(1), metset.GetOrCreateCounter("ol_logins_limited_total").Get()) +} diff --git a/agent/web/routing.go b/agent/web/routing.go index 251dc2f..6a3e97a 100644 --- a/agent/web/routing.go +++ b/agent/web/routing.go @@ -13,6 +13,7 @@ import ( "cattlecloud.net/go/webtools/middles" "cattlecloud.net/go/webtools/middles/oauth" "cattlecloud.net/go/webtools/middles/oauth/nonces" + "cattlecloud.net/go/webtools/ratelimit" "cattlecloud.net/go/webtools/runtime" "cattlecloud.net/onlylogins/database" "cattlecloud.net/onlylogins/pkg/encrypt" @@ -39,6 +40,7 @@ type Application struct { mint nonces.Mint cypher encrypt.Cypher payments payments.Client + limiter *ratelimit.Limiter } func (a *Application) createRoutes(router *mux.Router) { @@ -190,6 +192,7 @@ func (a *Application) createRoutes(router *mux.Router) { store: a.store, html: ui.Load("login.html"), metrics: a.metrics, + limiter: a.limiter, }) a.handleNormal(router, &profileHTTP{ @@ -423,6 +426,7 @@ type Objects struct { Mint nonces.Mint Cypher encrypt.Cypher Payments payments.Client + Limiter *ratelimit.Limiter } func New(config *Config, objects *Objects) *Application { @@ -439,6 +443,7 @@ func New(config *Config, objects *Objects) *Application { mint: objects.Mint, cypher: objects.Cypher, payments: objects.Payments, + limiter: objects.Limiter, } // setup gauges on storage diff --git a/go.mod b/go.mod index 929063f..efbd21e 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( cattlecloud.net/go/sitemaps v1.0.2 cattlecloud.net/go/ulog v1.1.3 cattlecloud.net/go/urlpath v1.1.0 - cattlecloud.net/go/webtools v0.8.0 + cattlecloud.net/go/webtools v0.9.0 github.com/BurntSushi/toml v1.6.0 github.com/VictoriaMetrics/metrics v1.45.0 github.com/dustin/go-humanize v1.1.0 diff --git a/go.sum b/go.sum index 5f6cf3f..5060a04 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ cattlecloud.net/go/ulog v1.1.3 h1:MF5OKiAny2p7wrmDMDi5C0+rGUipxozELbsjXYbPLnI= cattlecloud.net/go/ulog v1.1.3/go.mod h1:Dojc/73slqtZOAm7bvLDhi+X42hXtAqSqCYj+Xd2c64= cattlecloud.net/go/urlpath v1.1.0 h1:He/xP4Z1C13OTKgmSClmPG9EtztUrzM8wixu8NqLD2c= cattlecloud.net/go/urlpath v1.1.0/go.mod h1:EMkP1asJ089EYFignvJyVTOl6jRAOVIjs1Gn1BO3cqk= -cattlecloud.net/go/webtools v0.8.0 h1:sKgQ4ctaZAU7cPvUW41N+Y106HgtE61+HKt7CUpyzDk= -cattlecloud.net/go/webtools v0.8.0/go.mod h1:zixPQGlLVUe04maCxDRzPOJfJbcWC4fuJRtKlap1H9w= +cattlecloud.net/go/webtools v0.9.0 h1:XYDuwuSpxgaJ8q0gcIPxGWwIPnTGJEFmp432mrDSX6U= +cattlecloud.net/go/webtools v0.9.0/go.mod h1:kUQC+Mpd1jWBxmV0JLECFZHu3x9raToyRPvBxLfVkL4= github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/VictoriaMetrics/metrics v1.45.0 h1:Tv8dZRAjkO90rBTxYrWOHUWFBHsAKdKcZYmS/Wbo2EU=