package httpapi import ( "net" "net/http" "net/url" "strings" ) // secretTransportAllowed is the single rule for typing a secret into the // console: a real TLS connection, a loopback request, or a same-origin HTTPS // request that a trusted reverse proxy forwarded. The browser's origin scheme // must agree, so a plain-HTTP page behind a misconfigured proxy cannot submit // a key even if it spoofs X-Forwarded-Proto. func secretTransportAllowed(r *http.Request, trustTLSProxy bool) bool { if r.TLS != nil { return true } host := r.Host if parsedHost, _, err := net.SplitHostPort(host); err == nil { host = parsedHost } if strings.EqualFold(host, "localhost") { return true } if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() { return true } if trustTLSProxy && r.Header.Get("X-Forwarded-Proto") == "https" && r.Header.Get("Sec-Fetch-Site") == "same-origin" { origin, err := url.Parse(r.Header.Get("Origin")) return err == nil && origin.Scheme == "https" } return false } // secretTransportAllowedForPrivateConsole additionally accepts a same-origin // fetch from the operator console itself when it is served over plain http on a // private address. That is the only non-TLS shape this deployment needs, and it // still requires an exact Origin/Host match. func secretTransportAllowedForPrivateConsole(r *http.Request, trustTLSProxy, allowPrivateHTTP bool) bool { if secretTransportAllowed(r, trustTLSProxy) { return true } if !allowPrivateHTTP || r.Header.Get("Sec-Fetch-Site") != "same-origin" { return false } origin := strings.TrimSpace(r.Header.Get("Origin")) if origin == "" { return false } parsed, err := url.Parse(origin) if err != nil || parsed.Host == "" || !strings.EqualFold(parsed.Host, r.Host) { return false } host := parsed.Hostname() if ip := net.ParseIP(host); ip != nil { return ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() } return strings.EqualFold(host, "localhost") }