2019-01-15 08:44:03 +00:00
|
|
|
package forwardedheaders
|
|
|
|
|
|
|
|
import (
|
2019-04-02 14:56:05 +00:00
|
|
|
"net"
|
2019-01-15 08:44:03 +00:00
|
|
|
"net/http"
|
2024-09-16 09:10:04 +00:00
|
|
|
"net/textproto"
|
2019-04-02 14:56:05 +00:00
|
|
|
"os"
|
2024-09-16 09:10:04 +00:00
|
|
|
"slices"
|
2019-04-02 14:56:05 +00:00
|
|
|
"strings"
|
2019-01-15 08:44:03 +00:00
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/ip"
|
2024-09-16 09:10:04 +00:00
|
|
|
"golang.org/x/net/http/httpguts"
|
2019-01-15 08:44:03 +00:00
|
|
|
)
|
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
const (
|
2019-07-08 15:56:04 +00:00
|
|
|
xForwardedProto = "X-Forwarded-Proto"
|
|
|
|
xForwardedFor = "X-Forwarded-For"
|
|
|
|
xForwardedHost = "X-Forwarded-Host"
|
|
|
|
xForwardedPort = "X-Forwarded-Port"
|
|
|
|
xForwardedServer = "X-Forwarded-Server"
|
|
|
|
xForwardedURI = "X-Forwarded-Uri"
|
|
|
|
xForwardedMethod = "X-Forwarded-Method"
|
|
|
|
xForwardedTLSClientCert = "X-Forwarded-Tls-Client-Cert"
|
|
|
|
xForwardedTLSClientCertInfo = "X-Forwarded-Tls-Client-Cert-Info"
|
|
|
|
xRealIP = "X-Real-Ip"
|
|
|
|
connection = "Connection"
|
|
|
|
upgrade = "Upgrade"
|
2019-04-02 14:56:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var xHeaders = []string{
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedServer,
|
2019-07-08 15:56:04 +00:00
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
2019-04-02 14:56:05 +00:00
|
|
|
xRealIP,
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// XForwarded is an HTTP handler wrapper that sets the X-Forwarded headers,
|
|
|
|
// and other relevant headers for a reverse-proxy.
|
|
|
|
// Unless insecure is set,
|
|
|
|
// it first removes all the existing values for those headers if the remote address is not one of the trusted ones.
|
2019-01-15 08:44:03 +00:00
|
|
|
type XForwarded struct {
|
2024-09-16 09:10:04 +00:00
|
|
|
insecure bool
|
|
|
|
trustedIPs []string
|
|
|
|
connectionHeaders []string
|
|
|
|
ipChecker *ip.Checker
|
|
|
|
next http.Handler
|
|
|
|
hostname string
|
2019-01-15 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewXForwarded creates a new XForwarded.
|
2024-09-16 09:10:04 +00:00
|
|
|
func NewXForwarded(insecure bool, trustedIPs []string, connectionHeaders []string, next http.Handler) (*XForwarded, error) {
|
2019-01-15 08:44:03 +00:00
|
|
|
var ipChecker *ip.Checker
|
2024-09-16 09:10:04 +00:00
|
|
|
if len(trustedIPs) > 0 {
|
2019-01-15 08:44:03 +00:00
|
|
|
var err error
|
2024-09-16 09:10:04 +00:00
|
|
|
ipChecker, err = ip.NewChecker(trustedIPs)
|
2019-01-15 08:44:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hostname = "localhost"
|
|
|
|
}
|
|
|
|
|
2019-01-15 08:44:03 +00:00
|
|
|
return &XForwarded{
|
2024-09-16 09:10:04 +00:00
|
|
|
insecure: insecure,
|
|
|
|
trustedIPs: trustedIPs,
|
|
|
|
connectionHeaders: connectionHeaders,
|
|
|
|
ipChecker: ipChecker,
|
|
|
|
next: next,
|
|
|
|
hostname: hostname,
|
2019-01-15 08:44:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *XForwarded) isTrustedIP(ip string) bool {
|
|
|
|
if x.ipChecker == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return x.ipChecker.IsAuthorized(ip) == nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// removeIPv6Zone removes the zone if the given IP is an ipv6 address and it has {zone} information in it,
|
|
|
|
// like "[fe80::d806:a55d:eb1b:49cc%vEthernet (vmxnet3 Ethernet Adapter - Virtual Switch)]:64692".
|
2019-04-02 14:56:05 +00:00
|
|
|
func removeIPv6Zone(clientIP string) string {
|
2021-01-21 09:04:04 +00:00
|
|
|
if idx := strings.Index(clientIP, "%"); idx != -1 {
|
|
|
|
return clientIP[:idx]
|
|
|
|
}
|
|
|
|
return clientIP
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// isWebsocketRequest returns whether the specified HTTP request is a websocket handshake request.
|
2019-04-02 14:56:05 +00:00
|
|
|
func isWebsocketRequest(req *http.Request) bool {
|
|
|
|
containsHeader := func(name, value string) bool {
|
2021-01-21 09:04:04 +00:00
|
|
|
h := unsafeHeader(req.Header).Get(name)
|
|
|
|
for {
|
|
|
|
pos := strings.Index(h, ",")
|
|
|
|
if pos == -1 {
|
|
|
|
return strings.EqualFold(value, strings.TrimSpace(h))
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.EqualFold(value, strings.TrimSpace(h[:pos])) {
|
2019-04-02 14:56:05 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-01-21 09:04:04 +00:00
|
|
|
|
2021-02-02 10:40:04 +00:00
|
|
|
h = h[pos+1:]
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 10:40:04 +00:00
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
return containsHeader(connection, "upgrade") && containsHeader(upgrade, "websocket")
|
|
|
|
}
|
|
|
|
|
|
|
|
func forwardedPort(req *http.Request) string {
|
|
|
|
if req == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, port, err := net.SplitHostPort(req.Host); err == nil && port != "" {
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
if unsafeHeader(req.Header).Get(xForwardedProto) == "https" || unsafeHeader(req.Header).Get(xForwardedProto) == "wss" {
|
2019-04-02 14:56:05 +00:00
|
|
|
return "443"
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.TLS != nil {
|
|
|
|
return "443"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "80"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *XForwarded) rewrite(outreq *http.Request) {
|
|
|
|
if clientIP, _, err := net.SplitHostPort(outreq.RemoteAddr); err == nil {
|
|
|
|
clientIP = removeIPv6Zone(clientIP)
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
if unsafeHeader(outreq.Header).Get(xRealIP) == "" {
|
|
|
|
unsafeHeader(outreq.Header).Set(xRealIP, clientIP)
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
xfProto := unsafeHeader(outreq.Header).Get(xForwardedProto)
|
2019-04-02 14:56:05 +00:00
|
|
|
if xfProto == "" {
|
2022-07-06 09:54:08 +00:00
|
|
|
// TODO: is this expected to set the X-Forwarded-Proto header value to
|
|
|
|
// ws(s) as the underlying request used to upgrade the connection is
|
|
|
|
// made over HTTP(S)?
|
2020-06-10 12:32:03 +00:00
|
|
|
if isWebsocketRequest(outreq) {
|
|
|
|
if outreq.TLS != nil {
|
2021-01-21 09:04:04 +00:00
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedProto, "wss")
|
2020-06-10 12:32:03 +00:00
|
|
|
} else {
|
2021-01-21 09:04:04 +00:00
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedProto, "ws")
|
2020-06-10 12:32:03 +00:00
|
|
|
}
|
2019-04-02 14:56:05 +00:00
|
|
|
} else {
|
2020-06-10 12:32:03 +00:00
|
|
|
if outreq.TLS != nil {
|
2021-01-21 09:04:04 +00:00
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedProto, "https")
|
2020-06-10 12:32:03 +00:00
|
|
|
} else {
|
2021-01-21 09:04:04 +00:00
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedProto, "http")
|
2020-06-10 12:32:03 +00:00
|
|
|
}
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
if xfPort := unsafeHeader(outreq.Header).Get(xForwardedPort); xfPort == "" {
|
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedPort, forwardedPort(outreq))
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
if xfHost := unsafeHeader(outreq.Header).Get(xForwardedHost); xfHost == "" && outreq.Host != "" {
|
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedHost, outreq.Host)
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 14:36:07 +00:00
|
|
|
// Per https://www.rfc-editor.org/rfc/rfc2616#section-4.2, the Forwarded IPs list is in
|
|
|
|
// the same order as the values in the X-Forwarded-For header(s).
|
|
|
|
if xffs := unsafeHeader(outreq.Header).Values(xForwardedFor); len(xffs) > 0 {
|
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedFor, strings.Join(xffs, ", "))
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
if x.hostname != "" {
|
2021-01-21 09:04:04 +00:00
|
|
|
unsafeHeader(outreq.Header).Set(xForwardedServer, x.hostname)
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// ServeHTTP implements http.Handler.
|
2019-01-15 08:44:03 +00:00
|
|
|
func (x *XForwarded) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !x.insecure && !x.isTrustedIP(r.RemoteAddr) {
|
2019-04-02 14:56:05 +00:00
|
|
|
for _, h := range xHeaders {
|
2021-01-21 09:04:04 +00:00
|
|
|
unsafeHeader(r.Header).Del(h)
|
2019-04-02 14:56:05 +00:00
|
|
|
}
|
2019-01-15 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
x.rewrite(r)
|
|
|
|
|
2024-09-16 09:10:04 +00:00
|
|
|
x.removeConnectionHeaders(r)
|
|
|
|
|
2019-01-15 08:44:03 +00:00
|
|
|
x.next.ServeHTTP(w, r)
|
|
|
|
}
|
2021-01-21 09:04:04 +00:00
|
|
|
|
2024-09-16 09:10:04 +00:00
|
|
|
func (x *XForwarded) removeConnectionHeaders(req *http.Request) {
|
|
|
|
var reqUpType string
|
|
|
|
if httpguts.HeaderValuesContainsToken(req.Header[connection], upgrade) {
|
|
|
|
reqUpType = unsafeHeader(req.Header).Get(upgrade)
|
|
|
|
}
|
|
|
|
|
|
|
|
var connectionHopByHopHeaders []string
|
|
|
|
for _, f := range req.Header[connection] {
|
|
|
|
for _, sf := range strings.Split(f, ",") {
|
|
|
|
if sf = textproto.TrimString(sf); sf != "" {
|
|
|
|
// Connection header cannot dictate to remove X- headers managed by Traefik,
|
|
|
|
// as per rfc7230 https://datatracker.ietf.org/doc/html/rfc7230#section-6.1,
|
|
|
|
// A proxy or gateway MUST ... and then remove the Connection header field itself
|
|
|
|
// (or replace it with the intermediary's own connection options for the forwarded message).
|
|
|
|
if slices.Contains(xHeaders, sf) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep headers allowed through the middleware chain.
|
|
|
|
if slices.Contains(x.connectionHeaders, sf) {
|
|
|
|
connectionHopByHopHeaders = append(connectionHopByHopHeaders, sf)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply Connection header option.
|
|
|
|
req.Header.Del(sf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqUpType != "" {
|
|
|
|
connectionHopByHopHeaders = append(connectionHopByHopHeaders, upgrade)
|
|
|
|
unsafeHeader(req.Header).Set(upgrade, reqUpType)
|
|
|
|
}
|
|
|
|
if len(connectionHopByHopHeaders) > 0 {
|
|
|
|
unsafeHeader(req.Header).Set(connection, strings.Join(connectionHopByHopHeaders, ","))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafeHeader(req.Header).Del(connection)
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
// unsafeHeader allows to manage Header values.
|
|
|
|
// Must be used only when the header name is already a canonical key.
|
|
|
|
type unsafeHeader map[string][]string
|
|
|
|
|
|
|
|
func (h unsafeHeader) Set(key, value string) {
|
|
|
|
h[key] = []string{value}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h unsafeHeader) Get(key string) string {
|
|
|
|
if len(h[key]) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return h[key][0]
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:36:07 +00:00
|
|
|
func (h unsafeHeader) Values(key string) []string {
|
|
|
|
return h[key]
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:04:04 +00:00
|
|
|
func (h unsafeHeader) Del(key string) {
|
|
|
|
delete(h, key)
|
|
|
|
}
|