From 84ec44d9d7aba5327a7c9b0a56484ab7377889a9 Mon Sep 17 00:00:00 2001 From: Notealot <714804968@qq.com> Date: Wed, 27 Oct 2021 09:13:32 +0800 Subject: [PATCH] Revert "rationalize return logic in validateHeader()" This reverts commit 513b59fa8875d77c86f122af4bb0cc547eb98c8f. --- gin.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/gin.go b/gin.go index 73f71a3c..bee42dee 100644 --- a/gin.go +++ b/gin.go @@ -416,23 +416,24 @@ func (engine *Engine) isTrustedProxy(ip net.IP) bool { } func (engine *Engine) validateHeader(header string) (clientIP string, valid bool) { - if header != "" { - items := strings.Split(header, ",") - for i := len(items) - 1; i >= 0; i-- { - ipStr := strings.TrimSpace(items[i]) - ip := net.ParseIP(ipStr) - if ip == nil { - return "", false - } + if header == "" { + return "", false + } + items := strings.Split(header, ",") + for i := len(items) - 1; i >= 0; i-- { + ipStr := strings.TrimSpace(items[i]) + ip := net.ParseIP(ipStr) + if ip == nil { + return "", false + } - // X-Forwarded-For is appended by proxy - // Check IPs in reverse order and stop when find untrusted proxy - if (i == 0) || (!engine.isTrustedProxy(ip)) { - return ipStr, true - } + // X-Forwarded-For is appended by proxy + // Check IPs in reverse order and stop when find untrusted proxy + if (i == 0) || (!engine.isTrustedProxy(ip)) { + return ipStr, true } } - return "", false + return } // parseTrustedProxies parse Engine.trustedProxies to Engine.trustedCIDRs