package httpapi import ( "crypto/tls" "net/http" "net/http/httptest" "testing" ) func TestConsoleSecretEntryAllowedMatrix(t *testing.T) { cases := []struct { name string host string tls bool xfp string trust bool allow bool want bool }{ {name: "direct TLS", host: "console.example", tls: true, want: true}, {name: "loopback name", host: "localhost:8080", want: true}, {name: "loopback v4", host: "127.0.0.1:8080", want: true}, {name: "loopback v6 with port", host: "[::1]:8080", want: true}, {name: "loopback v6 without port", host: "[::1]", want: true}, // Bracketed non-IPv6 hosts are not address spellings; they must not // borrow loopback trust from the bracket stripper. {name: "bracketed v4 is not loopback", host: "[127.0.0.1]", want: false}, {name: "bracketed localhost is not loopback", host: "[localhost]", want: false}, {name: "bracketed v4 with toggle is not private", host: "[127.0.0.1]", allow: true, want: false}, {name: "private address with toggle", host: "172.29.151.11:28188", allow: true, want: true}, {name: "private address without toggle", host: "172.29.151.11:28188", want: false}, {name: "link-local with toggle", host: "169.254.10.7:8080", allow: true, want: true}, {name: "public address with toggle", host: "203.0.113.10:8080", allow: true, want: false}, {name: "public name with toggle", host: "console.example", allow: true, want: false}, {name: "trusted proxy https", host: "console.example", xfp: "https", trust: true, want: true}, {name: "untrusted proxy https", host: "console.example", xfp: "https", want: false}, {name: "trusted proxy reports http", host: "console.example", xfp: "http", trust: true, want: false}, // A forged header must not beat the address rule when the proxy is not trusted. {name: "untrusted proxy on private host", host: "172.29.151.11:28188", xfp: "https", want: false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "http://placeholder/api/v1/kb-bindings", nil) request.Host = tc.host if tc.tls { request.TLS = &tls.ConnectionState{} } if tc.xfp != "" { request.Header.Set("X-Forwarded-Proto", tc.xfp) } if got := consoleSecretEntryAllowed(request, tc.trust, tc.allow); got != tc.want { t.Fatalf("consoleSecretEntryAllowed = %v, want %v", got, tc.want) } }) } } func TestSecretTransportAllowedAcceptsBareIPv6Loopback(t *testing.T) { request := httptest.NewRequest(http.MethodPost, "http://placeholder/api/v1/kb-bindings/test", nil) request.Host = "[::1]" if !secretTransportAllowed(request, false) { t.Fatal("bare [::1] Host was rejected as a loopback transport") } request.Host = "console.example" if secretTransportAllowed(request, false) { t.Fatal("public name without TLS was accepted") } } // Plain-http consoles are not secure contexts, so Chrome never sends // Sec-Fetch-* with their fetches. A matching Origin is the whole contract. func TestSecretTransportPrivateConsoleNeedsOriginNotSecFetch(t *testing.T) { request := httptest.NewRequest(http.MethodPost, "http://placeholder/api/v1/kb-bindings/test/catalog", nil) request.Host = "192.168.50.134:18093" request.Header.Set("Origin", "http://192.168.50.134:18093") if !secretTransportAllowedForPrivateConsole(request, false, true) { t.Fatal("same-origin private console without Sec-Fetch-Site was rejected") } request.Header.Set("Origin", "http://evil.example") if secretTransportAllowedForPrivateConsole(request, false, true) { t.Fatal("cross-origin request was accepted as a private console") } request.Header.Set("Origin", "http://192.168.50.134:18093") if secretTransportAllowedForPrivateConsole(request, false, false) { t.Fatal("private console was accepted with the deployment toggle off") } } func TestSecretTransportRejectsBracketedNonIPv6Hosts(t *testing.T) { for _, host := range []string{"[127.0.0.1]", "[127.0.0.1]:28188", "[localhost]", "[localhost]:28188"} { request := httptest.NewRequest(http.MethodPost, "http://placeholder/api/v1/kb-bindings/test", nil) request.Host = host if secretTransportAllowed(request, false) { t.Fatalf("Host %q was accepted as a loopback transport", host) } request.Header.Set("Sec-Fetch-Site", "same-origin") request.Header.Set("Origin", "http://"+host) if secretTransportAllowedForPrivateConsole(request, false, true) { t.Fatalf("Host %q was accepted as a private console", host) } } } func TestSecretTransportRejectsMalformedIPv6Suffixes(t *testing.T) { for _, host := range []string{"[::1]junk", "[::1]:", "[::1]:abc"} { request := httptest.NewRequest(http.MethodPost, "http://placeholder/api/v1/kb-bindings/test", nil) request.Host = host if secretTransportAllowed(request, false) { t.Fatalf("Host %q was accepted as a loopback transport", host) } } }