Merge ec789274b1f5093f956d71d05b1cf88327e925d5 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
guoyangzhen 2026-10-21 08:04:11 +00:00 committed by GitHub
commit 4ba5aad110
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

11
gin.go
View File

@ -509,6 +509,17 @@ func (engine *Engine) validateHeader(header string) (clientIP string, valid bool
items := strings.Split(header, ",")
for i := len(items) - 1; i >= 0; i-- {
ipStr := strings.TrimSpace(items[i])
// Handle IPv6 with brackets and/or port: [::1], [::1]:8080, 192.168.1.1:8080
// net.SplitHostPort handles all these cases and strips brackets
if host, _, err := net.SplitHostPort(ipStr); err == nil {
ipStr = host
} else {
// No port present, just strip brackets if any (bare IPv6 like [::1])
ipStr = strings.TrimPrefix(ipStr, "[")
ipStr = strings.TrimSuffix(ipStr, "]")
}
ip := net.ParseIP(ipStr)
if ip == nil {
break