diff --git a/CHANGELOG.md b/CHANGELOG.md index f87cffa09..409e960b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ### Added +- IP Block List in Site Settings - Allow filtering by multiple custom properties - Wildcard and member filtering on the Stats API `event:goal` property - Allow filtering with `contains`/`matches` operator for custom properties diff --git a/lib/plausible/application.ex b/lib/plausible/application.ex index 182f9e12d..439888bea 100644 --- a/lib/plausible/application.ex +++ b/lib/plausible/application.ex @@ -41,6 +41,21 @@ defmodule Plausible.Application do interval: :timer.seconds(30), warmer_fn: :refresh_updated_recently ]}, + {Plausible.Shield.IPRuleCache, []}, + {Plausible.Cache.Warmer, + [ + child_name: Plausible.Shield.IPRuleCache.All, + cache_impl: Plausible.Shield.IPRuleCache, + interval: :timer.minutes(3) + Enum.random(1..:timer.seconds(10)), + warmer_fn: :refresh_all + ]}, + {Plausible.Cache.Warmer, + [ + child_name: Plausible.Shield.IPRuleCache.RecentlyUpdated, + cache_impl: Plausible.Shield.IPRuleCache, + interval: :timer.seconds(35), + warmer_fn: :refresh_updated_recently + ]}, {Plausible.Auth.TOTP.Vault, key: totp_vault_key()}, PlausibleWeb.Endpoint, {Oban, Application.get_env(:plausible, Oban)}, diff --git a/lib/plausible/cache.ex b/lib/plausible/cache.ex index 3c0a548cd..5f27bda82 100644 --- a/lib/plausible/cache.ex +++ b/lib/plausible/cache.ex @@ -53,6 +53,7 @@ defmodule Plausible.Cache do Application.fetch_env!(:plausible, __MODULE__)[:enabled] == true end + # credo:disable-for-this-file Credo.Check.Refactor.LongQuoteBlocks defmacro __using__(_opts) do quote do require Logger @@ -60,7 +61,7 @@ defmodule Plausible.Cache do @behaviour Plausible.Cache @modes [:all, :updated_recently] - @spec get(String.t(), Keyword.t()) :: any() | nil + @spec get(any(), Keyword.t()) :: any() | nil def get(key, opts \\ []) do cache_name = Keyword.get(opts, :cache_name, name()) force? = Keyword.get(opts, :force?, false) @@ -158,6 +159,9 @@ defmodule Plausible.Cache do 0 -> count_all() == 0 + + _ -> + false end end diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex index 881353b43..d31aebeba 100644 --- a/lib/plausible/ingestion/event.ex +++ b/lib/plausible/ingestion/event.ex @@ -26,6 +26,7 @@ defmodule Plausible.Ingestion.Event do | GateKeeper.policy() | :invalid | :dc_ip + | :site_ip_blocklist @type t() :: %__MODULE__{ domain: String.t() | nil, @@ -94,7 +95,8 @@ defmodule Plausible.Ingestion.Event do defp pipeline() do [ - &put_ip_classification/1, + &drop_datacenter_ip/1, + &drop_shield_rule_ip/1, &put_user_agent/1, &put_basic_info/1, &put_referrer/1, @@ -141,7 +143,7 @@ defmodule Plausible.Ingestion.Event do struct!(event, clickhouse_event_attrs: Map.merge(event.clickhouse_event_attrs, attrs)) end - defp put_ip_classification(%__MODULE__{} = event) do + defp drop_datacenter_ip(%__MODULE__{} = event) do case event.request.ip_classification do "dc_ip" -> drop(event, :dc_ip) @@ -151,6 +153,19 @@ defmodule Plausible.Ingestion.Event do end end + defp drop_shield_rule_ip(%__MODULE__{} = event) do + domain = event.domain + address = event.request.remote_ip + + case Plausible.Shield.IPRuleCache.get({domain, address}) do + %Plausible.Shield.IPRule{action: :deny} -> + drop(event, :site_ip_blocklist) + + _ -> + event + end + end + defp put_user_agent(%__MODULE__{} = event) do case parse_user_agent(event.request) do %UAInspector.Result{client: %UAInspector.Result.Client{name: "Headless Chrome"}} -> diff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.ex index 26689c56c..c5f6818e4 100644 --- a/lib/plausible/ingestion/request.ex +++ b/lib/plausible/ingestion/request.ex @@ -104,7 +104,7 @@ defmodule Plausible.Ingestion.Request do end defp put_remote_ip(changeset, conn) do - Changeset.put_change(changeset, :remote_ip, PlausibleWeb.RemoteIp.get(conn)) + Changeset.put_change(changeset, :remote_ip, PlausibleWeb.RemoteIP.get(conn)) end defp parse_body(conn) do diff --git a/lib/plausible/shield/ip_rule.ex b/lib/plausible/shield/ip_rule.ex new file mode 100644 index 000000000..d93026279 --- /dev/null +++ b/lib/plausible/shield/ip_rule.ex @@ -0,0 +1,42 @@ +defmodule Plausible.Shield.IPRule do + @moduledoc """ + Schema for IP block list + """ + use Ecto.Schema + import Ecto.Changeset + + @type t() :: %__MODULE__{} + + @primary_key {:id, :binary_id, autogenerate: true} + schema "shield_rules_ip" do + belongs_to :site, Plausible.Site + field :inet, EctoNetwork.INET + field :action, Ecto.Enum, values: [:deny, :allow], default: :deny + field :description, :string + field :added_by, :string + + # If `from_cache?` is set, the struct might be incomplete - see `Plausible.Site.Shield.Rules.IP.Cache` + field :from_cache?, :boolean, virtual: true, default: false + timestamps() + end + + def changeset(rule, attrs) do + rule + |> cast(attrs, [:site_id, :inet, :description, :added_by]) + |> validate_required([:site_id, :inet]) + |> disallow_netmask(:inet) + |> unique_constraint(:inet, + name: :shield_rules_ip_site_id_inet_index + ) + end + + defp disallow_netmask(changeset, field) do + case get_field(changeset, field) do + %Postgrex.INET{netmask: netmask} when netmask != 32 and netmask != 128 -> + add_error(changeset, field, "netmask unsupported") + + _ -> + changeset + end + end +end diff --git a/lib/plausible/shield/ip_rule_cache.ex b/lib/plausible/shield/ip_rule_cache.ex new file mode 100644 index 000000000..9b941ba19 --- /dev/null +++ b/lib/plausible/shield/ip_rule_cache.ex @@ -0,0 +1,65 @@ +defmodule Plausible.Shield.IPRuleCache do + @moduledoc """ + Allows retrieving IP Rules by domain and IP + """ + alias Plausible.Shield.IPRule + + import Ecto.Query + use Plausible.Cache + + @cache_name :ip_blocklist_by_domain + + @cached_schema_fields ~w( + id + inet + action + )a + + @impl true + def name(), do: @cache_name + + @impl true + def child_id(), do: :cachex_ip_blocklist + + @impl true + def count_all() do + Plausible.Repo.aggregate(IPRule, :count) + end + + @impl true + def base_db_query() do + from rule in IPRule, + inner_join: s in assoc(rule, :site), + select: { + s.domain, + s.domain_changed_from, + %{struct(rule, ^@cached_schema_fields) | from_cache?: true} + } + end + + @impl true + def get_from_source({domain, address}) do + query = + base_db_query() + |> where([rule, site], rule.inet == ^address and site.domain == ^domain) + + case Plausible.Repo.one(query) do + {_, _, rule} -> %IPRule{rule | from_cache?: false} + _any -> nil + end + end + + @impl true + def unwrap_cache_keys(items) do + Enum.reduce(items, [], fn + {domain, nil, object}, acc -> + [{{domain, to_string(object.inet)}, object} | acc] + + {domain, domain_changed_from, object}, acc -> + [ + {{domain, to_string(object.inet)}, object}, + {{domain_changed_from, to_string(object.inet)}, object} | acc + ] + end) + end +end diff --git a/lib/plausible/shields.ex b/lib/plausible/shields.ex new file mode 100644 index 000000000..9a0f1094a --- /dev/null +++ b/lib/plausible/shields.ex @@ -0,0 +1,72 @@ +defmodule Plausible.Shields do + @moduledoc """ + Contextual interface for shields. + """ + import Ecto.Query + alias Plausible.Repo + alias Plausible.Shield + + @maximum_ip_rules 30 + def maximum_ip_rules(), do: @maximum_ip_rules + + @spec list_ip_rules(Plausible.Site.t() | non_neg_integer()) :: [Shield.IPRule.t()] + def list_ip_rules(site_id) when is_integer(site_id) do + Repo.all( + from r in Shield.IPRule, + where: r.site_id == ^site_id, + order_by: [desc: r.inserted_at] + ) + end + + def list_ip_rules(%Plausible.Site{id: id}) do + list_ip_rules(id) + end + + @spec add_ip_rule(Plausible.Site.t() | non_neg_integer(), map()) :: + {:ok, Shield.IPRule.t()} | {:error, Ecto.Changeset.t()} + def add_ip_rule(site_id, params) when is_integer(site_id) do + Repo.transaction(fn -> + result = + if count_ip_rules(site_id) >= @maximum_ip_rules do + changeset = + %Shield.IPRule{} + |> Shield.IPRule.changeset(Map.put(params, "site_id", site_id)) + |> Ecto.Changeset.add_error(:inet, "maximum reached") + + {:error, changeset} + else + %Shield.IPRule{} + |> Shield.IPRule.changeset(Map.put(params, "site_id", site_id)) + |> Repo.insert() + end + + case result do + {:ok, rule} -> rule + {:error, changeset} -> Repo.rollback(changeset) + end + end) + end + + def add_ip_rule(%Plausible.Site{id: id}, params) do + add_ip_rule(id, params) + end + + @spec remove_ip_rule(Plausible.Site.t() | non_neg_integer(), String.t()) :: :ok + def remove_ip_rule(site_id, rule_id) when is_integer(site_id) do + Repo.delete_all(from(r in Shield.IPRule, where: r.site_id == ^site_id and r.id == ^rule_id)) + :ok + end + + def remove_ip_rule(%Plausible.Site{id: site_id}, rule_id) do + remove_ip_rule(site_id, rule_id) + end + + @spec count_ip_rules(Plausible.Site.t() | non_neg_integer()) :: non_neg_integer() + def count_ip_rules(site_id) when is_integer(site_id) do + Repo.aggregate(from(r in Shield.IPRule, where: r.site_id == ^site_id), :count) + end + + def count_ip_rules(%Plausible.Site{id: id}) do + count_ip_rules(id) + end +end diff --git a/lib/plausible_web/components/generic.ex b/lib/plausible_web/components/generic.ex index 3a6fb07a7..e0b792a5a 100644 --- a/lib/plausible_web/components/generic.ex +++ b/lib/plausible_web/components/generic.ex @@ -134,9 +134,10 @@ defmodule PlausibleWeb.Components.Generic do end attr :id, :any, default: nil - attr :href, :string, required: true + attr :href, :string, default: "#" attr :new_tab, :boolean, default: false attr :class, :string, default: "" + attr :rest, :global slot :inner_block def styled_link(assigns) do @@ -145,6 +146,7 @@ defmodule PlausibleWeb.Components.Generic do new_tab={@new_tab} href={@href} class={"text-indigo-600 hover:text-indigo-700 dark:text-indigo-500 dark:hover:text-indigo-600 " <> @class} + {@rest} > <%= render_slot(@inner_block) %> diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex index 7ba9fbe78..46622ee20 100644 --- a/lib/plausible_web/controllers/api/external_controller.ex +++ b/lib/plausible_web/controllers/api/external_controller.ex @@ -60,13 +60,14 @@ defmodule PlausibleWeb.Api.ExternalController do e -> "error: #{inspect(e)}" end - sites_cache_health = - if postgres_health == "ok" and Plausible.Site.Cache.ready?() do + cache_health = + if postgres_health == "ok" and Plausible.Site.Cache.ready?() and + Plausible.Shield.IPRuleCache.ready?() do "ok" end status = - case {postgres_health, clickhouse_health, sites_cache_health} do + case {postgres_health, clickhouse_health, cache_health} do {"ok", "ok", "ok"} -> 200 _ -> 500 end @@ -75,7 +76,7 @@ defmodule PlausibleWeb.Api.ExternalController do |> json(%{ postgres: postgres_health, clickhouse: clickhouse_health, - sites_cache: sites_cache_health + sites_cache: cache_health }) end diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index a84e6d960..fcf9ebee2 100644 --- a/lib/plausible_web/controllers/auth_controller.ex +++ b/lib/plausible_web/controllers/auth_controller.ex @@ -304,7 +304,7 @@ defmodule PlausibleWeb.AuthController do @email_change_interval :timer.hours(1) defp check_ip_rate_limit(conn) do - ip_address = PlausibleWeb.RemoteIp.get(conn) + ip_address = PlausibleWeb.RemoteIP.get(conn) case RateLimit.check_rate("login:ip:#{ip_address}", @login_interval, @login_limit) do {:allow, _} -> :ok diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index d4ddd4fe2..aa2775b0b 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -253,6 +253,18 @@ defmodule PlausibleWeb.SiteController do ) end + def settings_shields(conn, _params) do + site = conn.assigns.site + + conn + |> render("settings_shields.html", + site: site, + dogfood_page_path: "/:dashboard/settings/shields", + connect_live_socket: true, + layout: {PlausibleWeb.LayoutView, "site_settings.html"} + ) + end + def update_google_auth(conn, %{"google_auth" => attrs}) do site = conn.assigns[:site] |> Repo.preload(:google_auth) diff --git a/lib/plausible_web/live/components/modal.ex b/lib/plausible_web/live/components/modal.ex index 72e194f0b..f11c5ea16 100644 --- a/lib/plausible_web/live/components/modal.ex +++ b/lib/plausible_web/live/components/modal.ex @@ -53,7 +53,7 @@ defmodule PlausibleWeb.Live.Components.Modal do to ensure that. * `Modal.close/2` - to close the modal from the backend; usually done inside wrapped component's `handle_event/2`. The example - qouted above shows one way to implement this, under that assumption + quoted above shows one way to implement this, under that assumption that the component exposes a callback, like this: ``` diff --git a/lib/plausible_web/live/flash.ex b/lib/plausible_web/live/flash.ex index 60a0c72a5..2a9c5c8fa 100644 --- a/lib/plausible_web/live/flash.ex +++ b/lib/plausible_web/live/flash.ex @@ -45,7 +45,7 @@ defmodule PlausibleWeb.Live.Flash do def flash_messages(assigns) do ~H""" -
+
assign( + ip_rules_count: assigns.ip_rules_count, + remote_ip: assigns.remote_ip, + site: assigns.site, + current_user: assigns.current_user, + form: new_form() + ) + |> assign_new(:ip_rules, fn %{site: site} -> + Shields.list_ip_rules(site) + end) + + {:ok, socket} + end + + def render(assigns) do + ~H""" +
+
+
+

+ IP Block List +

+

+ Reject incoming traffic from specific IP addresses +

+ + +
+
+
+ + + Add IP Address + +
+ = Shields.maximum_ip_rules()} + class="mt-4" + title="Maximum number of addresses reached" + > +

+ You've reached the maximum number of IP addresses you can block. Please remove one before adding another. +

+
+
+ + <.live_component module={Modal} id="ip-rule-form-modal"> + <.form + :let={f} + for={@form} + phx-submit="save-ip-rule" + phx-target={@myself} + class="max-w-md w-full mx-auto bg-white dark:bg-gray-800" + > +

Add IP to Block List

+ + <.input + autofocus + field={f[:inet]} + label="IP Address" + class="focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-900 dark:text-gray-300 block w-7/12 rounded-md sm:text-sm border-gray-300 dark:border-gray-500 w-full p-2 mt-2" + placeholder="e.g. 192.168.127.12" + /> + +
+

+ Your current IP address is: <%= @remote_ip %> +
+ <.styled_link phx-target={@myself} phx-click="prefill-own-ip-rule"> + Click here + + to block your own traffic, or enter a custom address. +

+
+ + <.input + field={f[:description]} + label="Description" + class="focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-900 dark:text-gray-300 block w-7/12 rounded-md sm:text-sm border-gray-300 dark:border-gray-500 w-full p-2 mt-2" + placeholder="e.g. The Office" + /> + +

+ Once added, we will start rejecting traffic from this IP within a few minutes. +

+
+ + Add IP Address → + +
+ + + +

+ No IP Rules configured for this Site. +

+
+ + + + + + + + + + + <%= for rule <- @ip_rules do %> + + + + + + + <% end %> + +
+ IP Address + + Status + + Description + + Remove +
+
+ + <%= rule.inet %> + + + + + YOU + +
+
+ + Blocked + + + Allowed + + + + <%= rule.description %> + + + -- + + + +
+
+
+
+ """ + end + + def handle_event("prefill-own-ip-rule", %{}, socket) do + form = + %Plausible.Shield.IPRule{} + |> Plausible.Shield.IPRule.changeset(%{ + inet: socket.assigns.remote_ip, + description: socket.assigns.current_user.name + }) + |> to_form() + + {:noreply, assign(socket, form: form)} + end + + def handle_event("save-ip-rule", %{"ip_rule" => params}, socket) do + user = socket.assigns.current_user + + case Shields.add_ip_rule( + socket.assigns.site.id, + Map.put(params, "added_by", "#{user.name} <#{user.email}>") + ) do + {:ok, rule} -> + socket = + socket + |> Modal.close("ip-rule-form-modal") + |> assign( + form: new_form(), + ip_rules: [rule | socket.assigns.ip_rules], + ip_rules_count: socket.assigns.ip_rules_count + 1 + ) + + send_flash( + :success, + "IP rule added successfully. Traffic will be rejected within a few minutes." + ) + + {:noreply, socket} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, form: to_form(changeset))} + end + end + + def handle_event("remove-ip-rule", %{"rule-id" => rule_id}, socket) do + Shields.remove_ip_rule(socket.assigns.site.id, rule_id) + + send_flash( + :success, + "IP rule removed successfully. Traffic will be resumed within a few minutes." + ) + + {:noreply, + socket + |> assign( + ip_rules_count: socket.assigns.ip_rules_count - 1, + ip_rules: Enum.reject(socket.assigns.ip_rules, &(&1.id == rule_id)) + )} + end + + def send_flash(kind, message) do + send(self(), {:flash, kind, message}) + end + + defp new_form() do + %Shield.IPRule{} + |> Shield.IPRule.changeset(%{}) + |> to_form() + end + + defp ip_rule_present?(rules, ip) do + not is_nil(Enum.find(rules, &(to_string(&1.inet) == ip))) + end +end diff --git a/lib/plausible_web/live/shields/tabs.ex b/lib/plausible_web/live/shields/tabs.ex new file mode 100644 index 000000000..130511352 --- /dev/null +++ b/lib/plausible_web/live/shields/tabs.ex @@ -0,0 +1,59 @@ +defmodule PlausibleWeb.Live.Shields.Tabs do + @moduledoc """ + Currently only a placeholder module. Once more shields + are implemented it will display tabs with counters, + linking to their respective live views. + """ + use PlausibleWeb, :live_view + use Phoenix.HTML + + alias Plausible.Shields + alias Plausible.Sites + + def mount( + _params, + %{ + "remote_ip" => remote_ip, + "domain" => domain, + "current_user_id" => user_id + }, + socket + ) do + socket = + socket + |> assign_new(:site, fn -> + Sites.get_for_user!(user_id, domain, [:owner, :admin, :super_admin]) + end) + |> assign_new(:ip_rules_count, fn %{site: site} -> + Shields.count_ip_rules(site) + end) + |> assign_new(:current_user, fn -> + Plausible.Repo.get(Plausible.Auth.User, user_id) + end) + |> assign_new(:remote_ip, fn -> remote_ip end) + |> assign(:current_tab, :ip_rules) + + {:ok, socket} + end + + def render(assigns) do + ~H""" +
+ <.flash_messages flash={@flash} /> + <.live_component + module={PlausibleWeb.Live.Shields.IPRules} + current_user={@current_user} + ip_rules_count={@ip_rules_count} + site={@site} + remote_ip={@remote_ip} + id="ip-rules-#{@current_user.id}" + /> +
+ """ + end + + def handle_info({:flash, kind, message}, socket) do + socket = put_live_flash(socket, kind, message) + {:noreply, socket} + end +end diff --git a/lib/plausible_web/remote_ip.ex b/lib/plausible_web/remote_ip.ex index dd13aec7c..3fc68d15a 100644 --- a/lib/plausible_web/remote_ip.ex +++ b/lib/plausible_web/remote_ip.ex @@ -1,4 +1,8 @@ -defmodule PlausibleWeb.RemoteIp do +defmodule PlausibleWeb.RemoteIP do + @moduledoc """ + Implements the strategy of retrieving client's remote IP + """ + def get(conn) do x_plausible_ip = List.first(Plug.Conn.get_req_header(conn, "x-plausible-ip")) cf_connecting_ip = List.first(Plug.Conn.get_req_header(conn, "cf-connecting-ip")) @@ -24,7 +28,7 @@ defmodule PlausibleWeb.RemoteIp do |> 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("\"") - |> clean_ip + |> clean_ip() true -> to_string(:inet_parse.ntoa(conn.remote_ip)) @@ -50,6 +54,6 @@ defmodule PlausibleWeb.RemoteIp do String.split(header, ",") |> Enum.map(&String.trim/1) |> List.first() - |> clean_ip + |> clean_ip() end end diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index beea5f3e2..e89653d0e 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -354,6 +354,7 @@ defmodule PlausibleWeb.Router do get "/:website/settings/email-reports", SiteController, :settings_email_reports get "/:website/settings/danger-zone", SiteController, :settings_danger_zone get "/:website/settings/integrations", SiteController, :settings_integrations + get "/:website/settings/shields", SiteController, :settings_shields put "/:website/settings/features/visibility/:setting", SiteController, diff --git a/lib/plausible_web/templates/site/settings_shields.html.heex b/lib/plausible_web/templates/site/settings_shields.html.heex new file mode 100644 index 000000000..64ab4df6c --- /dev/null +++ b/lib/plausible_web/templates/site/settings_shields.html.heex @@ -0,0 +1,7 @@ +<%= live_render(@conn, PlausibleWeb.Live.Shields.Tabs, + session: %{ + "site_id" => @site.id, + "domain" => @site.domain, + "remote_ip" => PlausibleWeb.RemoteIP.get(@conn) + } +) %> diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex index 7af5bedcf..57307a5bf 100644 --- a/lib/plausible_web/views/layout_view.ex +++ b/lib/plausible_web/views/layout_view.ex @@ -51,6 +51,7 @@ defmodule PlausibleWeb.LayoutView do end, [key: "Custom Properties", value: "properties", icon: :document_text], [key: "Integrations", value: "integrations", icon: :arrow_path_rounded_square], + [key: "Shields", value: "shields", icon: :shield_exclamation], [key: "Email Reports", value: "email-reports", icon: :envelope], if conn.assigns[:current_user_role] == :owner do [key: "Danger Zone", value: "danger-zone", icon: :exclamation_triangle] diff --git a/mix.exs b/mix.exs index 058d341a9..8bd488ca7 100644 --- a/mix.exs +++ b/mix.exs @@ -134,7 +134,8 @@ defmodule Plausible.MixProject do {:scrivener_ecto, "~> 2.0"}, {:esbuild, "~> 0.7", runtime: Mix.env() in [:dev, :small_dev]}, {:tailwind, "~> 0.2.0", runtime: Mix.env() in [:dev, :small_dev]}, - {:ex_json_logger, "~> 1.4.0"} + {:ex_json_logger, "~> 1.4.0"}, + {:ecto_network, "~> 1.5.0"} ] end diff --git a/mix.lock b/mix.lock index 460bd2911..8a108e96e 100644 --- a/mix.lock +++ b/mix.lock @@ -33,6 +33,7 @@ "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, "ecto": {:hex, :ecto, "3.11.1", "4b4972b717e7ca83d30121b12998f5fcdc62ba0ed4f20fd390f16f3270d85c3e", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ebd3d3772cd0dfcd8d772659e41ed527c28b2a8bde4b00fe03e0463da0f1983b"}, "ecto_ch": {:hex, :ecto_ch, "0.3.2", "b6e7d0a6ad412662d7727ba1b5128a1a30a0835e1b4168db79e5b2551a27ba50", [:mix], [{:ch, "~> 0.2.0", [hex: :ch, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "a195d0f09d1177d9b11f00ef608895db1e02711d0a18730e98fbb50055758e6d"}, + "ecto_network": {:hex, :ecto_network, "1.5.0", "a930c910975e7a91237b858ebf0f4ad7b2aae32fa846275aa203cb858459ec73", [:mix], [{:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 0.0.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.14.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "4d614434ae3e6d373a2f693d56aafaa3f3349714668ffd6d24e760caf578aa2f"}, "ecto_sql": {:hex, :ecto_sql, "3.11.1", "e9abf28ae27ef3916b43545f9578b4750956ccea444853606472089e7d169470", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.11.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ce14063ab3514424276e7e360108ad6c2308f6d88164a076aac8a387e1fea634"}, "elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"}, "envy": {:hex, :envy, "1.1.1", "0bc9bd654dec24fcdf203f7c5aa1b8f30620f12cfb28c589d5e9c38fe1b07475", [:mix], [], "hexpm", "7061eb1a47415fd757145d8dec10dc0b1e48344960265cb108f194c4252c3a89"}, diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 14cf3fe13..a5cbdf3a4 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -49,6 +49,8 @@ site = ] ) +Plausible.Factory.insert_list(29, :ip_rule, site: site) + Plausible.Factory.insert(:google_auth, user: user, site: site, diff --git a/test/plausible/cache_test.exs b/test/plausible/cache_test.exs index a29734e77..0e66c25f6 100644 --- a/test/plausible/cache_test.exs +++ b/test/plausible/cache_test.exs @@ -52,6 +52,10 @@ defmodule Plausible.CacheTest do assert log =~ "Error retrieving key from 'NonExistingCache': :no_cache" end + + test "cache is not ready when it doesn't exist", %{test: test} do + refute ExampleCache.ready?(test) + end end describe "merging cache items" do diff --git a/test/plausible/ingestion/event_test.exs b/test/plausible/ingestion/event_test.exs index 8388a4f6b..ca6dba919 100644 --- a/test/plausible/ingestion/event_test.exs +++ b/test/plausible/ingestion/event_test.exs @@ -117,6 +117,26 @@ defmodule Plausible.Ingestion.EventTest do assert dropped.drop_reason == :dc_ip end + test "event pipeline drops a request when ip is on blocklist" do + site = insert(:site) + + payload = %{ + name: "pageview", + url: "http://dummy.site", + domain: site.domain + } + + conn = build_conn(:post, "/api/events", payload) + conn = %{conn | remote_ip: {127, 7, 7, 7}} + + {:ok, _} = Plausible.Shields.add_ip_rule(site, %{"inet" => "127.7.7.7"}) + + assert {:ok, request} = Request.build(conn) + + assert {:ok, %{buffered: [], dropped: [dropped]}} = Event.build_and_buffer(request) + assert dropped.drop_reason == :site_ip_blocklist + end + test "event pipeline drops events for site with accept_trafic_until in the past" do yesterday = Date.add(Date.utc_today(), -1) diff --git a/test/plausible/shield/ip_rule_cache_test.exs b/test/plausible/shield/ip_rule_cache_test.exs new file mode 100644 index 000000000..e455bbfcb --- /dev/null +++ b/test/plausible/shield/ip_rule_cache_test.exs @@ -0,0 +1,80 @@ +defmodule Plausible.Shield.IPRuleCacheTest do + use Plausible.DataCase, async: true + + alias Plausible.Shield.IPRule + alias Plausible.Shield.IPRuleCache + alias Plausible.Shields + + describe "public cache interface" do + test "cache caches IP rules", %{test: test} do + {:ok, _} = + Supervisor.start_link([{IPRuleCache, [cache_name: test, child_id: :ip_rules_cache_id]}], + strategy: :one_for_one, + name: :"cache_supervisor_#{test}" + ) + + site = insert(:site, domain: "site1.example.com") + + {:ok, %{id: rid1}} = Shields.add_ip_rule(site, %{"inet" => "1.1.1.1"}) + {:ok, %{id: rid2}} = Shields.add_ip_rule(site, %{"inet" => "2.2.2.2"}) + + :ok = IPRuleCache.refresh_all(cache_name: test) + + :ok = Shields.remove_ip_rule(site, rid1) + + assert IPRuleCache.size(test) == 2 + + assert %IPRule{from_cache?: true, id: ^rid1} = + IPRuleCache.get({site.domain, "1.1.1.1"}, force?: true, cache_name: test) + + assert %IPRule{from_cache?: true, id: ^rid2} = + IPRuleCache.get({site.domain, "2.2.2.2"}, force?: true, cache_name: test) + + refute IPRuleCache.get({site.domain, "3.3.3.3"}, cache_name: test, force?: true) + end + + test "cache allows lookups for sites with changed domain", %{test: test} do + {:ok, _} = start_test_cache(test) + site = insert(:site, domain: "new.example.com", domain_changed_from: "old.example.com") + + {:ok, _} = Shields.add_ip_rule(site, %{"inet" => "1.1.1.1"}) + :ok = IPRuleCache.refresh_all(cache_name: test) + + assert IPRuleCache.get({"old.example.com", "1.1.1.1"}, force?: true, cache_name: test) + assert IPRuleCache.get({"new.example.com", "1.1.1.1"}, force?: true, cache_name: test) + end + + test "refreshes only recently added rules", %{test: test} do + {:ok, _} = start_test_cache(test) + + domain = "site1.example.com" + site = insert(:site, domain: domain) + + cache_opts = [cache_name: test, force?: true] + + yesterday = DateTime.utc_now() |> DateTime.add(-1 * 60 * 60 * 24) + + insert(:ip_rule, + site: site, + inserted_at: yesterday, + updated_at: yesterday, + inet: "1.1.1.1" + ) + + insert(:ip_rule, site: site, inet: "2.2.2.2") + + assert IPRuleCache.get({domain, "1.1.1.1"}, cache_opts) == nil + assert IPRuleCache.get({domain, "2.2.2.2"}, cache_opts) == nil + + assert :ok = IPRuleCache.refresh_updated_recently(cache_opts) + + refute IPRuleCache.get({domain, "1.1.1.1"}, cache_opts) + assert %IPRule{} = IPRuleCache.get({domain, "2.2.2.2"}, cache_opts) + end + end + + defp start_test_cache(cache_name) do + %{start: {m, f, a}} = IPRuleCache.child_spec(cache_name: cache_name) + apply(m, f, a) + end +end diff --git a/test/plausible/shields_test.exs b/test/plausible/shields_test.exs new file mode 100644 index 000000000..80b17fc8a --- /dev/null +++ b/test/plausible/shields_test.exs @@ -0,0 +1,141 @@ +defmodule Plausible.ShieldsTest do + use Plausible.DataCase + import Plausible.Shields + + setup do + site = insert(:site) + {:ok, %{site: site}} + end + + describe "add_ip_rule/2" do + test "no input", %{site: site} do + assert {:error, changeset} = add_ip_rule(site, %{}) + assert changeset.errors == [inet: {"can't be blank", [validation: :required]}] + refute changeset.valid? + end + + test "unsupported netmask", %{site: site} do + assert {:error, changeset} = add_ip_rule(site, %{"inet" => "127.0.0.0/24"}) + assert changeset.errors == [inet: {"netmask unsupported", []}] + refute changeset.valid? + assert {:ok, _} = add_ip_rule(site, %{"inet" => "127.0.0.0/32"}) + end + + test "incorrect ip", %{site: site} do + assert {:error, changeset} = add_ip_rule(site, %{"inet" => "999.999.999.999"}) + + assert changeset.errors == [ + inet: {"is invalid", [{:type, EctoNetwork.INET}, {:validation, :cast}]} + ] + + refute changeset.valid? + end + + test "non-strict IPs", %{site: site} do + assert {:error, _} = add_ip_rule(site, %{"inet" => "111"}) + end + + test "double insert", %{site: site} do + assert {:ok, _} = add_ip_rule(site, %{"inet" => "0.0.0.111"}) + assert {:error, changeset} = add_ip_rule(site, %{"inet" => "0.0.0.111"}) + refute changeset.valid? + + assert changeset.errors == [ + inet: + {"has already been taken", + [ + {:constraint, :unique}, + {:constraint_name, "shield_rules_ip_site_id_inet_index"} + ]} + ] + end + + test "ipv6", %{site: site} do + assert {:ok, rule} = + add_ip_rule(site, %{"inet" => "2001:0000:130F:0000:0000:09C0:876A:130B"}) + + assert ^rule = Repo.get(Plausible.Shield.IPRule, rule.id) + end + + test "ipv4", %{site: site} do + assert {:ok, rule} = + add_ip_rule(site, %{"inet" => "1.1.1.1"}) + + assert ^rule = Repo.get(Plausible.Shield.IPRule, rule.id) + end + + test "over limit", %{site: site} do + for i <- 1..maximum_ip_rules() do + assert {:ok, _} = + add_ip_rule(site, %{"inet" => "1.1.1.#{i}"}) + end + + assert count_ip_rules(site) == maximum_ip_rules() + + assert {:error, changeset} = + add_ip_rule(site, %{"inet" => "1.1.1.31"}) + + refute changeset.valid? + assert changeset.errors == [inet: {"maximum reached", []}] + end + + test "with added_by", %{site: site} do + assert {:ok, rule} = add_ip_rule(site, %{"inet" => "1.1.1.1", "added_by" => "test"}) + assert rule.added_by == "test" + end + end + + describe "remove_ip_rule/2" do + test "is idempontent", %{site: site} do + {:ok, rule} = add_ip_rule(site, %{"inet" => "127.0.0.1"}) + assert remove_ip_rule(site, rule.id) == :ok + refute Repo.get(Plausible.Shield.IPRule, rule.id) + assert remove_ip_rule(site, rule.id) == :ok + end + end + + describe "list_ip_rules/1" do + test "empty", %{site: site} do + assert(list_ip_rules(site) == []) + end + + @tag :slow + test "many", %{site: site} do + {:ok, r1} = add_ip_rule(site, %{"inet" => "127.0.0.1"}) + :timer.sleep(1000) + {:ok, r2} = add_ip_rule(site, %{"inet" => "127.0.0.2"}) + assert [^r2, ^r1] = list_ip_rules(site) + end + end + + describe "count_ip_rules/1" do + test "counts", %{site: site} do + assert count_ip_rules(site) == 0 + {:ok, _} = add_ip_rule(site, %{"inet" => "127.0.0.1"}) + assert count_ip_rules(site) == 1 + {:ok, _} = add_ip_rule(site, %{"inet" => "127.0.0.2"}) + assert count_ip_rules(site) == 2 + end + end + + describe "IP Rules" do + test "end to end", %{site: site} do + site2 = insert(:site) + + assert count_ip_rules(site.id) == 0 + assert list_ip_rules(site.id) == [] + + assert {:ok, rule} = + add_ip_rule(site.id, %{"inet" => "127.0.0.1", "description" => "Localhost"}) + + add_ip_rule(site2, %{"inet" => "127.0.0.1", "description" => "Localhost"}) + + assert count_ip_rules(site) == 1 + assert [^rule] = list_ip_rules(site) + assert rule.inet == %Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32} + assert rule.description == "Localhost" + assert rule.action == :deny + refute rule.from_cache? + end + end +end diff --git a/test/plausible_web/live/shields_test.exs b/test/plausible_web/live/shields_test.exs new file mode 100644 index 000000000..355e52b39 --- /dev/null +++ b/test/plausible_web/live/shields_test.exs @@ -0,0 +1,208 @@ +defmodule PlausibleWeb.Live.ShieldsTest do + use PlausibleWeb.ConnCase, async: true + + import Phoenix.LiveViewTest + import Plausible.Test.Support.HTML + + alias Plausible.Shields + + setup [:create_user, :create_site, :log_in] + + describe "IP Rules - static" do + test "renders ip rules page with empty list", %{conn: conn, site: site} do + conn = get(conn, "/#{site.domain}/settings/shields") + resp = html_response(conn, 200) + + assert resp =~ "No IP Rules configured for this Site" + assert resp =~ "IP Block List" + end + + test "lists ip rules with remove actions", %{conn: conn, site: site} do + {:ok, r1} = + Shields.add_ip_rule(site, %{"inet" => "127.0.0.1", "description" => "Alice"}) + + {:ok, r2} = + Shields.add_ip_rule(site, %{"inet" => "127.0.0.2", "description" => "Bob"}) + + conn = get(conn, "/#{site.domain}/settings/shields") + resp = html_response(conn, 200) + + assert resp =~ "127.0.0.1" + assert resp =~ "Alice" + + assert resp =~ "127.0.0.2" + assert resp =~ "Bob" + + assert element_exists?( + resp, + ~s/button[phx-click="remove-ip-rule"][phx-value-rule-id="#{r1.id}"]#remove-ip-rule-#{r1.id}/ + ) + + assert element_exists?( + resp, + ~s/button[phx-click="remove-ip-rule"][phx-value-rule-id="#{r2.id}"]#remove-ip-rule-#{r2.id}/ + ) + end + + test "add rule button is rendered", %{conn: conn, site: site} do + conn = get(conn, "/#{site.domain}/settings/shields") + resp = html_response(conn, 200) + + assert element_exists?(resp, ~s/button#add-ip-rule[x-data]/) + attr = text_of_attr(resp, ~s/button#add-ip-rule/, "x-on:click") + + assert attr =~ "open-modal" + assert attr =~ "ip-rule-form-modal" + end + + test "add rule button is not rendered when maximum reached", %{conn: conn, site: site} do + for i <- 1..Shields.maximum_ip_rules() do + assert {:ok, _} = + Shields.add_ip_rule(site, %{"inet" => "1.1.1.#{i}"}) + end + + conn = get(conn, "/#{site.domain}/settings/shields") + resp = html_response(conn, 200) + + refute element_exists?(resp, ~s/button#add-ip-rule[x-data]/) + assert resp =~ "Maximum number of addresses reached" + end + end + + describe "IP Rules - LiveView" do + test "modal contains form", %{site: site, conn: conn} do + lv = get_liveview(conn, site) + html = render(lv) + + assert element_exists?( + html, + ~s/form[phx-submit="save-ip-rule"] input[name="ip_rule\[inet\]"]/ + ) + + assert element_exists?( + html, + ~s/form[phx-submit="save-ip-rule"] input[name="ip_rule\[description\]"]/ + ) + + assert submit_button(html, ~s/form[phx-submit="save-ip-rule"]/) + end + + test "form modal contains link to add own IP", %{site: site, conn: conn} do + ip = PlausibleWeb.RemoteIP.get(conn) + lv = get_liveview(conn, site) + html = render(lv) + + assert text(html) =~ "Your current IP address is: #{ip}" + assert element_exists?(html, ~s/a[phx-click="prefill-own-ip-rule"]/) + end + + test "form modal does not contains link to add own IP if already added", %{ + site: site, + conn: conn + } do + ip = PlausibleWeb.RemoteIP.get(conn) + + {:ok, _} = + Shields.add_ip_rule(site, %{ + "inet" => ip, + "description" => "Alice" + }) + + lv = get_liveview(conn, site) + html = render(lv) + + refute text(html) =~ "Your current IP address is: #{ip}" + refute element_exists?(html, ~s/a[phx-click="prefill-own-ip-rule"]/) + end + + test "clicking the link prefills own IP", %{conn: conn, site: site, user: user} do + lv = get_liveview(conn, site) + lv |> element(~s/a[phx-click="prefill-own-ip-rule"]/) |> render_click() + + html = render(lv) + + assert text_of_attr(html, "input[name=\"ip_rule[inet]\"]", "value") == + PlausibleWeb.RemoteIP.get(conn) + + assert text_of_attr(html, "input[name=\"ip_rule[description]\"]", "value") == user.name + end + + test "submitting own IP saves it", %{conn: conn, site: site, user: user} do + ip = PlausibleWeb.RemoteIP.get(conn) + assert [] = Shields.list_ip_rules(site) + + lv = get_liveview(conn, site) + lv |> element(~s/a[phx-click="prefill-own-ip-rule"]/) |> render_click() + lv |> element(~s/form/) |> render_submit() + + html = render(lv) + + assert html =~ ip + assert html =~ user.name + + assert [%{id: id}] = Shields.list_ip_rules(site) + + tooltip = text_of_attr(html, "#inet-#{id}", "title") + assert tooltip =~ "Added at #{Date.utc_today()}" + assert tooltip =~ "by #{user.name} <#{user.email}>" + + assert [_] = Shields.list_ip_rules(site) + end + + test "submitting a valid IP saves it", %{conn: conn, site: site} do + lv = get_liveview(conn, site) + + lv + |> element("form") + |> render_submit(%{ + "ip_rule[inet]" => "1.1.1.1", + "ip_rule[description]" => "A happy song" + }) + + html = render(lv) + + assert html =~ "1.1.1.1" + assert html =~ "A happy song" + + assert [%{inet: ip, description: "A happy song"}] = Shields.list_ip_rules(site) + assert to_string(ip) == "1.1.1.1" + end + + test "submitting invalid IP renders error", %{conn: conn, site: site} do + lv = get_liveview(conn, site) + + lv + |> element("form") + |> render_submit(%{ + "ip_rule[inet]" => "WRONG" + }) + + html = render(lv) + assert html =~ "is invalid" + end + + test "clicking Remove deletes the rule", %{conn: conn, site: site} do + {:ok, _} = + Shields.add_ip_rule(site, %{"inet" => "2.2.2.2", "description" => "Alice"}) + + lv = get_liveview(conn, site) + + html = render(lv) + assert html =~ "2.2.2.2" + + lv |> element(~s/button[phx-click="remove-ip-rule"]/) |> render_click() + + html = render(lv) + refute html =~ "2.2.2.2" + + assert Shields.count_ip_rules(site) == 0 + end + + defp get_liveview(conn, site) do + conn = assign(conn, :live_module, PlausibleWeb.Live.Shields) + {:ok, lv, _html} = live(conn, "/#{site.domain}/settings/shields") + + lv + end + end +end diff --git a/test/support/factory.ex b/test/support/factory.ex index 81725bc85..7c1ced73e 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -278,6 +278,14 @@ defmodule Plausible.Factory do } end + def ip_rule_factory do + %Plausible.Shield.IPRule{ + inet: Plausible.TestUtils.random_ip(), + description: "Test IP Rule", + added_by: "Mr Seed " + } + end + defp hash_key() do Keyword.fetch!( Application.get_env(:plausible, PlausibleWeb.Endpoint),