Files
opencloud/vendor/github.com/go-chi/httprate/deprecated.go
T
Pascal Bleser a7789f8462 graph: rate limit the personal data export
Each personal data export collects all events of the user and writes
them into the user's personal space, so starting exports over and over
puts load on the system. Limit how many exports a user can start to 5
per minute, configurable with GRAPH_EXPORT_PERSONAL_DATA_RATE_LIMIT
(0 disables the limit). Requests beyond it get a 429 with a
Retry-After header.

The limit uses a new middleware, RateLimitPerAuthenticatedUser. It
counts requests by the id of the authenticated user rather than by the
URL, so that variations of the URL, such as a trailing slash, can't get
around it. It comes after the check that users only export their own
data, so that requests for other users don't count against any limit.
The counters are kept in memory by every instance of the graph service.

This adds the dependency github.com/go-chi/httprate v0.16.1 (MIT), from
the authors of the chi router that the service already uses. It is used
instead of a limiter of our own, because counting requests in sliding
time windows correctly under concurrency is easy to get wrong, and the
library is maintained and tested for exactly that. It brings no further
modules: the two it needs are already required. There are no known
vulnerabilities in it, and govulncheck reports none.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-13 18:25:47 +01:00

151 lines
6.7 KiB
Go

package httprate
import (
"net"
"net/http"
"strings"
"time"
)
// This file collects the deprecated public surface in one place. These are kept
// for backward compatibility and still compile/behave as before, but new code
// should migrate to the replacements called out in each doc comment. httprate
// has not reached a stable v1, so a future major release may remove them.
// Deprecated: Use LimitBy(requestLimit, windowLength, keyFn, options...) instead,
// which makes the rate-limit key an explicit, required argument rather than an
// optional WithKeyFuncs. Pass the key function directly (e.g. a KeyFunc that
// reads a trusted client IP — see CanonicalizeIP), or httprate.Key("*") for a
// single global bucket. The remaining options
// (WithLimitCounter, WithLimitHandler, WithResponseHeaders, ...) carry over
// unchanged as LimitBy's trailing variadic.
func Limit(requestLimit int, windowLength time.Duration, options ...Option) func(next http.Handler) http.Handler {
// Key("*") is the default key; any WithKeyFuncs in options overrides it.
return LimitBy(requestLimit, windowLength, Key("*"), options...)
}
// Deprecated: Use LimitBy(requestLimit, windowLength, Key("*")) instead — a
// single global rate-limit bucket keyed by a constant. (LimitAll already keys
// every request by "*" under the hood; this just makes that explicit.)
func LimitAll(requestLimit int, windowLength time.Duration) func(next http.Handler) http.Handler {
return LimitBy(requestLimit, windowLength, Key("*"))
}
// Deprecated: LimitByIP keys off r.RemoteAddr (see KeyByIP). It is not
// spoofable, but behind a reverse proxy, load balancer, or CDN r.RemoteAddr is
// the proxy's address, so every client sharing that proxy lands in one bucket —
// usually the wrong key in production. State your trust model explicitly:
// install one of chi's middleware.ClientIPFrom* middlewares (chi v5.3.0+) and
// key off the resolved IP (CanonicalizeIP buckets IPv6 by /64):
//
// // Directly exposed to clients (LimitByIP's exact behavior, made explicit):
// r.Use(middleware.ClientIPFromRemoteAddr)
// r.Use(httprate.LimitBy(requestLimit, windowLength, func(r *http.Request) (string, error) {
// return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
// }))
func LimitByIP(requestLimit int, windowLength time.Duration) func(next http.Handler) http.Handler {
return LimitBy(requestLimit, windowLength, KeyByIP)
}
// Deprecated: LimitByRealIP is built on the spoofable KeyByRealIP and lets a
// remote attacker forge the rate-limit key — see GHSA-9g5q-2w5x-hmxf,
// GHSA-rjr7-jggh-pgcp, GHSA-3fxj-6jh8-hvhx for the equivalent flaw in chi's
// middleware.RealIP. Install one of chi's middleware.ClientIPFrom* middlewares
// (chi v5.3.0+) and key off the resolved IP instead (CanonicalizeIP buckets
// IPv6 by /64):
//
// r.Use(middleware.ClientIPFromXFF("10.0.0.0/8"))
// r.Use(httprate.LimitBy(requestLimit, windowLength, func(r *http.Request) (string, error) {
// return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
// }))
func LimitByRealIP(requestLimit int, windowLength time.Duration) func(next http.Handler) http.Handler {
return LimitBy(requestLimit, windowLength, KeyByRealIP)
}
// Deprecated: KeyByRealIP trusts the client-supplied True-Client-IP,
// X-Real-IP, and X-Forwarded-For headers without verifying any proxy chain, so
// a remote attacker can forge the rate-limit key — see GHSA-9g5q-2w5x-hmxf,
// GHSA-rjr7-jggh-pgcp, GHSA-3fxj-6jh8-hvhx for the equivalent flaw in chi's
// middleware.RealIP. On a rate-limiter this is two-sided: an attacker can evade
// the limit by rotating the spoofed header (unbounded buckets) or lock a victim
// out by pinning the header to the victim's IP (exhausting their bucket).
//
// Install one of chi's middleware.ClientIPFrom* middlewares (chi v5.3.0+) and
// key off the resolved IP instead (CanonicalizeIP buckets IPv6 by /64):
//
// r.Use(middleware.ClientIPFromXFF("10.0.0.0/8"))
// r.Use(httprate.LimitBy(100, time.Minute, func(r *http.Request) (string, error) {
// return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
// }))
func KeyByRealIP(r *http.Request) (string, error) {
var ip string
if tcip := r.Header.Get("True-Client-IP"); tcip != "" {
ip = tcip
} else if xrip := r.Header.Get("X-Real-IP"); xrip != "" {
ip = xrip
} else if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
i := strings.Index(xff, ", ")
if i == -1 {
i = len(xff)
}
ip = xff[:i]
} else {
var err error
ip, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
}
}
return CanonicalizeIP(ip), nil
}
// Deprecated: WithKeyByRealIP installs the spoofable KeyByRealIP and lets a
// remote attacker forge the rate-limit key — see GHSA-9g5q-2w5x-hmxf,
// GHSA-rjr7-jggh-pgcp, GHSA-3fxj-6jh8-hvhx for the equivalent flaw in chi's
// middleware.RealIP. Install one of chi's middleware.ClientIPFrom* middlewares
// (chi v5.3.0+) and key off the resolved IP with LimitBy instead (see
// CanonicalizeIP).
func WithKeyByRealIP() Option {
return WithKeyFuncs(KeyByRealIP)
}
// Deprecated: KeyByIP keys off r.RemoteAddr, the TCP peer that opened the
// connection. Unlike KeyByRealIP it is NOT spoofable (RemoteAddr is set by
// net/http, never from a header) — but behind a reverse proxy, load balancer,
// or CDN, r.RemoteAddr is the proxy's address, so every client sharing that
// proxy lands in one rate-limit bucket. That's usually the wrong key in
// production, and there is no safe default IP source.
//
// State your trust model explicitly with one of chi's middleware.ClientIPFrom*
// middlewares (chi v5.3.0+) and key off the resolved IP (CanonicalizeIP buckets
// IPv6 by /64):
//
// // Directly exposed to clients (KeyByIP's exact behavior, made explicit):
// r.Use(middleware.ClientIPFromRemoteAddr)
// r.Use(httprate.LimitBy(100, time.Minute, func(r *http.Request) (string, error) {
// return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
// }))
//
// // Behind a proxy: use ClientIPFromXFF / ClientIPFromHeader / ... instead.
//
// KeyByIP returns the IPv4 address unchanged, or the /64 prefix for IPv6.
func KeyByIP(r *http.Request) (string, error) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
}
return CanonicalizeIP(ip), nil
}
// Deprecated: WithKeyByIP installs KeyByIP, which keys off r.RemoteAddr — the
// proxy's address behind a reverse proxy, load balancer, or CDN, and usually
// the wrong key in production (see KeyByIP). It is not a spoofing issue, but
// there is no safe default IP source. State your trust model explicitly:
// install one of chi's middleware.ClientIPFrom* middlewares (chi v5.3.0+) and
// key off the resolved IP with LimitBy instead (see CanonicalizeIP).
func WithKeyByIP() Option {
return WithKeyFuncs(KeyByIP)
}