reverseproxy: Avoid returning an error if host address is not an IP address
I am using the reverseproxy module with a custom underlay network, more concretely a SCION network. The SCION underlay network doesn't use IP addresses as host addresses. More concretely, the retrieved addrPort value on https://github.com/caddyserver/caddy/blob/v2.9.0-beta.3/modules/caddyhttp/reverseproxy/reverseproxy.go#L687 will yield and address of the following format [isd-as,ipv4]:port or [isd-as,ipv6%zone]:port.
An error is returned on https://github.com/caddyserver/caddy/blob/v2.9.0-beta.3/modules/caddyhttp/reverseproxy/reverseproxy.go#L740 after netip.ParseAddr(clientIP), in turn, returns an error. I would suggest to ignore the trusted proxy check if netip.ParseAddr(clientIP) returns an error, for example something similar to this:
...
trusted := caddyhttp.GetVar(req.Context(), caddyhttp.TrustedProxyVarKey).(bool)
clientIP, _, _ = strings.Cut(clientIP, "%")
ipAddr, err := netip.ParseAddr(clientIP)
if err == nil {
// Check if the client is a trusted proxy
for _, ipRange := range h.trustedProxies {
if ipRange.Contains(ipAddr) {
trusted = true
break
}
}
}
...
Note that SplitHostPort here is able to split non-ip hosts, so it seems more appropriate to consider that the host address is not necessarily an IP address .This change would maintain the current behavior (meaning that it would not break something that is already working) while allowing for other host address types.
package reverseproxy
import (
"net"
"net/http"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
"golang.org/x/net/http/httpguts"
"net/netip"
)
// getClientIP retrieves the client's IP address from the request, either from
// the RemoteAddr or from a trusted proxy's forwarded headers.
func (h Handler) getClientIP(req *http.Request) (string, error) {
// Start with the remote address
clientIP, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
return "", fmt.Errorf("parsing RemoteAddr (%s): %v", req.RemoteAddr, err)
}
// If there are no trusted proxies configured, return the remote address
if len(h.trustedProxies) == 0 {
return clientIP, nil
}
// Check if the client is a trusted proxy
trusted := caddyhttp.GetVar(req.Context(), caddyhttp.TrustedProxyVarKey).(bool)
clientIP, _, _ = strings.Cut(clientIP, "%") // Remove zone for IPv6
ipAddr, err := netip.ParseAddr(clientIP)
if err == nil {
// Check if the client is a trusted proxy
for _, ipRange := range h.trustedProxies {
if ipRange.Contains(ipAddr) {
trusted = true
break
}
}
} // If err != nil, skip trusted proxy check and proceed with clientIP
if !trusted {
return clientIP, nil
}
// If trusted, check for forwarded headers
for _, header := range h.forwardedHeaders {
if forwarded := req.Header.Get(header); forwarded != "" {
// Take the first IP in the forwarded header (closest to client)
forwardedIPs := strings.Split(forwarded, ",")
for _, ip := range forwardedIPs {
ip = strings.TrimSpace(ip)
if ip != "" {
// Validate it's a valid IP
if _, err := netip.ParseAddr(ip); err == nil {
return ip, nil
}
}
}
}
}
return clientIP, nil
}
I am following up on this, does some maintainer has an opinion on this issue? Thanks!
PR welcome, it would be more useful to have a conversation seeing the implementation, with some tests to cover it.
Hi @JordiSubira and @francislavoie,
I just opened a PR to address this. I implemented the logic to ignore the error when ParseAddr fails (treating the connection as untrusted instead of crashing) and added the unit test as requested.
Looking forward to your feedback!