8c84c9c601
build / build (push) Successful in 2m26s
- Accept the private-console transport with an exact Origin/Host match; the http console is not a secure context, so Chrome sends no Sec-Fetch-Site and that requirement made every real write 400. - Reject bracketed non-IPv6 host spellings, including ported forms, instead of normalising them to loopback. - Show the plaintext-warning copy or the disabled state from a tri-state capability field; old APIs keep the legacy message. - Document WEKNORA_ALLOW_PRIVATE_HTTP in .env.example and the operations runbook.
118 lines
4.0 KiB
Go
118 lines
4.0 KiB
Go
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
|
|
}
|
|
if loopbackHost(r.Host) {
|
|
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. It deliberately does not require
|
|
// Sec-Fetch-Site: Chrome only sends Sec-Fetch-* from secure contexts, so an
|
|
// http console on a private address never carries that header.
|
|
func secretTransportAllowedForPrivateConsole(r *http.Request, trustTLSProxy, allowPrivateHTTP bool) bool {
|
|
if secretTransportAllowed(r, trustTLSProxy) {
|
|
return true
|
|
}
|
|
if !allowPrivateHTTP {
|
|
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()
|
|
return privateAddressHost(host)
|
|
}
|
|
|
|
// consoleSecretEntryAllowed mirrors the transports the write gates accept, but
|
|
// it cannot simply call secretTransportAllowed: that gate guards a write and
|
|
// therefore insists on a same-origin Origin header, which a GET navigation
|
|
// never sends. Trusting X-Forwarded-Proto alone on a read is safe because
|
|
// browsers cannot set it and the deployment already declares the proxy
|
|
// trusted; a forged header still cannot make the write gate accept anything.
|
|
func consoleSecretEntryAllowed(r *http.Request, trustTLSProxy, allowPrivateHTTP bool) bool {
|
|
if r.TLS != nil || loopbackHost(r.Host) {
|
|
return true
|
|
}
|
|
if trustTLSProxy && r.Header.Get("X-Forwarded-Proto") == "https" {
|
|
return true
|
|
}
|
|
return allowPrivateHTTP && privateAddressHost(r.Host)
|
|
}
|
|
|
|
// requestHostname strips the optional port and IPv6 brackets. Brackets come
|
|
// off only around a real IPv6 literal: "[127.0.0.1]", "[localhost]" and their
|
|
// ported forms are not address spellings the server or a browser ever emits,
|
|
// so they stay untouched and keep failing the address checks.
|
|
func requestHostname(host string) string {
|
|
host = strings.TrimSpace(host)
|
|
if strings.HasPrefix(host, "[") {
|
|
end := strings.Index(host, "]")
|
|
if end <= 0 {
|
|
return host
|
|
}
|
|
ip := net.ParseIP(host[1:end])
|
|
if ip == nil || ip.To4() != nil {
|
|
return host
|
|
}
|
|
if rest := host[end+1:]; rest != "" && (rest[0] != ':' || rest == ":" || strings.Trim(rest[1:], "0123456789") != "") {
|
|
return host
|
|
}
|
|
return ip.String()
|
|
}
|
|
if parsedHost, _, err := net.SplitHostPort(host); err == nil {
|
|
return parsedHost
|
|
}
|
|
return host
|
|
}
|
|
|
|
// loopbackHost is the address-only part of the secret transport rule: a
|
|
// request from this machine never crosses a network.
|
|
func loopbackHost(host string) bool {
|
|
host = requestHostname(host)
|
|
if strings.EqualFold(host, "localhost") {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return ip != nil && ip.IsLoopback()
|
|
}
|
|
|
|
// privateAddressHost accepts loopback and private addresses. Names other than
|
|
// localhost are rejected because the deployment toggle is about addresses,
|
|
// not about trusting whatever DNS resolves to.
|
|
func privateAddressHost(host string) bool {
|
|
host = requestHostname(host)
|
|
if strings.EqualFold(host, "localhost") {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return ip != nil && (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast())
|
|
}
|