mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
Loopback trust (no-auth access, recovery tokens and sessions, unauthenticated config transfer, the rate-limit exemption) now needs more than a loopback peer: no reverse-proxy forwarding header, a Host that is localhost, *.localhost or a loopback literal, and a browser request that is same-origin by Fetch Metadata or names exactly this origin. A same-host proxy passing a public Host, a page whose name resolves to 127.0.0.1, or a page on another origin no longer counts as a local user. GetClientIP now takes the right-most X-Forwarded-For hop that is not in PULSE_TRUSTED_PROXY_CIDRS, reading every header line and accepting entries with ports, instead of the first entry. A proxy that appends keeps the client's own entries on the left, so the first entry is client-chosen. Lockout, rate limits, audit addresses and recovery-token binding use this. The reverse-proxy guide explains multi-hop setups: list upstream hops as trusted or have the final proxy replace the header.
97 lines
3.2 KiB
Go
97 lines
3.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// loopbackProxyHeaders are request headers that a reverse proxy adds and a
|
|
// local client talking straight to Pulse does not send. Any of them means the
|
|
// loopback peer may be forwarding someone else's request.
|
|
var loopbackProxyHeaders = []string{
|
|
"X-Forwarded-For",
|
|
"Forwarded",
|
|
"X-Real-IP",
|
|
"X-Forwarded-Host",
|
|
"X-Forwarded-Proto",
|
|
"X-Forwarded-Server",
|
|
"X-Forwarded-Port",
|
|
"X-Forwarded-Scheme",
|
|
"Via",
|
|
"CF-Connecting-IP",
|
|
"True-Client-IP",
|
|
}
|
|
|
|
// isDirectLoopbackRequest is the trust rule for a client on the Pulse host
|
|
// talking straight to Pulse. It gates no-auth anonymous access, recovery
|
|
// tokens and sessions, unauthenticated config transfer and quick setup, and
|
|
// the rate-limit exemption.
|
|
//
|
|
// A loopback peer is not enough on its own. The request must also:
|
|
// - carry no reverse-proxy forwarding headers;
|
|
// - name a local Host (localhost, *.localhost or a loopback address), so a
|
|
// same-host proxy passing on a public Host, or a page whose name was
|
|
// re-resolved to 127.0.0.1, is not mistaken for a local client;
|
|
// - not come from another origin's page in a browser on the Pulse host.
|
|
//
|
|
// A same-host proxy that rewrites Host to a loopback address and adds no
|
|
// forwarding headers still cannot be told apart from a local client, so such
|
|
// a proxy must add X-Forwarded-For (see docs/REVERSE_PROXY.md).
|
|
func isDirectLoopbackRequest(req *http.Request) bool {
|
|
if req == nil {
|
|
return false
|
|
}
|
|
|
|
ip := net.ParseIP(extractRemoteIP(req.RemoteAddr))
|
|
if ip == nil || !ip.IsLoopback() {
|
|
return false
|
|
}
|
|
|
|
for _, header := range loopbackProxyHeaders {
|
|
if _, present := req.Header[http.CanonicalHeaderKey(header)]; present {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if !isLocalRequestHost(req.Host) {
|
|
return false
|
|
}
|
|
|
|
return isSameOriginOrNonBrowserRequest(req)
|
|
}
|
|
|
|
// isSameOriginOrNonBrowserRequest is the browser half of loopback trust. A
|
|
// browser's Sec-Fetch-Site cannot be set by page script, so "same-origin" and
|
|
// "none" (typed or bookmarked) are accepted as they are. Any other request that
|
|
// names a browser origin must name exactly this one, reads and WebSocket
|
|
// upgrades included: a page on another local port is same-site but must not
|
|
// borrow loopback trust, and a WebSocket is not held back by CORS. A client
|
|
// that sends no browser metadata at all, such as curl, is accepted.
|
|
func isSameOriginOrNonBrowserRequest(req *http.Request) bool {
|
|
switch strings.ToLower(strings.TrimSpace(req.Header.Get("Sec-Fetch-Site"))) {
|
|
case "same-origin", "none":
|
|
return true
|
|
case "":
|
|
return browserOriginMetadataMatches(req, requestOrigin(req), true)
|
|
default:
|
|
return browserOriginMetadataMatches(req, requestOrigin(req), false)
|
|
}
|
|
}
|
|
|
|
// isLocalRequestHost reports whether a Host header names this machine without
|
|
// relying on DNS: localhost, a *.localhost name (browsers resolve these to
|
|
// loopback themselves), or a loopback IP literal.
|
|
func isLocalRequestHost(raw string) bool {
|
|
host, ok := validateRequestHost(raw)
|
|
if !ok {
|
|
return false
|
|
}
|
|
name := strings.TrimSuffix(strings.ToLower(host.hostname), ".")
|
|
if name == "localhost" || strings.HasSuffix(name, ".localhost") {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(name)
|
|
return ip != nil && ip.IsLoopback()
|
|
}
|