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 }