Merge 668da512661be305ff9c102e643a89b75d86a1a2 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
Shirong Lu 2026-10-15 05:49:15 +00:00 committed by GitHub
commit 4fda621807
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

8
gin.go
View File

@ -509,6 +509,14 @@ 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]:port or IPv4:port notation (e.g., [::1]:38792, 192.168.1.1:38792)
if host, _, err := net.SplitHostPort(ipStr); err == nil {
ipStr = host
}
// Strip brackets around bare IPv6 addresses (e.g., [::1] -> ::1)
if len(ipStr) > 0 && ipStr[0] == '[' {
ipStr = strings.Trim(ipStr, "[]")
}
ip := net.ParseIP(ipStr)
if ip == nil {
break