From ef67d4ed625a483a9eee18f284369b79937b8d9b Mon Sep 17 00:00:00 2001 From: orinnz Date: Sat, 3 Oct 2026 19:06:45 +0700 Subject: [PATCH] fix: resolve RealIP for bare RemoteAddr without port in v4 Context.RealIP fell back to net.SplitHostPort and ignored its error, so a RemoteAddr holding just an IP (no :port) came back as an empty string. Use extractIP, the helper ExtractIPDirect already uses, which accepts both forms. Backport of #3054. --- context.go | 4 +--- context_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index dd5f11c46..439231517 100644 --- a/context.go +++ b/context.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "mime/multipart" - "net" "net/http" "net/url" "strings" @@ -315,8 +314,7 @@ func (c *context) RealIP() string { ip = strings.TrimSuffix(ip, "]") return ip } - ra, _, _ := net.SplitHostPort(c.request.RemoteAddr) - return ra + return extractIP(c.request) } func (c *context) Path() string { diff --git a/context_test.go b/context_test.go index 67a2b6174..e7a40bb1f 100644 --- a/context_test.go +++ b/context_test.go @@ -1374,6 +1374,22 @@ func TestContext_RealIP(t *testing.T) { }, "89.89.89.89", }, + { + &context{ + request: &http.Request{ + RemoteAddr: "89.89.89.89", + }, + }, + "89.89.89.89", + }, + { + &context{ + request: &http.Request{ + RemoteAddr: "2001:db8::1", + }, + }, + "2001:db8::1", + }, } for _, tt := range tests {