Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Fix IP address parsing for ipv6 with port number
Uku Tahtc0083815d496
diff --git a/lib/plausible_web/remote_ip.ex b/lib/plausible_web/remote_ip.exindex 0d61180141..110156eb23 100644--- a/lib/plausible_web/remote_ip.ex+++ b/lib/plausible_web/remote_ip.ex@@ -7,28 +7,38 @@ defmodule PlausibleWeb.RemoteIp docond docf_connecting_ip ->cf_connecting_ip+ |> clean_ipforwarded_for ->String.split(forwarded_for, ",")|> Enum.map(&String.trim/1)|> List.first()- |> remove_port+ |> clean_ipforwarded ->Regex.named_captures(~r/for=(?<for>[^;,]+).*$/, forwarded)|> Map.get("for")# IPv6 addresses are enclosed in quote marks and square brackets: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded|> String.trim("\"")- |> String.trim_leading("[")- |> String.trim_trailing("]")+ |> clean_iptrue ->to_string(:inet_parse.ntoa(conn.remote_ip))endend- defp remove_port(ip) do- String.split(ip, ":")- |> List.first()+ # Removes port from both IPv4 and IPv6 addresses. From https://regexr.com/3hpvt+ # Removes surrounding [] of an IPv6 address+ @port_regex ~r/((\.\d+)|(\]))(?<port>:[0-9]+)$/+ defp clean_ip(ip_and_port) do+ ip =+ case Regex.named_captures(@port_regex, ip_and_port) do+ %{"port" => port} -> String.trim_trailing(ip_and_port, port)+ _ -> ip_and_port+ end++ ip+ |> String.trim_leading("[")+ |> String.trim_trailing("]")endenddiff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exsindex e71f4be3be..6c65932078 100644--- a/test/plausible_web/controllers/api/external_controller_test.exs+++ b/test/plausible_web/controllers/api/external_controller_test.exs@@ -504,6 +504,38 @@ defmodule PlausibleWeb.Api.ExternalControllerTest doassert pageview.country_code == "US"end+ test "works with ipv6 without port in x-forwarded-for", %{conn: conn} do+ params = %{+ name: "pageview",+ domain: "external-controller-test-x-forwarded-for-ipv6.com",+ url: "http://gigride.live/"+ }++ conn+ |> put_req_header("x-forwarded-for", "1:1:1:1:1:1:1:1")+ |> post("/api/event", params)++ pageview = get_event("external-controller-test-x-forwarded-for-ipv6.com")++ assert pageview.country_code == "US"+ end++ test "works with ipv6 with a port number in x-forwarded-for", %{conn: conn} do+ params = %{+ name: "pageview",+ domain: "external-controller-test-x-forwarded-for-ipv6-port.com",+ url: "http://gigride.live/"+ }++ conn+ |> put_req_header("x-forwarded-for", "[1:1:1:1:1:1:1:1]:123")+ |> post("/api/event", params)++ pageview = get_event("external-controller-test-x-forwarded-for-ipv6-port.com")++ assert pageview.country_code == "US"+ end+test "uses cloudflare's special header for client IP address if present", %{conn: conn} doparams = %{name: "pageview",