All checks were successful
check / check (push) Successful in 2m47s
With TRUSTED_PROXIES empty behind the reverse proxy production is required to run behind, every login POST keyed on the proxy's address and shared one 5/minute bucket. A stranger sending five POSTs a minute -- 0.08 requests per second, from anywhere -- kept that bucket permanently full, and the operator's own correct password was answered 429 indefinitely with no second administrative path. The login POST no longer has a pre-emptive limiter. The handler verifies credentials first and spends budget only on a FAILED attempt, so a correct password is never throttled whatever the counters hold. Three things follow, and are implemented together because the first is unsafe without the other two: - Failures are counted per (client bucket, submitted username), five per minute, after which further failures get 429 with a Retry-After. A successful login clears the counter, so mistyping and then succeeding does not leave the operator throttled. - Both key sets are capped at 1024 entries. The submitted username is attacker-controlled, so past the first cap failures fall back to a counter keyed on the client alone, and past both caps a failure is answered as throttled without being recorded. Tracked state stays under half a megabyte and does not grow with invented usernames. - Concurrent Argon2id verifications are capped at two, a 128 MB ceiling at 64 MB per hash, and the queue for those slots is capped at 16 waiters. Every password-hashing endpoint takes a slot, including the password-change endpoint, which holds one across both its hashes. A request that waits five seconds without a slot is answered 503, and one that arrives with the queue already full is shed with 503 immediately rather than joining it. Bounding the wait alone would not bound memory, and the queue depth is sized from what a parked waiter measurably retains rather than from the 1 MB body cap, which bounds only the raw body read. The body-cap, CSRF and form-parsing middleware all run before the guard, so a waiter holds its parsed form plus its request header block for the whole wait. Measured on the pinned go1.26.1 toolchain as the HeapAlloc delta across two GCs with 64 waiters parked in the handler: an ordinary two-field login form retains ~0 MB, a 1 MB urlencoded body at Go's 10,000-parameter parse cap retains 2.82 MB (3.09 MB with %41 escapes), and the ~0.9 MB of headers the 1 MB header cap allows takes it to 4.18 MB. The retained parse and the header block dominate, not the raw body. So 16 waiters: 16 x 4.18 MB is about 67 MB of committed queue memory, and two slots drain a full 16-deep queue in about 0.6 s, far inside the deadline. Peak commitment for the endpoint is about 203 MB — 128 MB of Argon2id plus the 18 requests holding a parsed form, 16 queued and the 2 being hashed, at about 75 MB. An unknown username is verified against a dummy hash instead of returning early, so a nonexistent account costs the same time as a real one and the response cannot be used to enumerate usernames. The password-change limiter is unchanged: RequireAuth runs ahead of it, so only a request already carrying a valid session reaches its bucket. Two consequences are documented rather than fixed, because they follow from the shape the issue asks for. Online guessing throughput rises from 5 a minute to roughly 27 a second, about 2.3 million a day: the credential check always precedes the counter, so the 429 is a label on the response rather than a gate in front of the hash, and what bounds brute force is the semaphore. And under a sustained flood the residual exposure is a loss of login availability, not merely of latency -- above about 27 requests a second most attempts are shed with 503, so a determined flood still denies login for as long as it runs. It costs roughly 400x more to run, nothing accumulates, and the first attempt after it stops succeeds. Restarting the service does not help: the counters a restart clears are not what is saturated. Also adds the missing test for the third bucketKey call site, where the peer is a trusted proxy but the forwarded chain names no client. Every existing test of that fallback uses an IPv4 proxy, where bucketKey is the identity function, so dropping the /64 masking there left the suite green. README and the TRUSTED_PROXIES startup warning updated: a shared bucket now costs precision, not the availability of the admin path.
378 lines
13 KiB
Go
378 lines
13 KiB
Go
package middleware
|
|
|
|
import (
|
|
"math"
|
|
"net/http"
|
|
"net/netip"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-chi/httprate"
|
|
)
|
|
|
|
const (
|
|
// loginRateLimit is the maximum number of FAILED login attempts
|
|
// one client may make against one submitted username per
|
|
// interval before further failures are answered 429. Successful
|
|
// attempts are never counted and never throttled — see
|
|
// loginGuard.
|
|
loginRateLimit = 5
|
|
|
|
// loginRateInterval is the time window for the login failure
|
|
// limit.
|
|
loginRateInterval = 1 * time.Minute
|
|
|
|
// passwordChangeRateLimit is the maximum number of password
|
|
// change attempts per interval. Each attempt verifies the
|
|
// current password, so the endpoint must be rate-limited
|
|
// like any other password-based authentication endpoint.
|
|
passwordChangeRateLimit = 5
|
|
|
|
// passwordChangeRateInterval is the time window for the
|
|
// password change rate limit.
|
|
passwordChangeRateInterval = 1 * time.Minute
|
|
|
|
// receiverRateInterval is the time window for the webhook
|
|
// receiver rate limit. The configured limit is expressed in
|
|
// requests per minute.
|
|
receiverRateInterval = 1 * time.Minute
|
|
|
|
// receiverAggregateMultiplier scales the configured
|
|
// per-entrypoint receiver limit into the aggregate limit one
|
|
// client IP may spend across the whole /webhook/* route. Ten
|
|
// entrypoints' worth lets a single sender address drive several
|
|
// entrypoints at their full rate, while still capping what one
|
|
// address costs the unauthenticated receiver.
|
|
receiverAggregateMultiplier = 10
|
|
|
|
// maxForwardedHops bounds how many X-Forwarded-For entries the
|
|
// chain walk examines. Real chains are one to three hops, but a
|
|
// client can pad the header up to MaxHeaderBytes, so without a
|
|
// bound every request pays a walk proportional to whatever the
|
|
// client sent.
|
|
maxForwardedHops = 64
|
|
|
|
// ipv6BucketBits is the prefix length IPv6 clients are bucketed
|
|
// on. A routed /64 is the normal residential and mobile
|
|
// allocation, so it is the unit an attacker gets addresses in
|
|
// and therefore the unit worth limiting.
|
|
ipv6BucketBits = 64
|
|
)
|
|
|
|
// normalizeAddr strips the IPv4-in-IPv6 wrapper and any zone from
|
|
// addr so that comparisons and bucket keys are canonical.
|
|
func normalizeAddr(addr netip.Addr) netip.Addr {
|
|
return addr.Unmap().WithZone("")
|
|
}
|
|
|
|
// bucketKey is the rate-limit bucket identity of a client address.
|
|
// IPv4 keys on the full address; IPv6 keys on its /64 prefix,
|
|
// because keying IPv6 per /128 lets one ordinary subscriber rotate
|
|
// source addresses inside its own routed /64 and mint a fresh bucket
|
|
// per request — evading every limiter here at the network layer,
|
|
// with no spoofing and nothing to detect.
|
|
//
|
|
// An IPv4-mapped address (::ffff:1.2.3.4) is keyed as the IPv4
|
|
// address it carries, never masked to a /64: mapped form all shares
|
|
// the ::ffff:0:0/96 prefix, so masking would collapse every IPv4
|
|
// client reaching a proxy that emits it into one bucket. Callers
|
|
// pass addresses through normalizeAddr, which already unmaps; the
|
|
// unmap here keeps the property true of the key function itself.
|
|
//
|
|
// The two families cannot collide: an IPv4 key is a bare dotted
|
|
// quad, and an IPv6 key always carries a "/64" suffix.
|
|
func bucketKey(addr netip.Addr) string {
|
|
addr = addr.Unmap()
|
|
|
|
if addr.Is4() {
|
|
return addr.String()
|
|
}
|
|
|
|
// Prefix errors only on a negative bit count, on over 32 bits
|
|
// for an IPv4 address, or on over 128 for IPv6. The count here
|
|
// is the constant 64 and the IPv4 case returned above, so the
|
|
// error is unreachable. (The zero Addr does not error either: it
|
|
// yields the zero Prefix. Neither call site can produce one,
|
|
// since both parse the address first.)
|
|
prefix, _ := addr.Prefix(ipv6BucketBits)
|
|
|
|
return prefix.String()
|
|
}
|
|
|
|
// isTrustedProxy reports whether addr belongs to a network the
|
|
// operator listed in TRUSTED_PROXIES. The list is empty by default,
|
|
// so by default nothing is trusted.
|
|
func (m *Middleware) isTrustedProxy(addr netip.Addr) bool {
|
|
for _, prefix := range m.params.Config.TrustedProxies {
|
|
if prefix.Contains(addr) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// forwardedClientAddr returns the client address named by this
|
|
// request's X-Forwarded-For chain. It is consulted only for requests
|
|
// whose direct peer is a trusted proxy.
|
|
//
|
|
// X-Forwarded-For is the only header read. X-Real-IP and
|
|
// True-Client-IP are deliberately ignored: the reverse proxies in
|
|
// common use append to X-Forwarded-For and pass any other header the
|
|
// client sent through untouched, so believing a single-valued header
|
|
// would let a client behind the trusted proxy name its own bucket —
|
|
// the very bypass this gating exists to close.
|
|
//
|
|
// The chain is walked right to left, because the rightmost entry is
|
|
// the one the nearest proxy appended and everything to its left may
|
|
// have been written by the client. The first hop that is not itself
|
|
// a trusted proxy is the client. A hop that cannot be read as a bare
|
|
// address ends the walk: past it the chain is not the shape assumed
|
|
// here, so the caller falls back to the peer address.
|
|
//
|
|
// Only the last maxForwardedHops entries are examined. A longer chain
|
|
// is padding, and running out of hops falls back to the peer address
|
|
// the same way an unreadable hop does.
|
|
//
|
|
// The entries are cut off the right end of each header value in place
|
|
// rather than split out of it: the receiver is unauthenticated and a
|
|
// client can pad the header up to MaxHeaderBytes, so splitting would
|
|
// allocate in proportion to the padding (about 8 MB for a 1 MB
|
|
// header) before the cap could discard any of it. Multiple header
|
|
// values are walked in reverse for the same reason, since joining
|
|
// them copies the whole chain.
|
|
func (m *Middleware) forwardedClientAddr(
|
|
r *http.Request,
|
|
) (netip.Addr, bool) {
|
|
seen := 0
|
|
|
|
for _, value := range slices.Backward(
|
|
r.Header.Values("X-Forwarded-For"),
|
|
) {
|
|
for last := false; !last && seen < maxForwardedHops; seen++ {
|
|
hop := value
|
|
|
|
comma := strings.LastIndexByte(value, ',')
|
|
if comma < 0 {
|
|
last = true
|
|
} else {
|
|
hop, value = value[comma+1:], value[:comma]
|
|
}
|
|
|
|
hop = strings.TrimSpace(hop)
|
|
if hop == "" {
|
|
continue
|
|
}
|
|
|
|
addr, err := netip.ParseAddr(hop)
|
|
if err != nil {
|
|
return netip.Addr{}, false
|
|
}
|
|
|
|
if addr = normalizeAddr(addr); !m.isTrustedProxy(addr) {
|
|
return addr, true
|
|
}
|
|
}
|
|
}
|
|
|
|
return netip.Addr{}, false
|
|
}
|
|
|
|
// rateLimitKey is the client identity every rate limiter in this
|
|
// package buckets on. Forwarded headers are honoured only when the
|
|
// direct peer (RemoteAddr) is inside the configured trusted-proxy
|
|
// set; otherwise the peer address itself is the key. Without that
|
|
// gate any client could mint a fresh bucket per request, or starve
|
|
// another client's bucket, by picking an X-Forwarded-For value —
|
|
// which makes every limit here decorative against a deliberate
|
|
// attacker.
|
|
//
|
|
// The address that identifies the client is then reduced to a bucket
|
|
// by bucketKey: full address for IPv4, /64 prefix for IPv6.
|
|
func (m *Middleware) rateLimitKey(r *http.Request) (string, error) {
|
|
return m.clientKey(r), nil
|
|
}
|
|
|
|
// clientKey computes the bucket key described on rateLimitKey.
|
|
func (m *Middleware) clientKey(r *http.Request) string {
|
|
peer, err := netip.ParseAddr(ipFromHostPort(r.RemoteAddr))
|
|
if err != nil {
|
|
// Not an address we can reason about; key on the raw
|
|
// value, the most specific identity left. Distinct
|
|
// RemoteAddr values stay in distinct buckets, so this
|
|
// path cannot silently collapse unrelated clients
|
|
// together. On a Unix-socket listener every peer
|
|
// carries the same RemoteAddr and so shares one bucket,
|
|
// which is the fail-closed direction.
|
|
return r.RemoteAddr
|
|
}
|
|
|
|
peer = normalizeAddr(peer)
|
|
if !m.isTrustedProxy(peer) {
|
|
return bucketKey(peer)
|
|
}
|
|
|
|
if addr, ok := m.forwardedClientAddr(r); ok {
|
|
return bucketKey(addr)
|
|
}
|
|
|
|
return bucketKey(peer)
|
|
}
|
|
|
|
// tooManyRequests returns the 429 handler used by the
|
|
// password-change and per-entrypoint receiver limiters: it logs the
|
|
// rejection with logMessage and answers with responseMessage.
|
|
// httprate adds the Retry-After header (RFC 6585). The aggregate
|
|
// receiver limiter uses floodTooManyRequests instead.
|
|
func (m *Middleware) tooManyRequests(
|
|
logMessage, responseMessage string,
|
|
) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
m.log.Warn(logMessage, "path", r.URL.Path)
|
|
http.Error(w, responseMessage, http.StatusTooManyRequests)
|
|
}
|
|
}
|
|
|
|
// floodTooManyRequests returns the 429 handler for a limiter whose
|
|
// rejections are themselves the flood: it logs at DEBUG and without
|
|
// the path, then answers with responseMessage.
|
|
//
|
|
// The aggregate receiver limiter trips exactly when one address is
|
|
// sending faster than the receiver wants to serve, so its rejection
|
|
// log is one line per request of that flood. At WARN with "path" that
|
|
// hands a client a way to write its own text into the operator's log,
|
|
// at a level that trips alerting, once per request — the log-volume
|
|
// problem this limiter exists to bound. DEBUG is off in production by
|
|
// default, so a flood costs nothing here; the path is dropped so that
|
|
// turning DEBUG on to diagnose one does not restore the problem.
|
|
//
|
|
// This limiter bounds the database work an invented path costs, not
|
|
// the number of log lines it produces: the access log in
|
|
// middleware.go still records every request, served or rejected.
|
|
func (m *Middleware) floodTooManyRequests(
|
|
logMessage, responseMessage string,
|
|
) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, _ *http.Request) {
|
|
m.log.Debug(logMessage)
|
|
http.Error(w, responseMessage, http.StatusTooManyRequests)
|
|
}
|
|
}
|
|
|
|
// PasswordChangeRateLimit returns middleware that enforces
|
|
// per-IP rate limiting on password change attempts. The change
|
|
// endpoint verifies the current password, so without a limit a
|
|
// stolen session could be used to brute-force it.
|
|
//
|
|
// Unlike the login POST this limit is still spent on arrival, which
|
|
// is safe here: RequireAuth runs ahead of it, so only a request
|
|
// already carrying a valid session can reach the bucket, and an
|
|
// operator locked out of changing a password can still log in.
|
|
func (m *Middleware) PasswordChangeRateLimit() func(http.Handler) http.Handler {
|
|
return m.postRateLimit(
|
|
passwordChangeRateLimit,
|
|
passwordChangeRateInterval,
|
|
"password change rate limit exceeded",
|
|
"Too many password change attempts. "+
|
|
"Please try again later.",
|
|
)
|
|
}
|
|
|
|
// postRateLimit builds middleware that enforces a per-IP rate
|
|
// limit on POST requests only; all other methods pass through
|
|
// unaffected. Requests over the limit receive a 429 with the
|
|
// given response message, and each rejection is logged with the
|
|
// given log message. Clients are identified by rateLimitKey.
|
|
func (m *Middleware) postRateLimit(
|
|
limit int,
|
|
interval time.Duration,
|
|
logMessage, responseMessage string,
|
|
) func(http.Handler) http.Handler {
|
|
limiter := httprate.Limit(
|
|
limit,
|
|
interval,
|
|
httprate.WithKeyFuncs(m.rateLimitKey),
|
|
httprate.WithLimitHandler(
|
|
m.tooManyRequests(logMessage, responseMessage),
|
|
),
|
|
)
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
limited := limiter(next)
|
|
|
|
return http.HandlerFunc(func(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
// Only rate-limit POST requests.
|
|
if r.Method != http.MethodPost {
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
}
|
|
|
|
limited.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
// ReceiverRateLimit returns middleware that rate-limits the public
|
|
// webhook receiver endpoint with two limits in series.
|
|
//
|
|
// The inner limit is per client IP per request path: the path
|
|
// contains the entrypoint UUID, so each sender is limited per
|
|
// entrypoint without affecting other senders or other entrypoints.
|
|
// It is Config.ReceiverRateLimit requests per minute.
|
|
//
|
|
// That limit alone bounds nothing in aggregate. The route pattern
|
|
// /webhook/{uuid} matches any single segment, so a client that
|
|
// invents a fresh path per request mints a fresh bucket per request
|
|
// and never refills one — and every such request still reaches the
|
|
// handler's entrypoint lookup before it 404s. The outer limit is
|
|
// therefore keyed on the client IP alone, capping what one address
|
|
// can spend across the whole route however it varies the path.
|
|
//
|
|
// Requests over either limit receive a 429. Clients are identified
|
|
// by rateLimitKey.
|
|
func (m *Middleware) ReceiverRateLimit() func(http.Handler) http.Handler {
|
|
perEntrypoint := httprate.Limit(
|
|
m.params.Config.ReceiverRateLimit,
|
|
receiverRateInterval,
|
|
httprate.WithKeyFuncs(
|
|
m.rateLimitKey,
|
|
httprate.KeyByEndpoint,
|
|
),
|
|
httprate.WithLimitHandler(m.tooManyRequests(
|
|
"webhook receiver rate limit exceeded",
|
|
"Too many requests. Please slow down.",
|
|
)),
|
|
)
|
|
|
|
aggregate := httprate.Limit(
|
|
receiverAggregateLimit(m.params.Config.ReceiverRateLimit),
|
|
receiverRateInterval,
|
|
httprate.WithKeyFuncs(m.rateLimitKey),
|
|
httprate.WithLimitHandler(m.floodTooManyRequests(
|
|
"webhook receiver aggregate rate limit exceeded",
|
|
"Too many requests. Please slow down.",
|
|
)),
|
|
)
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
return aggregate(perEntrypoint(next))
|
|
}
|
|
}
|
|
|
|
// receiverAggregateLimit is the per-IP aggregate limit derived from
|
|
// the configured per-entrypoint limit. The operator sets the latter
|
|
// and nothing bounds it from above, so the multiplication is
|
|
// saturated rather than allowed to wrap into a negative limit that
|
|
// would reject every request.
|
|
func receiverAggregateLimit(perEntrypoint int) int {
|
|
if perEntrypoint > math.MaxInt/receiverAggregateMultiplier {
|
|
return math.MaxInt
|
|
}
|
|
|
|
return perEntrypoint * receiverAggregateMultiplier
|
|
}
|