Bumps the minor-and-patch group with 7 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/go-chi/httprate](https://github.com/go-chi/httprate) | `0.16.0` | `0.16.1` | | [github.com/go-ldap/ldap/v3](https://github.com/go-ldap/ldap) | `3.4.14` | `3.4.15` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.15.0` | `2.15.1` | | [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) | `1.24.1` | `1.25.0` | | [github.com/tidwall/gjson](https://github.com/tidwall/gjson) | `1.19.1` | `1.20.0` | | [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp](https://github.com/open-telemetry/opentelemetry-go-contrib) | `0.71.0` | `0.72.0` | | [golang.org/x/sync](https://github.com/golang/sync) | `0.23.0` | `0.24.0` | Updates `github.com/go-chi/httprate` from 0.16.0 to 0.16.1 - [Release notes](https://github.com/go-chi/httprate/releases) - [Commits](https://github.com/go-chi/httprate/compare/v0.16.0...v0.16.1) Updates `github.com/go-ldap/ldap/v3` from 3.4.14 to 3.4.15 - [Release notes](https://github.com/go-ldap/ldap/releases) - [Commits](https://github.com/go-ldap/ldap/compare/v3.4.14...v3.4.15) Updates `github.com/nats-io/nats-server/v2` from 2.15.0 to 2.15.1 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.15.0...v2.15.1) Updates `github.com/prometheus/client_golang` from 1.24.1 to 1.25.0 - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/v1.25.0/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.24.1...v1.25.0) Updates `github.com/tidwall/gjson` from 1.19.1 to 1.20.0 - [Commits](https://github.com/tidwall/gjson/compare/v1.19.1...v1.20.0) Updates `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` from 0.71.0 to 0.72.0 - [Release notes](https://github.com/open-telemetry/opentelemetry-go-contrib/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go-contrib/compare/zpages/v0.71.0...zpages/v0.72.0) Updates `golang.org/x/sync` from 0.23.0 to 0.24.0 - [Commits](https://github.com/golang/sync/compare/v0.23.0...v0.24.0) --- updated-dependencies: - dependency-name: github.com/go-chi/httprate dependency-version: 0.16.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/go-ldap/ldap/v3 dependency-version: 3.4.15 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.15.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/prometheus/client_golang dependency-version: 1.25.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/tidwall/gjson dependency-version: 1.20.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp dependency-version: 0.72.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: golang.org/x/sync dependency-version: 0.24.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
httprate - HTTP Rate Limiter
net/http request rate limiter based on the Sliding Window Counter pattern inspired by
CloudFlare https://blog.cloudflare.com/counting-things-a-lot-of-different-things.
Warning
Security:
LimitByRealIP/KeyByRealIP/WithKeyByRealIPare deprecated. They derive the rate-limit key from client-supplied headers (True-Client-IP,X-Real-IP,X-Forwarded-For) with no proxy-trust check, so a remote attacker can spoof the key — either rotating it to evade the limit or pinning it to a victim's IP to lock that victim out (HTTP 429). This is the same flaw fixed in chi'smiddleware.RealIP(see GHSA-9g5q-2w5x-hmxf, GHSA-rjr7-jggh-pgcp, GHSA-3fxj-6jh8-hvhx). Rate-limit by a trusted client IP instead — see Rate limit by client IP behind a proxy.
The sliding window counter pattern is accurate, smooths traffic and offers a simple counter
design to share a rate-limit among a cluster of servers. For example, if you'd like
to use redis to coordinate a rate-limit across a group of microservices you just need
to implement the httprate.LimitCounter interface to support an atomic increment and get.
Backends
- Local in-memory backend (default)
- Redis backend: https://github.com/go-chi/httprate-redis
Example
package main
import (
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
// Enable httprate request limiter of 100 requests per minute, keyed by the
// client IP.
//
// There is no safe default IP source, so you state your trust model
// explicitly: one of chi's middleware.ClientIPFrom* middlewares (chi v5.3.0+)
// resolves the client IP, and the KeyFunc reads it back with
// middleware.GetClientIP. Pick the chi ClientIPFrom* that matches your
// deployment — here we assume the server is directly exposed to clients (no
// proxy), so the client IP is the TCP peer (RemoteAddr). Behind a reverse
// proxy or CDN, use ClientIPFromXFF / ClientIPFromHeader instead; see "Rate
// limit by client IP behind a proxy" below.
//
// httprate.CanonicalizeIP buckets IPv6 clients by their /64 so they can't
// rotate within it to win fresh buckets (see that section for why).
//
// To have a single rate-limiter for all requests, use a constant key:
// httprate.LimitBy(.., httprate.Key("*")).
//
// Please see _example/main.go for more, or read the library code.
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
}))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("."))
})
http.ListenAndServe(":3333", r)
}
Common use cases
Rate limit by client IP behind a proxy
If your app runs behind a reverse proxy, load balancer, or CDN, the request's
RemoteAddr is the proxy, not the client. Resolving the real client IP safely
means deciding which hop to trust — there is no safe default, and trusting a
client-supplied header blindly is exactly the spoofing bug behind the deprecated
LimitByRealIP.
Use chi's middleware.ClientIPFrom*
middlewares (chi v5.3.0+) to resolve a trusted client IP, then rate-limit by
it with a LimitBy KeyFunc that reads it back and canonicalizes it:
import (
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
)
// 1. Resolve a trusted client IP. Pick exactly ONE that matches your
// deployment (see the table below):
r.Use(middleware.ClientIPFromXFF("10.0.0.0/8"))
// 2. Rate-limit by that trusted client IP.
r.Use(httprate.LimitBy(100, time.Minute, clientIPKey))
// clientIPKey is the rate-limit key. middleware.GetClientIP reads the IP
// resolved in step 1; httprate.CanonicalizeIP buckets IPv6 clients by /64.
func clientIPKey(r *http.Request) (string, error) {
return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
}
Note
middleware.GetClientIPreturns the full client IP. Keying on it directly lets an IPv6 client rotate within its own/64(2^64 addresses via SLAAC) to get a fresh bucket per request and bypass the limit.httprate.CanonicalizeIPbuckets IPv6 by/64(IPv4 unchanged) — copy its logic and widen the prefix (e.g./56,/48) if your clients are delegated a larger block. The deprecatedKeyByIP/KeyByRealIPdid this canonicalization for you;CanonicalizeIPkeeps it while making the trust model and prefix your choice.
Pick the one ClientIPFrom* middleware that matches how requests reach you:
| Your setup | Use |
|---|---|
| Directly on the public internet, no proxy | middleware.ClientIPFromRemoteAddr |
Behind nginx (X-Real-IP), Cloudflare (CF-Connecting-IP), Apache (X-Client-IP) |
middleware.ClientIPFromHeader("X-Real-IP") |
| Behind one or more proxies whose IP ranges you can list | middleware.ClientIPFromXFF("10.0.0.0/8", ...) |
| Behind a known, fixed number of proxies with dynamic IPs | middleware.ClientIPFromXFFTrustedProxies(2) |
See chi's Choosing a ClientIP middleware for the full picker.
Important
If no
ClientIPFrom*middleware is installed upstream,middleware.GetClientIPreturns""and every request shares a single global rate-limit bucket. That's strictly more restrictive (not a security hole), but it's a footgun — you'll trip the limit afterrequestLimittotal requests in dev. Make sure exactly oneClientIPFrom*middleware runs before the limiter.
A KeyFunc is just func(r *http.Request) (string, error), so it isn't tied to
chi — read the client IP (or tenant/user ID) from wherever echo, fiber, gin, or
your own middleware stashes it on the request, and return it.
Rate limit by IP and URL path (aka endpoint)
// clientIPKey is the KeyFunc from "Rate limit by client IP behind a proxy" above.
r.Use(httprate.LimitBy(
10, // requests
10*time.Second, // per duration
httprate.JoinKeys(clientIPKey, httprate.KeyByEndpoint),
))
Rate limit by arbitrary keys
r.Use(httprate.LimitBy(
100,
time.Minute,
// an oversimplified example of rate limiting by a custom header
func(r *http.Request) (string, error) {
return r.Header.Get("X-Access-Token"), nil
},
))
Rate limit by request payload
// Rate-limiter for login endpoint.
loginRateLimiter := httprate.NewRateLimiter(5, time.Minute)
r.Post("/login", func(w http.ResponseWriter, r *http.Request) {
var payload struct {
Username string `json:"username"`
Password string `json:"password"`
}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil || payload.Username == "" || payload.Password == "" {
w.WriteHeader(400)
return
}
// Rate-limit login at 5 req/min.
if loginRateLimiter.RespondOnLimit(w, r, payload.Username) {
return
}
w.Write([]byte("login at 5 req/min\n"))
})
Send specific response for rate-limited requests
The default response is HTTP 429 with Too Many Requests body. You can override it with:
r.Use(httprate.LimitBy(
10,
time.Minute,
clientIPKey, // the KeyFunc from "Rate limit by client IP behind a proxy" above
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"error": "Rate-limited. Please, slow down."}`, http.StatusTooManyRequests)
}),
))
Send specific response on errors
An error can be returned by:
- A custom key function provided by
httprate.WithKeyFunc(customKeyFn) - A custom backend provided by
httprateredis.WithRedisLimitCounter(customBackend)- The default local in-memory counter is guaranteed not return any errors
- Backends that fall-back to the local in-memory counter (e.g. httprate-redis) can choose not to return any errors either
r.Use(httprate.LimitBy(
10,
time.Minute,
clientIPKey, // the KeyFunc from "Rate limit by client IP behind a proxy" above
httprate.WithErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {
http.Error(w, fmt.Sprintf(`{"error": %q}`, err), http.StatusPreconditionRequired)
}),
httprate.WithLimitCounter(customBackend),
))
Send custom response headers
r.Use(httprate.LimitBy(
1000,
time.Minute,
clientIPKey, // the KeyFunc from "Rate limit by client IP behind a proxy" above
httprate.WithResponseHeaders(httprate.ResponseHeaders{
Limit: "X-RateLimit-Limit",
Remaining: "X-RateLimit-Remaining",
Reset: "X-RateLimit-Reset",
RetryAfter: "Retry-After",
Increment: "", // omit
}),
))
Omit response headers
r.Use(httprate.LimitBy(
1000,
time.Minute,
clientIPKey, // the KeyFunc from "Rate limit by client IP behind a proxy" above
httprate.WithResponseHeaders(httprate.ResponseHeaders{}),
))
LICENSE
MIT