From 4c924c979bd660fe4c57c1e1933960b262f45887 Mon Sep 17 00:00:00 2001 From: codetheuri Date: Tue, 13 Oct 2026 16:44:34 +0300 Subject: [PATCH 1/3] fix(testdb): take the database lock before migrating ConnectWith migrated first and only then took the advisory lock that serialises test packages. go test runs packages as separate processes against one database, so the first run after a new migration raced: two packages applied it at once and one failed with "column ... already exists". The lock is now taken before the migration, and every failure path after it releases the lock before failing the test. Verified by recreating the test database and running four database packages at once with -p 4. --- pkg/testdb/testdb.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/testdb/testdb.go b/pkg/testdb/testdb.go index 6ccabc0..d393a31 100644 --- a/pkg/testdb/testdb.go +++ b/pkg/testdb/testdb.go @@ -157,9 +157,21 @@ func ConnectWith(t *testing.T, opts Options) *gorm.DB { return nil } - if err := migrator.Up(sqlDB); err != nil { + // Serialise from here, before migrating. go test runs packages as separate + // processes against one database, and migrating is as much a change to the + // shared schema as truncating is: with the lock taken only afterwards, the + // first run after a new migration raced — two packages applied it at once, + // and the loser failed with "column ... already exists". + lockDatabase(t, sqlDB) + fail := func(format string, args ...any) { + t.Helper() + unlockDatabase() _ = sqlDB.Close() - t.Fatalf("failed to migrate test database: %v", err) + t.Fatalf(format, args...) + } + + if err := migrator.Up(sqlDB); err != nil { + fail("failed to migrate test database: %v", err) return nil } @@ -169,8 +181,7 @@ func ConnectWith(t *testing.T, opts Options) *gorm.DB { Logger: logger.Default.LogMode(logger.Silent), }) if err != nil { - _ = sqlDB.Close() - t.Fatalf("failed to open gorm connection: %v", err) + fail("failed to open gorm connection: %v", err) return nil } @@ -178,14 +189,10 @@ func ConnectWith(t *testing.T, opts Options) *gorm.DB { // therefore running the same callbacks production does, which is what makes // them evidence that tenancy stays inert. if err := tenant.Register(gormDB); err != nil { - _ = sqlDB.Close() - t.Fatalf("failed to register tenant callbacks: %v", err) + fail("failed to register tenant callbacks: %v", err) return nil } - // Serialise before the first TRUNCATE, release after the last one. - lockDatabase(t, sqlDB) - Truncate(t, gormDB) t.Cleanup(func() { Truncate(t, gormDB) From a3dc71f34c43a5002e95bc901a51d43802dcfd37 Mon Sep 17 00:00:00 2001 From: codetheuri Date: Mon, 19 Oct 2026 08:36:43 +0300 Subject: [PATCH 2/3] fix(middleware): let handlers reach the connection behind the logger The request logger wrapped the ResponseWriter without Unwrap, so http.ResponseController could not see through it. Every handler behind it (all routes, since app.New installs it) was stuck with the server's WriteTimeout and could not flush. Salio's APK downloads, which take half an hour on a slow connection, would be cut at 60 seconds. Unwrap returns the underlying writer. A test checks that SetWriteDeadline and Flush work through the logger; both failed before. --- pkg/middleware/logger.go | 9 +++++++ pkg/middleware/logger_test.go | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 pkg/middleware/logger_test.go diff --git a/pkg/middleware/logger.go b/pkg/middleware/logger.go index 34ba114..b541b00 100755 --- a/pkg/middleware/logger.go +++ b/pkg/middleware/logger.go @@ -48,3 +48,12 @@ func (lrw *loggingResponseWriter) WriteHeader(code int) { lrw.statusCode = code lrw.ResponseWriter.WriteHeader(code) } + +// Unwrap returns the writer underneath, so http.ResponseController can reach +// the connection through this middleware: extend a write deadline, flush, +// take over the connection. Without it every handler behind the logger is +// stuck with the server's WriteTimeout, and a long download (an APK over a +// slow connection takes half an hour) is cut off part way. +func (lrw *loggingResponseWriter) Unwrap() http.ResponseWriter { + return lrw.ResponseWriter +} diff --git a/pkg/middleware/logger_test.go b/pkg/middleware/logger_test.go new file mode 100644 index 0000000..a155db7 --- /dev/null +++ b/pkg/middleware/logger_test.go @@ -0,0 +1,46 @@ +package middleware + +import ( + "io" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +// A handler behind the logger must still be able to extend its own write +// deadline: http.ResponseController finds the connection through Unwrap. +func TestLoggerLetsHandlersReachTheConnection(t *testing.T) { + var deadlineErr, flushErr error + handler := Logger(newDummyLogger())(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rc := http.NewResponseController(w) + deadlineErr = rc.SetWriteDeadline(time.Now().Add(time.Hour)) + _, _ = io.WriteString(w, "ok") + flushErr = rc.Flush() + })) + + srv := httptest.NewServer(handler) + defer srv.Close() + res, err := http.Get(srv.URL) + if err != nil { + t.Fatal(err) + } + res.Body.Close() + + if deadlineErr != nil { + t.Errorf("SetWriteDeadline through the logger: %v", deadlineErr) + } + if flushErr != nil { + t.Errorf("Flush through the logger: %v", flushErr) + } +} + +func TestLoggerRecordsTheStatus(t *testing.T) { + rec := httptest.NewRecorder() + Logger(newDummyLogger())(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusTeapot) + })).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil)) + if rec.Code != http.StatusTeapot { + t.Fatalf("got %d", rec.Code) + } +} From d262afcdcd193f28d05807b511bcf6f1eb9320db Mon Sep 17 00:00:00 2001 From: codetheuri Date: Mon, 26 Oct 2026 15:41:28 +0300 Subject: [PATCH 3/3] fix(middleware)!: make the rate limiter limit It keyed on RemoteAddr with its port, new on every connection, and on X-Forwarded-For from anyone, which a client sets freely: each request could arrive as a new caller with a full bucket. A caller is now its address without the port (IPv6 by /64); X-Forwarded-For is read only from TRUSTED_PROXIES, right to left past trusted hops; the table is capped. Adds ClientIP, ParseTrustedProxies and RateLimiter.Allow(key). BREAKING CHANGE: behind a reverse proxy, set TRUSTED_PROXIES, or every client shares the proxy's allowance. --- .env.example | 5 + CHANGELOG.md | 9 ++ config/config.go | 8 + pkg/app/app.go | 7 +- pkg/middleware/ratelimit.go | 242 +++++++++++++++++++++++++------ pkg/middleware/ratelimit_test.go | 165 +++++++++++++++++++++ 6 files changed, 390 insertions(+), 46 deletions(-) diff --git a/.env.example b/.env.example index 63eaa66..7a1451b 100755 --- a/.env.example +++ b/.env.example @@ -60,6 +60,11 @@ MAX_REQUEST_BODY_BYTES=10485760 # Token bucket: BURST is the capacity, RPS the sustained refill rate. RATE_LIMIT_BURST=30 RATE_LIMIT_RPS=10 +# Who may say who the client is (X-Forwarded-For): the reverse proxy, or the +# private network the service is published on. Empty trusts nobody, which is +# right when clients connect directly; behind a proxy it makes every client +# share the proxy's one allowance. +TRUSTED_PROXIES= # --- Logging ------------------------------------------------------------------ LOG_LEVEL=debug diff --git a/CHANGELOG.md b/CHANGELOG.md index 508e7f2..785e5d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Security +- **The rate limiter now limits.** It keyed on the client's address *with its port*, which every new connection changes, and believed `X-Forwarded-For` from anyone, which a client can set to anything: either way each request could arrive as a new caller with a full bucket, so password guessing went unthrottled. A caller is now its address without the port (an IPv6 address by its /64), and `X-Forwarded-For` is read only from a trusted proxy, right to left, past every trusted hop. The table is capped (`DefaultMaxClients`, `WithMaxClients`); when full, callers whose bucket has refilled are dropped and new ones share one strict bucket. + - **Behaviour change:** set `TRUSTED_PROXIES` (addresses and CIDR ranges) when running behind a reverse proxy. Left empty, nobody is trusted and every client shares the proxy's one allowance. + - New: `middleware.ClientIP`, `ParseTrustedProxies`, `WithTrustedProxies`, and `RateLimiter.Allow(key)` for limits on something other than an address, such as an account. + +### Fixed +- **`internal/testdb`** takes the database lock before migrating, so parallel test packages no longer race on the schema. +- **The logger middleware's response writer** exposes `Unwrap`, so handlers reach the connection behind it (`http.ResponseController`). + ### Added - **Code-First RBAC Engine (`pkg/authz`)**: Multi-tenant-ready Role-Based Access Control system with permission constants, global registry, and runtime GORM DB synchronization. - **Huma v2 Integration**: Strongly-typed HTTP API framework built on Chi with automated OpenAPI 3.0 specification (`/openapi.json`) and interactive documentation UI (`/docs`). diff --git a/config/config.go b/config/config.go index 3387230..5d7fa1a 100755 --- a/config/config.go +++ b/config/config.go @@ -46,6 +46,13 @@ type Config struct { RateLimitBurst float64 // bucket capacity — how large a spike is tolerated RateLimitRPS float64 // sustained refill rate, requests per second + // TrustedProxies lists the addresses and CIDR ranges allowed to say who + // the client is in X-Forwarded-For, comma-separated. Empty means nobody: + // the connection's own address is the client. Behind a reverse proxy, + // name it (or the private network the service is published on), or + // every client shares the proxy's one allowance. + TrustedProxies string + // DocsEnabled controls whether /docs and /openapi.json are served. // Defaults to false in production. API endpoints are unaffected either way. DocsEnabled bool @@ -195,6 +202,7 @@ func LoadConfig() (*Config, error) { // --- Rate limiting --- cfg.RateLimitBurst = float64(envInt("RATE_LIMIT_BURST", 30)) cfg.RateLimitRPS = float64(envInt("RATE_LIMIT_RPS", 10)) + cfg.TrustedProxies = os.Getenv("TRUSTED_PROXIES") // --- API documentation --- // Off by default in production; on by default everywhere else. An explicit diff --git a/pkg/app/app.go b/pkg/app/app.go index dc3b2a8..5133b50 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -109,7 +109,12 @@ func New(opts Options) (*App, error) { // Recovery above the handlers so a panic becomes a 500 rather than killing the // process; rate limiting and the body cap before any handler allocates memory // on behalf of an unauthenticated caller. - rateLimiter := middleware.NewRateLimiter(cfg.RateLimitBurst, cfg.RateLimitRPS, log) + trusted, err := middleware.ParseTrustedProxies(cfg.TrustedProxies) + if err != nil { + return nil, fmt.Errorf("TRUSTED_PROXIES: %w", err) + } + rateLimiter := middleware.NewRateLimiter(cfg.RateLimitBurst, cfg.RateLimitRPS, log, + middleware.WithTrustedProxies(trusted)) r.Use(middleware.RequestID()) r.Use(middleware.Logger(log)) diff --git a/pkg/middleware/ratelimit.go b/pkg/middleware/ratelimit.go index 9ce85f5..bf69b4c 100644 --- a/pkg/middleware/ratelimit.go +++ b/pkg/middleware/ratelimit.go @@ -1,37 +1,83 @@ package middleware import ( + "fmt" + "net" "net/http" + "net/netip" + "strings" "sync" "time" "github.com/codetheuri/tusk/v2/pkg/logger" ) +// DefaultMaxClients bounds how many callers a RateLimiter tracks at once. +// Each costs about a hundred bytes, so the default is around 10 MB at most. +const DefaultMaxClients = 100_000 + +// overflowKey is the one bucket every new caller shares while the table is +// full of callers still inside their limits. Strict on purpose: a flood of +// fresh addresses gets one allowance between them, not one each. +const overflowKey = "\x00overflow" + type clientVisitor struct { - tokens float64 - lastSeen time.Time - maxTokens float64 - refillRate float64 // tokens per second + tokens float64 + lastSeen time.Time } -// RateLimiter provides IP-based token bucket rate limiting for endpoints. +// RateLimiter is a token bucket per caller: maxTokens is how large a burst +// is allowed, refillRate how many requests per second are sustained. +// +// A caller is its IP address without the port (an IPv6 address by its /64, +// which one device can roam within freely). X-Forwarded-For is believed only +// from a trusted proxy (WithTrustedProxies); without one, the connection's +// own address is the caller. Earlier versions keyed on the address with its +// port, which every new connection changes, and on X-Forwarded-For as sent +// by anyone, which a client can set to anything: either way each request +// could arrive as a new caller with a full bucket. type RateLimiter struct { mu sync.Mutex visitors map[string]*clientVisitor maxTokens float64 refillRate float64 + maxClients int + trusted []netip.Prefix log logger.Logger } -// NewRateLimiter creates a RateLimiter with specified max tokens (capacity) and refill rate per second. -func NewRateLimiter(maxTokens float64, refillRate float64, log logger.Logger) *RateLimiter { +// RateLimitOption configures a RateLimiter. +type RateLimitOption func(*RateLimiter) + +// WithTrustedProxies names the proxies whose X-Forwarded-For is believed. +// Only addresses that can reach the service directly belong here: a proxy +// in front of it, or the private network it is published on. +func WithTrustedProxies(prefixes []netip.Prefix) RateLimitOption { + return func(rl *RateLimiter) { rl.trusted = prefixes } +} + +// WithMaxClients changes DefaultMaxClients. +func WithMaxClients(n int) RateLimitOption { + return func(rl *RateLimiter) { + if n > 0 { + rl.maxClients = n + } + } +} + +// NewRateLimiter creates a RateLimiter with specified max tokens (capacity) +// and refill rate per second. +func NewRateLimiter(maxTokens float64, refillRate float64, log logger.Logger, opts ...RateLimitOption) *RateLimiter { rl := &RateLimiter{ visitors: make(map[string]*clientVisitor), maxTokens: maxTokens, refillRate: refillRate, + maxClients: DefaultMaxClients, log: log, } + for _, opt := range opts { + opt(rl) + } // Periodically clean up stale visitors (older than 10 minutes) go func() { @@ -50,54 +96,160 @@ func NewRateLimiter(maxTokens float64, refillRate float64, log logger.Logger) *R return rl } -// Limit returns a middleware that limits incoming HTTP requests per remote IP. -func (rl *RateLimiter) Limit() func(http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ip := r.RemoteAddr - if forwardFor := r.Header.Get("X-Forwarded-For"); forwardFor != "" { - ip = forwardFor - } +// Allow takes one request from key's bucket and reports whether there was +// one to take. The middleware calls it with the caller's address; a service +// can call it with anything else worth limiting, such as an account. +func (rl *RateLimiter) Allow(key string) bool { + rl.mu.Lock() + defer rl.mu.Unlock() - rl.mu.Lock() - v, exists := rl.visitors[ip] - now := time.Now() - - if !exists { - v = &clientVisitor{ - tokens: rl.maxTokens - 1, - lastSeen: now, - maxTokens: rl.maxTokens, - refillRate: rl.refillRate, - } - rl.visitors[ip] = v - rl.mu.Unlock() - next.ServeHTTP(w, r) - return - } + now := time.Now() + v, ok := rl.visitors[key] + if !ok { + if len(rl.visitors) >= rl.maxClients { + rl.dropRefilled(now) + } + if len(rl.visitors) >= rl.maxClients { + key = overflowKey + v, ok = rl.visitors[key] + } + if !ok { + v = &clientVisitor{tokens: rl.maxTokens, lastSeen: now} + rl.visitors[key] = v + } + } - // Refill tokens based on elapsed time - elapsed := now.Sub(v.lastSeen).Seconds() - v.tokens += elapsed * v.refillRate - if v.tokens > v.maxTokens { - v.tokens = v.maxTokens - } - v.lastSeen = now + v.tokens += now.Sub(v.lastSeen).Seconds() * rl.refillRate + if v.tokens > rl.maxTokens { + v.tokens = rl.maxTokens + } + v.lastSeen = now + if v.tokens < 1 { + return false + } + v.tokens-- + return true +} + +// dropRefilled forgets callers whose bucket has filled up again: tracking +// them changes nothing, as a new caller starts full. Called with mu held. +func (rl *RateLimiter) dropRefilled(now time.Time) { + for key, v := range rl.visitors { + if key == overflowKey { + continue + } + if v.tokens+now.Sub(v.lastSeen).Seconds()*rl.refillRate >= rl.maxTokens { + delete(rl.visitors, key) + } + } +} - if v.tokens < 1 { - rl.mu.Unlock() - rl.log.Warn("Rate limit exceeded", "ip", ip, "path", r.URL.Path) +// Limit returns a middleware that limits incoming HTTP requests per caller. +func (rl *RateLimiter) Limit() func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ip := ClientIP(r, rl.trusted) + if !rl.Allow(callerKey(ip, r.RemoteAddr)) { + rl.log.Warn("Rate limit exceeded", "ip", ip.String(), "path", r.URL.Path) w.Header().Set("Content-Type", "application/json") w.Header().Set("Retry-After", "60") w.WriteHeader(http.StatusTooManyRequests) w.Write([]byte(`{"success":false,"message":"Too many requests. Please try again later."}`)) return } - - v.tokens -= 1 - rl.mu.Unlock() - next.ServeHTTP(w, r) }) } } + +// ClientIP is the address of whoever made r. When the connection comes from +// a trusted proxy, X-Forwarded-For is read from the right, past every +// trusted hop, to the first address none of them is: the one the nearest +// trusted proxy saw connect. Entries left of it are whatever the client +// sent, and are never believed. Returns the zero Addr if no address can be +// read at all. +func ClientIP(r *http.Request, trusted []netip.Prefix) netip.Addr { + peer := parseAddr(r.RemoteAddr) + if !peer.IsValid() || !isTrusted(peer, trusted) { + return peer + } + hops := r.Header.Values("X-Forwarded-For") + for i := len(hops) - 1; i >= 0; i-- { + parts := strings.Split(hops[i], ",") + for j := len(parts) - 1; j >= 0; j-- { + addr := parseAddr(strings.TrimSpace(parts[j])) + if !addr.IsValid() { + // Garbage from a client, past which nothing can be trusted: + // the last good hop is the best answer there is. + return peer + } + if !isTrusted(addr, trusted) { + return addr + } + peer = addr + } + } + return peer +} + +// ParseTrustedProxies reads a comma-separated list of addresses and CIDR +// ranges, such as "172.16.0.0/12, 127.0.0.1". +func ParseTrustedProxies(list string) ([]netip.Prefix, error) { + var out []netip.Prefix + for _, item := range strings.Split(list, ",") { + item = strings.TrimSpace(item) + if item == "" { + continue + } + if strings.Contains(item, "/") { + p, err := netip.ParsePrefix(item) + if err != nil { + return nil, fmt.Errorf("trusted proxy %q: %w", item, err) + } + out = append(out, p.Masked()) + continue + } + a, err := netip.ParseAddr(item) + if err != nil { + return nil, fmt.Errorf("trusted proxy %q: %w", item, err) + } + a = a.Unmap() + out = append(out, netip.PrefixFrom(a, a.BitLen())) + } + return out, nil +} + +// callerKey groups an IPv6 address by its /64. An unreadable address falls +// back to the raw RemoteAddr, port and all, rather than sharing one bucket +// with every other unreadable caller. +func callerKey(ip netip.Addr, remoteAddr string) string { + if !ip.IsValid() { + return remoteAddr + } + if ip.Is6() { + p, _ := ip.Prefix(64) + return p.String() + } + return ip.String() +} + +// parseAddr reads "1.2.3.4", "1.2.3.4:5678", "::1" or "[::1]:5678". +func parseAddr(s string) netip.Addr { + if host, _, err := net.SplitHostPort(s); err == nil { + s = host + } + a, err := netip.ParseAddr(s) + if err != nil { + return netip.Addr{} + } + return a.Unmap() +} + +func isTrusted(a netip.Addr, trusted []netip.Prefix) bool { + for _, p := range trusted { + if p.Contains(a) { + return true + } + } + return false +} diff --git a/pkg/middleware/ratelimit_test.go b/pkg/middleware/ratelimit_test.go index 0ff7d44..f5fa97c 100644 --- a/pkg/middleware/ratelimit_test.go +++ b/pkg/middleware/ratelimit_test.go @@ -3,7 +3,9 @@ package middleware import ( "net/http" "net/http/httptest" + "net/netip" "testing" + "time" ) func TestRateLimiter(t *testing.T) { @@ -40,3 +42,166 @@ func TestRateLimiter(t *testing.T) { t.Errorf("req 3 expected 429 Too Many Requests, got %d", rec3.Code) } } + +// limited serves through a limiter of burst 1 that never refills, so the +// second request from one caller is refused. +func limited(t *testing.T, opts ...RateLimitOption) func(remote string, xff ...string) int { + t.Helper() + handler := NewRateLimiter(1, 0, newDummyLogger(), opts...).Limit()( + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) + return func(remote string, xff ...string) int { + req := httptest.NewRequest("POST", "/api/v1/auth/login", nil) + req.RemoteAddr = remote + for _, h := range xff { + req.Header.Add("X-Forwarded-For", h) + } + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, req) + return rec.Code + } +} + +func mustProxies(t *testing.T, list string) []netip.Prefix { + t.Helper() + p, err := ParseTrustedProxies(list) + if err != nil { + t.Fatal(err) + } + return p +} + +// Each new connection has a new port; keying on it gave every connection a +// full bucket. +func TestRateLimiterIgnoresThePort(t *testing.T) { + do := limited(t) + if got := do("41.90.1.2:50001"); got != http.StatusOK { + t.Fatalf("first: %d", got) + } + if got := do("41.90.1.2:50002"); got != http.StatusTooManyRequests { + t.Fatalf("same address, new port: got %d, want 429", got) + } +} + +// Without a trusted proxy, X-Forwarded-For is whatever the client sent. +func TestRateLimiterIgnoresForwardedForFromAnyone(t *testing.T) { + do := limited(t) + do("41.90.1.2:50001", "1.1.1.1") + if got := do("41.90.1.2:50002", "2.2.2.2"); got != http.StatusTooManyRequests { + t.Fatalf("a made-up X-Forwarded-For bought a new bucket: got %d", got) + } +} + +// Behind the proxy, each client is its own caller, and what a client puts in +// front of the proxy's entry changes nothing. +func TestRateLimiterBehindATrustedProxy(t *testing.T) { + do := limited(t, WithTrustedProxies(mustProxies(t, "172.16.0.0/12"))) + const proxy = "172.20.0.5:40000" + + if got := do(proxy, "41.90.1.2"); got != http.StatusOK { + t.Fatalf("client A: %d", got) + } + if got := do(proxy, "41.90.9.9"); got != http.StatusOK { + t.Fatalf("client B is a separate caller: %d", got) + } + if got := do(proxy, "6.6.6.6, 41.90.1.2"); got != http.StatusTooManyRequests { + t.Fatalf("client A with a forged entry in front: got %d, want 429", got) + } + // The same, spread over two headers. + if got := do(proxy, "7.7.7.7", "41.90.1.2"); got != http.StatusTooManyRequests { + t.Fatalf("client A with a forged header in front: got %d, want 429", got) + } +} + +func TestClientIP(t *testing.T) { + trusted := mustProxies(t, "10.0.0.0/8, 172.20.0.40") + cases := []struct { + name, remote string + xff []string + want string + }{ + {"direct", "41.90.1.2:1", nil, "41.90.1.2"}, + {"direct, header ignored", "41.90.1.2:1", []string{"1.1.1.1"}, "41.90.1.2"}, + {"one trusted hop", "172.20.0.40:1", []string{"41.90.1.2"}, "41.90.1.2"}, + {"two trusted hops", "10.0.0.1:1", []string{"9.9.9.9, 41.90.1.2, 172.20.0.40"}, "41.90.1.2"}, + {"trusted, no header", "172.20.0.40:1", nil, "172.20.0.40"}, + {"garbage stops the walk", "172.20.0.40:1", []string{"41.90.1.2, nonsense"}, "172.20.0.40"}, + {"ipv6 with port", "[2001:db8::1]:443", nil, "2001:db8::1"}, + {"ipv4-mapped", "[::ffff:41.90.1.2]:1", nil, "41.90.1.2"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + req := httptest.NewRequest("GET", "/", nil) + req.RemoteAddr = c.remote + for _, h := range c.xff { + req.Header.Add("X-Forwarded-For", h) + } + if got := ClientIP(req, trusted).String(); got != c.want { + t.Fatalf("got %s, want %s", got, c.want) + } + }) + } +} + +// One device can take any address in its /64. +func TestRateLimiterGroupsIPv6ByItsSlash64(t *testing.T) { + do := limited(t) + do("[2001:db8:1:2::1]:1") + if got := do("[2001:db8:1:2:ffff::9]:1"); got != http.StatusTooManyRequests { + t.Fatalf("same /64: got %d, want 429", got) + } + if got := do("[2001:db8:1:3::1]:1"); got != http.StatusOK { + t.Fatalf("another /64: got %d", got) + } +} + +// A flood of fresh addresses cannot grow the table without bound, nor buy a +// bucket each once it is full. +func TestRateLimiterCapsWhatItTracks(t *testing.T) { + rl := NewRateLimiter(1, 0, newDummyLogger(), WithMaxClients(2)) + if !rl.Allow("a") || !rl.Allow("b") { + t.Fatal("the first two callers") + } + if !rl.Allow("c") { + t.Fatal("the first caller past the cap takes the shared bucket") + } + if rl.Allow("d") { + t.Fatal("the next new caller shares that same, now empty, bucket") + } + if n := len(rl.visitors); n > 3 { + t.Fatalf("tracking %d callers, want at most the cap and the shared bucket", n) + } +} + +// A caller whose bucket has filled up again is the same as a new one, so it +// makes room for new callers before anyone is sent to the shared bucket. +func TestRateLimiterDropsRefilledCallersWhenFull(t *testing.T) { + rl := NewRateLimiter(1, 1000, newDummyLogger(), WithMaxClients(1)) + rl.Allow("a") + time.Sleep(5 * time.Millisecond) // a's bucket refills + rl.Allow("b") + if _, ok := rl.visitors[overflowKey]; ok { + t.Fatal("b was sent to the shared bucket although a had refilled") + } +} + +func TestParseTrustedProxies(t *testing.T) { + got, err := ParseTrustedProxies(" 172.16.0.0/12, 127.0.0.1 ,,::1") + if err != nil { + t.Fatal(err) + } + want := []string{"172.16.0.0/12", "127.0.0.1/32", "::1/128"} + if len(got) != len(want) { + t.Fatalf("got %v", got) + } + for i := range want { + if got[i].String() != want[i] { + t.Fatalf("got %v, want %v", got, want) + } + } + if _, err := ParseTrustedProxies("172.16.0.0/40"); err == nil { + t.Fatal("a bad range was accepted") + } + if _, err := ParseTrustedProxies("proxy.local"); err == nil { + t.Fatal("a name was accepted") + } +}