From 9ef3ade40261692d3f2bc318caaf963c2cf21895 Mon Sep 17 00:00:00 2001 From: Shirshendu Bhowmick <12836504+shirshendubhowmick@users.noreply.github.com> Date: Sat, 9 May 2026 13:38:45 +0530 Subject: [PATCH] fix(context): return empty string for invalid remote IP in ClientIP method fix(gin): unmap address before checking if it is IPv4 in prepareTrustedCIDRs --- context.go | 3 +++ gin.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 3ecaf520..768aa462 100644 --- a/context.go +++ b/context.go @@ -1022,6 +1022,9 @@ func (c *Context) ClientIP() string { } } } + if !remoteIP.IsValid() { + return "" + } return remoteIP.String() } diff --git a/gin.go b/gin.go index cdb102b5..d01f344a 100644 --- a/gin.go +++ b/gin.go @@ -418,7 +418,7 @@ func (engine *Engine) prepareTrustedCIDRs() ([]netip.Prefix, error) { if err != nil { return cidrs, &net.ParseError{Type: "IP address", Text: trustedProxy} } - if addr.Is4() { + if addr.Unmap().Is4() { trustedProxy += "/32" } else { trustedProxy += "/128"