From c83abdf57010887da052cc9adf3302061a3f8ccf Mon Sep 17 00:00:00 2001 From: Irshad Ahmed Date: Tue, 30 Jan 2024 10:31:42 -0600 Subject: [PATCH] fix ip parsing --- cmd/api/src/api/middleware/middleware.go | 9 +-------- .../middleware/middleware_internal_test.go | 19 +------------------ 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/cmd/api/src/api/middleware/middleware.go b/cmd/api/src/api/middleware/middleware.go index 1b33a910ed..7713ed9f1a 100644 --- a/cmd/api/src/api/middleware/middleware.go +++ b/cmd/api/src/api/middleware/middleware.go @@ -156,14 +156,7 @@ func parseUserIP(r *http.Request) string { log.Errorf("No data found in X-Forwarded-For") } - if parsedUrl, err := url.Parse(r.RemoteAddr); err != nil { - log.Errorf("Error parsing IP address from RemoteAddr: %s", err) - } else if hostName := parsedUrl.Hostname(); hostName == "" { - log.Errorf("Hostname not found in URL: %s", parsedUrl.String()) - } else { - res += "Remote Address: " + parsedUrl.Hostname() - } - + res += "Remote Address: " + r.RemoteAddr return res } diff --git a/cmd/api/src/api/middleware/middleware_internal_test.go b/cmd/api/src/api/middleware/middleware_internal_test.go index 941ad84bd9..6217961f35 100644 --- a/cmd/api/src/api/middleware/middleware_internal_test.go +++ b/cmd/api/src/api/middleware/middleware_internal_test.go @@ -91,23 +91,6 @@ func TestParseUserIP_XForwardedForMissing(t *testing.T) { require.Contains(t, res, "Remote Address") } -func TestParseUserIP_RemoteAddrError(t *testing.T) { - req, err := http.NewRequest("GET", "/teapot", nil) - require.Nil(t, err) - - ip1 := "192.168.1.1:8080" - ip2 := "192.168.1.2" - ip3 := "192.168.1.3" - req.Header.Set("X-Forwarded-For", strings.Join([]string{ip1, ip2, ip3}, ",")) - req.RemoteAddr = "0.0.0.0:3000" - - res := parseUserIP(req) - require.Contains(t, res, "X-Forwarded-For") - require.Contains(t, res, ip1) - require.Contains(t, res, ip2) - require.NotContains(t, res, "Remote Address") -} - func TestParseUserIP_Success(t *testing.T) { req, err := http.NewRequest("GET", "/teapot", nil) require.Nil(t, err) @@ -117,7 +100,7 @@ func TestParseUserIP_Success(t *testing.T) { ip3 := "192.168.1.3" req.Header.Set("X-Forwarded-For", strings.Join([]string{ip1, ip2, ip3}, ",")) - req.RemoteAddr = "http://www.google.com/0.0.0.0:3000" + req.RemoteAddr = "0.0.0.0:3000" res := parseUserIP(req) require.Contains(t, res, "X-Forwarded-For")