fix(context): return empty string for invalid remote IP in ClientIP method

fix(gin): unmap address before checking if it is IPv4 in prepareTrustedCIDRs
This commit is contained in:
Shirshendu Bhowmick 2026-05-09 13:38:45 +05:30
parent 6f54838d7f
commit 9ef3ade402
No known key found for this signature in database
2 changed files with 4 additions and 1 deletions

View File

@ -1022,6 +1022,9 @@ func (c *Context) ClientIP() string {
} }
} }
} }
if !remoteIP.IsValid() {
return ""
}
return remoteIP.String() return remoteIP.String()
} }

2
gin.go
View File

@ -418,7 +418,7 @@ func (engine *Engine) prepareTrustedCIDRs() ([]netip.Prefix, error) {
if err != nil { if err != nil {
return cidrs, &net.ParseError{Type: "IP address", Text: trustedProxy} return cidrs, &net.ParseError{Type: "IP address", Text: trustedProxy}
} }
if addr.Is4() { if addr.Unmap().Is4() {
trustedProxy += "/32" trustedProxy += "/32"
} else { } else {
trustedProxy += "/128" trustedProxy += "/128"