Skip to content

Commit 179ecff

Browse files
committed
fix: correctly parse X-Forwarded-For when there are multiple IPs
1 parent 436d046 commit 179ecff

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

internal/utils/utils.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ func EnvSubst(input string) string {
8585
}
8686

8787
func ReadUserIP(r *http.Request) string {
88-
IPAddress := r.Header.Get("X-Forwarded-For")
89-
if IPAddress == "" {
90-
host, _, _ := net.SplitHostPort(r.RemoteAddr)
91-
IPAddress = host
88+
forwardedFor := r.Header.Get("X-Forwarded-For")
89+
if forwardedFor != "" {
90+
ips := strings.Split(forwardedFor, ",")
91+
return strings.TrimSpace(ips[0])
9292
}
93-
return IPAddress
93+
94+
host, _, _ := net.SplitHostPort(r.RemoteAddr)
95+
return host
9496
}
9597

9698
func StringInSlice(a string, list []string) bool {

0 commit comments

Comments
 (0)