From d5ef3baa684df8698e7a2f246cd11f942e7cb155 Mon Sep 17 00:00:00 2001 From: Bhupesh Varshney Date: Fri, 27 Nov 2026 13:15:52 +0530 Subject: [PATCH] fix: GetClientIP discards real IP on multi-hop X-Forwarded-For and leaves a port on the RemoteAddr fallback GetClientIP previously returned the X-Forwarded-For header verbatim (unusable once more than one hop is present) and fell back to RemoteAddr including its port when no proxy header was set. It now takes the rightmost X-Forwarded-For entry (the nearest trusted proxy's own observed peer, not the client-claimed leftmost one - which a client-appending proxy like AWS ALB would let the client fake), falls back to X-Real-IP, then RemoteAddr, stripping the port from whichever entry is used via a shared stripPort helper (handles bracketed IPv6 too). Ported from the equivalent devtron-enterprise fix (PR #3450). --- util/RequestUtil.go | 47 +++++++++++++--- util/RequestUtil_test.go | 115 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 util/RequestUtil_test.go diff --git a/util/RequestUtil.go b/util/RequestUtil.go index 9b25e5410e..9862ae7dc0 100644 --- a/util/RequestUtil.go +++ b/util/RequestUtil.go @@ -17,17 +17,50 @@ package util import ( + "net" "net/http" + "strings" ) -const xForwardedForHeaderName = "X-Forwarded-For" +const ( + xForwardedForHeaderName = "X-Forwarded-For" + xRealIPHeaderName = "X-Real-IP" +) -// GetClientIP gets a requests IP address by reading off the forwarded-for -// header (for proxies) and falls back to use the remote address. +// GetClientIP gets a request's originating client IP address, in order of trust: +// the nearest proxy's own view of the connecting peer (X-Forwarded-For's +// rightmost entry), X-Real-IP, then the raw connection address. +// +// X-Forwarded-For is a comma-separated hop chain; each proxy appends the peer IP +// it directly observed. The leftmost entry is whatever the client sent and is +// not trustworthy on proxies that append rather than replace (e.g. AWS ALB) - +// the rightmost entry is the nearest proxy's own observation, which is what we +// trust here. All three sources (X-Forwarded-For, X-Real-IP, RemoteAddr) are +// only as trustworthy as whatever sits in front of this process - a request +// with no proxy in front (bare NodePort/LoadBalancer) lets the client set +// either header to anything. func GetClientIP(r *http.Request) string { - xForwardedFor := r.Header.Get(xForwardedForHeaderName) - if len(xForwardedFor) > 0 { - return xForwardedFor + if xForwardedFor := r.Header.Get(xForwardedForHeaderName); xForwardedFor != "" { + hops := strings.Split(xForwardedFor, ",") + for i := len(hops) - 1; i >= 0; i-- { + if hop := strings.TrimSpace(hops[i]); hop != "" { + return stripPort(hop) + } + } + } + if xRealIP := strings.TrimSpace(r.Header.Get(xRealIPHeaderName)); xRealIP != "" { + return stripPort(xRealIP) + } + return stripPort(r.RemoteAddr) +} + +// stripPort removes a trailing ":port" from a host[:port] value (IPv4, or IPv6 +// bracketed as "[::1]:1234"). A value with no port - including a bare IPv6 +// address, which net.SplitHostPort would otherwise misparse - is returned +// unchanged. +func stripPort(hostport string) string { + if host, _, err := net.SplitHostPort(hostport); err == nil { + return host } - return r.RemoteAddr + return hostport } diff --git a/util/RequestUtil_test.go b/util/RequestUtil_test.go new file mode 100644 index 0000000000..3e8b725b37 --- /dev/null +++ b/util/RequestUtil_test.go @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2020-2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package util + +import ( + "net/http" + "testing" +) + +func newClientIPTestRequest(xForwardedFor, xRealIP, remoteAddr string) *http.Request { + r := &http.Request{Header: http.Header{}, RemoteAddr: remoteAddr} + if xForwardedFor != "" { + r.Header.Set(xForwardedForHeaderName, xForwardedFor) + } + if xRealIP != "" { + r.Header.Set(xRealIPHeaderName, xRealIP) + } + return r +} + +func TestGetClientIP(t *testing.T) { + tests := []struct { + name string + xForwardedFor string + xRealIP string + remoteAddr string + want string + }{ + { + name: "single XFF entry, no port - the common single-proxy case", + xForwardedFor: "203.0.113.5", + remoteAddr: "10.48.1.1:443", + want: "203.0.113.5", + }, + { + name: "multi-hop XFF takes the rightmost entry, not the client-claimed leftmost", + xForwardedFor: "1.2.3.4, 203.0.113.5", + remoteAddr: "10.48.1.1:443", + want: "203.0.113.5", + }, + { + name: "whitespace around entries is trimmed", + xForwardedFor: "1.2.3.4 , 203.0.113.5 ", + want: "203.0.113.5", + }, + { + name: "trailing empty entry is skipped in favor of the last real one", + xForwardedFor: "1.2.3.4, 203.0.113.5,", + want: "203.0.113.5", + }, + { + name: "leading empty entry is skipped when scanning from the right", + xForwardedFor: ", 1.2.3.4", + want: "1.2.3.4", + }, + { + name: "Azure App Gateway style ip:port entry has its port stripped", + xForwardedFor: "1.2.3.4:5000, 203.0.113.5:6000", + want: "203.0.113.5", + }, + { + name: "bracketed IPv6 entry with port has its port stripped", + xForwardedFor: "[2001:db8::1]:6000", + want: "2001:db8::1", + }, + { + name: "no XFF - falls back to X-Real-IP", + xRealIP: "203.0.113.9", + remoteAddr: "10.48.1.1:443", + want: "203.0.113.9", + }, + { + name: "no XFF, no X-Real-IP - falls back to RemoteAddr with port stripped (IPv4)", + remoteAddr: "10.0.0.85:51234", + want: "10.0.0.85", + }, + { + name: "RemoteAddr with bracketed IPv6 and port", + remoteAddr: "[2001:db8::1]:51234", + want: "2001:db8::1", + }, + { + name: "RemoteAddr with no port is returned as-is", + remoteAddr: "10.0.0.85", + want: "10.0.0.85", + }, + { + name: "unparseable RemoteAddr is returned as-is rather than dropped", + remoteAddr: "not-an-address", + want: "not-an-address", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := newClientIPTestRequest(tt.xForwardedFor, tt.xRealIP, tt.remoteAddr) + if got := GetClientIP(r); got != tt.want { + t.Errorf("GetClientIP() = %q, want %q", got, tt.want) + } + }) + } +}