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
Allow "Unknown" (ZZ) on country code block shield (#6021)
Adam Rutkowskib2578981d4aa
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6973862db6..1cb81b0085 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.### Added- Adds team_id to query debug metadata (saved in system.query_log log_comment column)+- Add "Unknown" option to Countries shield, for when the country code is unrecognized### Removeddiff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.exindex 5f2f7385b8..ee4d99e1f8 100644--- a/lib/plausible/ingestion/event.ex+++ b/lib/plausible/ingestion/event.ex@@ -344,7 +344,20 @@ defmodule Plausible.Ingestion.Event doendend- defp drop_shield_rule_country(%__MODULE__{} = event, _context), do: event+ defp drop_shield_rule_country(+ %__MODULE__{domain: domain} = event,+ _context+ )+ when is_binary(domain) do+ if Plausible.Shields.country_blocked?(+ domain,+ Plausible.Shield.CountryRule.unknown_country_code()+ ) do+ drop(event, :site_country_blocklist)+ else+ event+ end+ enddefp put_props(%__MODULE__{request: %{props: %{} = props}} = event, _context) do# defensive: ensuring the keys/values are always in the same orderdiff --git a/lib/plausible/shield/country_rule.ex b/lib/plausible/shield/country_rule.exindex 067928f340..1329f74bda 100644--- a/lib/plausible/shield/country_rule.ex+++ b/lib/plausible/shield/country_rule.ex@@ -19,13 +19,20 @@ defmodule Plausible.Shield.CountryRule dotimestamps()end+ @unknown_country_code "ZZ"++ @spec unknown_country_code() :: String.t()+ def unknown_country_code(), do: @unknown_country_code+def changeset(rule, attrs) dorule|> cast(attrs, [:site_id, :country_code])|> validate_required([:site_id, :country_code])|> validate_length(:country_code, is: 2)|> validate_change(:country_code, fn :country_code, cc ->- if cc in Enum.map(Location.Country.all(), & &1.alpha_2) do+ valid_codes = [@unknown_country_code | Enum.map(Location.Country.all(), & &1.alpha_2)]++ if cc in valid_codes do[]else[country_code: "is invalid"]diff --git a/lib/plausible_web/live/shields/country_rules.ex b/lib/plausible_web/live/shields/country_rules.exindex 067fafdd04..44d7803c25 100644--- a/lib/plausible_web/live/shields/country_rules.ex+++ b/lib/plausible_web/live/shields/country_rules.ex@@ -93,7 +93,6 @@ defmodule PlausibleWeb.Live.Shields.CountryRules do<.th invisible>Actions</.th></:thead><:tbody :let={rule}>- <% country = Location.Country.get_country(rule.country_code) %><.td><div class="flex items-center"><span@@ -101,7 +100,7 @@ defmodule PlausibleWeb.Live.Shields.CountryRules doclass="mr-4 cursor-help"title={"Added at #{format_added_at(rule.inserted_at, @site.timezone)} by #{rule.added_by}"}>- {country.flag} {country.name}+ {format_country(rule.country_code)}</span></div></.td>@@ -222,12 +221,22 @@ defmodule PlausibleWeb.Live.Shields.CountryRules doenddefp options(country_rules) do- Location.Country.all()- |> Enum.sort_by(& &1.name)- |> Enum.map(fn c -> {c.alpha_2, c.flag <> " " <> c.name} end)- |> Enum.reject(fn {country_code, _} ->- country_code in Enum.map(country_rules, & &1.country_code)- end)+ existing_codes = Enum.map(country_rules, & &1.country_code)++ unknown_option =+ if Shield.CountryRule.unknown_country_code() in existing_codes do+ []+ else+ [{Shield.CountryRule.unknown_country_code(), unknown()}]+ end++ country_options =+ Location.Country.all()+ |> Enum.sort_by(& &1.name)+ |> Enum.map(fn c -> {c.alpha_2, c.flag <> " " <> c.name} end)+ |> Enum.reject(fn {country_code, _} -> country_code in existing_codes end)++ country_options ++ unknown_optionenddefp format_added_at(dt, tz) do@@ -235,4 +244,15 @@ defmodule PlausibleWeb.Live.Shields.CountryRules do|> Plausible.Timezones.to_datetime_in_timezone(tz)|> Calendar.strftime("%Y-%m-%d %H:%M:%S")end++ defp format_country(country_code) do+ case Location.Country.get_country(country_code) do+ %Location.Country{} = country -> "#{country.flag} #{country.name}"+ nil -> unknown()+ end+ end++ defp unknown() do+ "🏴☠️ Unknown"+ endenddiff --git a/test/plausible/ingestion/event_test.exs b/test/plausible/ingestion/event_test.exsindex bb0a2fbf4b..f1e51d1d9a 100644--- a/test/plausible/ingestion/event_test.exs+++ b/test/plausible/ingestion/event_test.exs@@ -254,6 +254,45 @@ defmodule Plausible.Ingestion.EventTest doassert dropped.drop_reason == :site_country_blocklistend+ test "drops a request when country is unknown and Unknown (ZZ) is on blocklist" do+ site = new_site()++ payload = %{+ name: "pageview",+ url: "http://dummy.site",+ domain: site.domain+ }++ conn = build_conn(:post, "/api/events", payload)+ conn = %{conn | remote_ip: {127, 0, 0, 1}}++ {:ok, _} = Plausible.Shields.add_country_rule(site, %{"country_code" => "ZZ"})++ assert {:ok, request, _conn} = Request.build(conn)++ assert {:ok, %{buffered: [], dropped: [dropped]}} = Event.build_and_buffer(request)+ assert dropped.drop_reason == :site_country_blocklist+ end++ test "allows a request when country is unknown and Unknown (ZZ) is not on blocklist" do+ site = new_site()++ payload = %{+ name: "pageview",+ url: "http://dummy.site",+ domain: site.domain+ }++ conn = build_conn(:post, "/api/events", payload)+ conn = %{conn | remote_ip: {127, 0, 0, 1}}++ {:ok, _} = Plausible.Shields.add_country_rule(site, %{"country_code" => "EE"})++ assert {:ok, request, _conn} = Request.build(conn)++ assert {:ok, %{buffered: [_], dropped: []}} = Event.build_and_buffer(request)+ end+test "drops a request when page is on blocklist" dosite = new_site()diff --git a/test/plausible/shields/country_test.exs b/test/plausible/shields/country_test.exsindex 8b2218688c..801eb7c6c3 100644--- a/test/plausible/shields/country_test.exs+++ b/test/plausible/shields/country_test.exs@@ -20,6 +20,11 @@ defmodule Plausible.Shields.CountryTest dorefute changeset.valid?end+ test "accepts Unknown (ZZ) country code", %{site: site} do+ assert {:ok, rule} = add_country_rule(site, %{"country_code" => "ZZ"})+ assert rule.country_code == "ZZ"+ end+test "incorrect country format", %{site: site} doassert {:error, changeset} = add_country_rule(site, %{"country_code" => "Germany"})@@ -132,5 +137,11 @@ defmodule Plausible.Shields.CountryTest doassert country_blocked?(site, "ee")refute country_blocked?(site, "xx")end++ test "Unknown country (ZZ) can be blocked", %{site: site} do+ assert {:ok, rule} = add_country_rule(site, %{"country_code" => "ZZ"})+ assert rule.country_code == "ZZ"+ assert country_blocked?(site, "ZZ")+ endendenddiff --git a/test/plausible_web/live/shields/countries_test.exs b/test/plausible_web/live/shields/countries_test.exsindex 16364d8bf1..68cd90225d 100644--- a/test/plausible_web/live/shields/countries_test.exs+++ b/test/plausible_web/live/shields/countries_test.exs@@ -38,6 +38,17 @@ defmodule PlausibleWeb.Live.Shields.CountriesTest doassert text_of_attr(remove_button_2, "phx-value-rule-id") == r2.idend+ test "lists Unknown country rule", %{conn: conn, site: site} do+ {:ok, rule} =+ Shields.add_country_rule(site, %{"country_code" => "ZZ"})++ conn = get(conn, "/#{site.domain}/settings/shields/countries")+ resp = html_response(conn, 200)++ assert resp =~ "Unknown"+ assert find(resp, "#remove-country-rule-#{rule.id}")+ end+test "add rule button is rendered", %{conn: conn, site: site} doconn = get(conn, "/#{site.domain}/settings/shields/countries")resp = html_response(conn, 200)@@ -105,6 +116,22 @@ defmodule PlausibleWeb.Live.Shields.CountriesTest doassert tooltip =~ "by #{added_by}"end+ test "submitting Unknown (ZZ) country saves it", %{conn: conn, site: site} do+ lv = get_liveview(conn, site)++ lv+ |> element("form")+ |> render_submit(%{+ "country_rule[country_code]" => "ZZ"+ })++ html = render(lv)++ assert html =~ "Unknown"++ assert [%{country_code: "ZZ"}] = Shields.list_country_rules(site)+ end+test "clicking Remove deletes the rule", %{conn: conn, site: site} do{:ok, _} =Shields.add_country_rule(site, %{"country_code" => "EE"})