From 179ecffe12c24325d0a46fd093ba6e71109dfadb Mon Sep 17 00:00:00 2001 From: Bart Jeukendrup Date: Tue, 20 Feb 2024 21:47:34 +0100 Subject: [PATCH] fix: correctly parse X-Forwarded-For when there are multiple IPs --- internal/utils/utils.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index c1f599c..abeee65 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -85,12 +85,14 @@ func EnvSubst(input string) string { } func ReadUserIP(r *http.Request) string { - IPAddress := r.Header.Get("X-Forwarded-For") - if IPAddress == "" { - host, _, _ := net.SplitHostPort(r.RemoteAddr) - IPAddress = host + forwardedFor := r.Header.Get("X-Forwarded-For") + if forwardedFor != "" { + ips := strings.Split(forwardedFor, ",") + return strings.TrimSpace(ips[0]) } - return IPAddress + + host, _, _ := net.SplitHostPort(r.RemoteAddr) + return host } func StringInSlice(a string, list []string) bool {