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
12 changes: 12 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -97,6 +108,7 @@ func (l *OnlyLogins) Start(parent scope.C) error {
Mint: mint,
Cypher: cypher,
Payments: stripec,
Limiter: limits,
})
go webapp.Start(parent)

Expand Down
1 change: 1 addition & 0 deletions agent/web/abort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions agent/web/http_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,6 +34,7 @@ type loginHTTP struct {
environment runtime.Environment
html *template.Template
metrics *metrics.Set
limiter *ratelimit.Limiter
}

func (h *loginHTTP) Path() string {
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions agent/web/http_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
5 changes: 5 additions & 0 deletions agent/web/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down