Files
mirror-3x-ui/internal/util/clientip/clientip_test.go
T
amirim f9e15a6b9b feat(sub): opt-in per-client rate limit for the subscription server (#6966)
A loop probing subscription ids, or one misbehaving app, could turn the subscription server into a database load generator, and there was nothing to cap it.

Add a setting, off by default, that limits how many subscription fetches one client address may make per minute (60 by default, 1 to 1000); requests over it get 429 with Retry-After. Static assets stay outside the guard. The client address comes from the same resolution the panel's login limiter uses, now shared in internal/util/clientip, so trustedProxyCIDRs decides it in one place for the panel and the subscription server.
2026-11-24 20:51:32 +03:30

31 lines
2.1 KiB
Go

package clientip
import "testing"
func TestResolve(t *testing.T) {
tests := []struct {
name string
remoteAddr, xRealIP, xff string
trusted string
want string
}{
{name: "untrusted peer cannot set headers", remoteAddr: "203.0.113.7:4434", xRealIP: "8.8.8.8", xff: "9.9.9.9", trusted: "127.0.0.1/32", want: "203.0.113.7"},
{name: "first untrusted hop behind two proxies", remoteAddr: "127.0.0.1:5555", xff: "203.0.113.9, 10.1.1.1", trusted: "127.0.0.1/32,10.0.0.0/8", want: "203.0.113.9"},
{name: "client-sent leftmost entry is ignored", remoteAddr: "127.0.0.1:5555", xff: "6.6.6.6, 203.0.113.9, 10.1.1.1", trusted: "127.0.0.1/32,10.0.0.0/8", want: "203.0.113.9"},
{name: "real IP agreeing with the chain", remoteAddr: "127.0.0.1:5555", xRealIP: "198.51.100.4", xff: "198.51.100.4", trusted: "127.0.0.1/32", want: "198.51.100.4"},
{name: "real IP contradicting the chain falls back to the proxy", remoteAddr: "127.0.0.1:5555", xRealIP: "6.6.6.6", xff: "198.51.100.4", trusted: "127.0.0.1/32", want: "127.0.0.1"},
{name: "real IP alone from a trusted proxy", remoteAddr: "127.0.0.1:5555", xRealIP: "198.51.100.4", trusted: "127.0.0.1/32", want: "198.51.100.4"},
{name: "malformed headers fall back to the proxy", remoteAddr: "127.0.0.1:5555", xRealIP: "also-garbage", xff: "garbage", trusted: "127.0.0.1/32", want: "127.0.0.1"},
{name: "bare address in the trusted list", remoteAddr: "[::1]:5555", xff: "198.51.100.4", trusted: "127.0.0.1/32, ::1", want: "198.51.100.4"},
{name: "unparseable trusted entry is skipped", remoteAddr: "10.2.3.4:5555", xff: "198.51.100.4", trusted: "not-an-ip,10.0.0.0/8", want: "198.51.100.4"},
{name: "unparseable peer has no identity", remoteAddr: "not-an-address", trusted: "127.0.0.1/32", want: "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Resolve(tt.remoteAddr, tt.xRealIP, tt.xff, tt.trusted); got != tt.want {
t.Fatalf("Resolve(%q, %q, %q, %q) = %q, want %q", tt.remoteAddr, tt.xRealIP, tt.xff, tt.trusted, got, tt.want)
}
})
}
}