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""" -
+ Reject incoming traffic from specific IP addresses +
+ ++ You've reached the maximum number of IP addresses you can block. Please remove one before adding another. +
+
+ 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.
+
+ Once added, we will start rejecting traffic from this IP within a few minutes. +
++ No IP Rules configured for this Site. +
+| + IP Address + | ++ Status + | ++ Description + | ++ Remove + | +
|---|---|---|---|
|
+
+
+ <%= rule.inet %>
+
+
+
+
+ YOU
+
+
+ |
+ + + Blocked + + + Allowed + + | ++ + <%= rule.description %> + + + -- + + | ++ + | +