diff --git a/internal/config/trusted_proxies_internal_test.go b/internal/config/trusted_proxies_internal_test.go index ae61bfd..41a184d 100644 --- a/internal/config/trusted_proxies_internal_test.go +++ b/internal/config/trusted_proxies_internal_test.go @@ -83,3 +83,41 @@ func TestTrustedProxiesInvalidAbortsStartup(t *testing.T) { }, }) } + +// TestTrustedProxiesMappedEntryIsIPv4Range checks that an entry written in +// the IPv4-mapped form (::ffff:a.b.c.d) is kept as the IPv4 range it covers, +// since the client address is matched as the IPv4 address it holds. /96, the +// shortest such entry, covers every IPv4 address. +func TestTrustedProxiesMappedEntryIsIPv4Range(t *testing.T) { + t.Parallel() + + c, err := configFromYAML(t, signingKeyLine+ + `trusted_proxies: ["::ffff:10.0.0.0/104", "::ffff:0:0/96"]`+"\n") + if err != nil { + t.Fatalf("IPv4-mapped trusted_proxies entries should load: %v", err) + } + + got := make([]string, len(c.TrustedProxies)) + for i, p := range c.TrustedProxies { + got[i] = p.String() + } + + if joined := strings.Join(got, ","); joined != "10.0.0.0/8,0.0.0.0/0" { + t.Errorf("TrustedProxies = %v, want 10.0.0.0/8 and 0.0.0.0/0", got) + } +} + +// TestTrustedProxiesMappedEntryShorterThan96AbortsStartup checks that an +// entry in the IPv4-mapped form shorter than /96, which covers more than the +// IPv4 addresses, aborts startup with an error naming the key and the value. +func TestTrustedProxiesMappedEntryShorterThan96AbortsStartup(t *testing.T) { + t.Parallel() + + runAbortCases(t, []abortCase{ + { + name: "mapped-95", + yaml: signingKeyLine + `trusted_proxies: ["::ffff:0:0/95"]` + "\n", + wantErrSubstrings: []string{keyTrustedProxies, "::ffff:0:0/95"}, + }, + }) +}