Files
OwnCord/Server/api/clientip_test.go
T
J3vbandClaude Fable 5 d880b64d64 test: audit 2026-08-19 — fix stale tests, close coverage gaps (#1397)
* test(server): admin/handlers/channels — test-audit 2026-08-19 fixes

* test(server): api/constants — test-audit 2026-08-19 fixes

* test(server): api/middleware — test-audit 2026-08-19 fixes

* test(server): api/waf — test-audit 2026-08-19 fixes

* test(server): auth/totp/encrypt — test-audit 2026-08-19 fixes

* test(server): db/session/expiry/test — test-audit 2026-08-19 fixes

* test(server): migrations/030/attachments/unlink/on/message/delete — test-audit 2026-08-19 fixes

* test(server): updater/download — test-audit 2026-08-19 fixes

* test(server): ws/handlers_command — test-audit 2026-08-19 fixes

* test(server): ws/hub/broadcast — test-audit 2026-08-19 fixes

* test(server): ws/hub/events — test-audit 2026-08-19 fixes

* test(server): ws/livekit/webhook — test-audit 2026-08-19 fixes

* test(server): ws/voice/controls — test-audit 2026-08-19 fixes

* test(server): ws/voice/join — test-audit 2026-08-19 fixes

* test(server): ws/voice/moderation — test-audit 2026-08-19 fixes

* test(rust): src-tauri/src/commands.rs — test-audit 2026-08-19 fixes

* test(rust): src-tauri/src/secret_store.rs — test-audit 2026-08-19 fixes

* test(rust): src-tauri/src/update_commands.rs — test-audit 2026-08-19 fixes

* test(client): src/components/ChannelSidebar.ts — test-audit 2026-08-19 fixes

* test(client): src/lib/ws.ts — test-audit 2026-08-19 fixes

* test(rust): src-tauri/src/credentials.rs — test-audit 2026-08-19 fixes

* test(rust): src-tauri/src/tofu.rs — test-audit 2026-08-19 fixes

* test(client): src/lib/hostValidation.ts — test-audit 2026-08-19 fixes

* test(client): src/lib/rate-limiter.ts — test-audit 2026-08-19 fixes

* test(client): src/pages/connect-page/LoginForm.ts — test-audit 2026-08-19 fixes

* test(client): src/pages/main-page/SidebarArea.ts — test-audit 2026-08-19 fixes

* test(client): src/stores/voice.store.ts — test-audit 2026-08-19 fixes

* test(client): tests/browser/smoke.test.ts — test-audit 2026-08-19 fixes

* test(client): tests/unit/media.test.ts — test-audit 2026-08-19 fixes

* test(client): tests/unit/renderers.test.ts — test-audit 2026-08-19 fixes

* test(client): src/components/UserProfilePopup.ts — test-audit 2026-08-19 fixes

* test(client): src/lib/e2eeCrypto.ts — test-audit 2026-08-19 fixes

* test(client): tests/unit/log-persistence.test.ts — test-audit 2026-08-19 fixes

* test(client): keep tests/browser out of the jsdom suite and run it in CI

* test(server): ws/hub_broadcast_test.go — bytes.Equal payload compare (gocritic)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(client): src/lib/credentials.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/dispatcher.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/permissions.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/rate-limiter.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/hostValidation.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/stores/messages.store.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/e2eeCrypto.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/ws.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/identity.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/lib/livekitE2EE.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/stores/auth.store.ts — test-audit 2026-08-19 round 2 (Stryker)

* test(client): src/stores/voice.store.ts — test-audit 2026-08-19 round 2 (Stryker)

* docs: test audit 2026-08-19 — findings, fixes, measured baselines

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(graph): refresh the knowledge graph after the 2026-08-19 test audit

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 19:29:31 +02:00

236 lines
8.2 KiB
Go

package api
// White-box tests for clientIP and the trusted-proxy CIDR matching
// (parseCIDRList + ipInNets — the W3-3a replacement for isTrustedProxy).
// These live in package api (not api_test) so they can reach unexported symbols.
import (
"net/http"
"net/http/httptest"
"testing"
)
// ─── parseCIDRList + ipInNets ────────────────────────────────────────────────
// inCIDRs is the test shorthand for the old isTrustedProxy semantics: does ip
// fall inside any of the (string) CIDRs?
func inCIDRs(ip string, cidrs []string) bool {
return ipInNets(ip, parseCIDRList(cidrs))
}
func TestIPInNets_EmptyList_ReturnsFalse(t *testing.T) {
if inCIDRs("10.0.0.1", nil) {
t.Error("ipInNets(empty list) = true, want false")
}
}
func TestIPInNets_ExactIPMatch(t *testing.T) {
if !inCIDRs("10.0.0.1", []string{"10.0.0.1/32"}) {
t.Error("ipInNets exact match = false, want true")
}
}
func TestIPInNets_CIDRMatch(t *testing.T) {
if !inCIDRs("192.168.1.50", []string{"192.168.1.0/24"}) {
t.Error("ipInNets CIDR match = false, want true")
}
}
func TestIPInNets_CIDRNoMatch(t *testing.T) {
if inCIDRs("10.9.9.9", []string{"192.168.1.0/24"}) {
t.Error("ipInNets CIDR non-match = true, want false")
}
}
func TestIPInNets_MultipleCIDRs_FirstMatches(t *testing.T) {
if !inCIDRs("10.0.0.5", []string{"172.16.0.0/12", "10.0.0.0/8"}) {
t.Error("ipInNets multi-CIDR first match = false, want true")
}
}
func TestIPInNets_MultipleCIDRs_NoneMatch(t *testing.T) {
if inCIDRs("8.8.8.8", []string{"10.0.0.0/8", "192.168.0.0/16"}) {
t.Error("ipInNets multi-CIDR no match = true, want false")
}
}
func TestParseCIDRList_InvalidCIDR_Skipped(t *testing.T) {
// Invalid entries are skipped (with a startup warning) — they never match,
// so a fully invalid list grants nothing (fail closed at the call sites).
if nets := parseCIDRList([]string{"not-a-cidr"}); len(nets) != 0 {
t.Errorf("parseCIDRList(invalid) = %d nets, want 0", len(nets))
}
if inCIDRs("10.0.0.1", []string{"not-a-cidr"}) {
t.Error("invalid CIDR matched an IP, want no match")
}
}
func TestParseCIDRList_BarePlainIP_SkippedNotPanic(t *testing.T) {
// Bare IP without mask is not valid CIDR notation — skipped, no panic.
if nets := parseCIDRList([]string{"10.0.0.1"}); len(nets) != 0 {
t.Errorf("parseCIDRList(bare IP) = %d nets, want 0", len(nets))
}
}
func TestParseCIDRList_MixedValidInvalid_KeepsValid(t *testing.T) {
nets := parseCIDRList([]string{"not-a-cidr", "10.0.0.0/8"})
if len(nets) != 1 {
t.Fatalf("parseCIDRList(mixed) = %d nets, want 1", len(nets))
}
if !ipInNets("10.1.2.3", nets) {
t.Error("valid entry from mixed list did not match")
}
}
func TestIPInNets_IPv6Match(t *testing.T) {
if !inCIDRs("::1", []string{"::1/128"}) {
t.Error("ipInNets IPv6 exact match = false, want true")
}
}
// ─── clientIP with trusted proxies ───────────────────────────────────────────
func TestClientIP_NoTrustedProxies_UsesRemoteAddr(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "203.0.113.5:4321"
req.Header.Set("X-Real-IP", "1.2.3.4")
req.Header.Set("X-Forwarded-For", "1.2.3.4")
ip := clientIPWithProxies(req, nil)
if ip != "203.0.113.5" {
t.Errorf("clientIP no trusted proxies = %q, want %q", ip, "203.0.113.5")
}
}
func TestClientIP_TrustedProxy_UsesXRealIP(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.1:9999"
req.Header.Set("X-Real-IP", "203.0.113.42")
ip := clientIPWithProxies(req, parseCIDRList([]string{"10.0.0.0/8"}))
if ip != "203.0.113.42" {
t.Errorf("clientIP trusted proxy = %q, want %q", ip, "203.0.113.42")
}
}
func TestClientIP_TrustedProxy_NoXRealIP_FallsBackToRemoteAddr(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.1:9999"
// No X-Real-IP header set.
ip := clientIPWithProxies(req, parseCIDRList([]string{"10.0.0.0/8"}))
if ip != "10.0.0.1" {
t.Errorf("clientIP trusted proxy no header = %q, want %q", ip, "10.0.0.1")
}
}
func TestClientIP_UntrustedSource_IgnoresXRealIP(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "8.8.8.8:12345"
req.Header.Set("X-Real-IP", "192.168.1.1") // attacker-supplied
ip := clientIPWithProxies(req, parseCIDRList([]string{"10.0.0.0/8"}))
// Must use RemoteAddr, not the forged X-Real-IP.
if ip != "8.8.8.8" {
t.Errorf("clientIP untrusted source = %q, want %q", ip, "8.8.8.8")
}
}
func TestClientIP_XForwardedFor_UsedWhenNoXRealIP(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.1:9999"
req.Header.Set("X-Forwarded-For", "203.0.113.10, 10.0.0.1")
// No X-Real-IP; X-Forwarded-For first entry should be used.
ip := clientIPWithProxies(req, parseCIDRList([]string{"10.0.0.0/8"}))
if ip != "203.0.113.10" {
t.Errorf("clientIP X-Forwarded-For = %q, want %q", ip, "203.0.113.10")
}
}
// TestClientIP_BroadTrustedCIDRKeepsClientsDistinct locks the W2-5 fix: with
// a trusted_proxies range broad enough to cover the clients themselves, the
// right-to-left walk exhausts; falling back to RemoteAddr would collapse
// every client into the proxy's own bucket (one user's failed logins would
// lock out everyone). The leftmost valid XFF entry keeps clients distinct.
func TestClientIP_BroadTrustedCIDRKeepsClientsDistinct(t *testing.T) {
trusted := parseCIDRList([]string{"10.0.0.0/8"}) // covers proxy AND LAN clients
newReq := func(xff string) *http.Request {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.2:9999" // the proxy
req.Header.Set("X-Forwarded-For", xff)
return req
}
ip1 := clientIPWithProxies(newReq("10.5.1.7"), trusted)
ip2 := clientIPWithProxies(newReq("10.5.1.8"), trusted)
if ip1 != "10.5.1.7" || ip2 != "10.5.1.8" {
t.Fatalf("clients behind broad trusted CIDR collapsed: ip1=%q ip2=%q", ip1, ip2)
}
// Multi-hop: leftmost valid entry (furthest upstream) wins on exhaustion.
ip3 := clientIPWithProxies(newReq("10.5.1.9, 10.0.0.3"), trusted)
if ip3 != "10.5.1.9" {
t.Fatalf("expected furthest-upstream entry, got %q", ip3)
}
}
// TestClientIP_SpoofedXFFFromUntrustedRemoteIgnored: an untrusted connecting
// address never gets its forwarded headers honoured, exhaustion fallback or
// not.
func TestClientIP_SpoofedXFFFromUntrustedRemoteIgnored(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "203.0.113.9:1234"
req.Header.Set("X-Forwarded-For", "10.5.1.7")
ip := clientIPWithProxies(req, parseCIDRList([]string{"10.0.0.0/8"}))
if ip != "203.0.113.9" {
t.Fatalf("spoofed XFF from untrusted remote honoured: got %q", ip)
}
}
// TestClientIP_XForwardedFor_SkipsMalformedEntries pins the skip guard in the
// right-to-left walk: a garbage or empty XFF entry must be stepped over, never
// used as a rate-limit/lockout key. The garbage sits to the RIGHT of the real
// client so the walk actually reaches it. Dropping the guard would return
// "not-an-ip" / "garbage" as the key.
func TestClientIP_XForwardedFor_SkipsMalformedEntries(t *testing.T) {
trusted := parseCIDRList([]string{"10.0.0.0/8"})
tests := []struct {
name string
xff string
want string
}{
{"garbage rightmost", "203.0.113.10, not-an-ip", "203.0.113.10"},
{"empty entry", "203.0.113.10, , 10.0.0.1", "203.0.113.10"},
{"garbage between hops", "203.0.113.10, ::gg::, 10.0.0.1", "203.0.113.10"},
// Nothing parseable at all: fall back to RemoteAddr, never a garbage key.
{"all malformed falls back to RemoteAddr", "garbage, , not-an-ip", "10.0.0.1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.1:9999" // the trusted proxy
req.Header.Set("X-Forwarded-For", tt.xff)
if ip := clientIPWithProxies(req, trusted); ip != tt.want {
t.Errorf("clientIP XFF %q = %q, want %q", tt.xff, ip, tt.want)
}
})
}
}
func TestClientIP_RemoteAddrWithoutPort(t *testing.T) {
// RemoteAddr sometimes has no port (e.g. Unix sockets in tests).
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.1"
ip := clientIPWithProxies(req, nil)
if ip != "10.0.0.1" {
t.Errorf("clientIP no port = %q, want %q", ip, "10.0.0.1")
}
}