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
Enforce stats requests limit by team (#6010)
Artur Pata400bdecf3ea8
diff --git a/config/ce.exs b/config/ce.exsindex 6ebd95e0e3..9e2381d640 100644--- a/config/ce.exs+++ b/config/ce.exs@@ -15,3 +15,5 @@ config :esbuild,cd: Path.expand("../assets", __DIR__),env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}]++config :plausible, Plausible.Auth.ApiKey, legacy_per_user_hourly_request_limit: 1_000_000diff --git a/config/config.exs b/config/config.exsindex abe2969b88..326858d2d6 100644--- a/config/config.exs+++ b/config/config.exs@@ -88,4 +88,6 @@ config :sentry,config :prom_ex, :storage_adapter, Plausible.PromEx.StripedPeepconfig :peep, :bucket_calculator, Plausible.PromEx.Buckets+config :plausible, Plausible.Auth.ApiKey, legacy_per_user_hourly_request_limit: 600+import_config "#{config_env()}.exs"diff --git a/config/test.exs b/config/test.exsindex 17f97b1398..1fcf16c553 100644--- a/config/test.exs+++ b/config/test.exs@@ -62,6 +62,8 @@ config :plausible, Plausible.InstallationSupport.Checks.VerifyInstallation,plug: {Req.Test, Plausible.InstallationSupport.Checks.VerifyInstallation}]+config :plausible, Plausible.Auth.ApiKey, legacy_per_user_hourly_request_limit: 100+config :plausible, Plausible.Session.Salts, interval: :timer.hours(1)config :plausible, max_goals_per_site: 10diff --git a/lib/plausible/auth/api_key.ex b/lib/plausible/auth/api_key.exindex b1033119c0..529768c9e8 100644--- a/lib/plausible/auth/api_key.ex+++ b/lib/plausible/auth/api_key.ex@@ -1,4 +1,35 @@defmodule Plausible.Auth.ApiKey do+ @moduledoc """+ There are two kinds of API keys, legacy API keys and team-scoped API keys.++ Legacy keys have `team` / `team_id` set to `nil`.++ Legacy Stats API keys can be used to+ - access the stats of sites of any team of the user,+ - access the stats of sites that they are a guest of,+ - access data about the sites of the teams that they belong to,+ - access data about sites that they are a guest of.++ Legacy Sites API keys allow the above and additionally+ - to provision sites for any team of the user,+ - to configure sites for any team of the user,+ - to configure sites that they are a guest of.++ It's not possible to create legacy keys any more through the UI.++ Team-scoped keys have `team` / `team_id` set to a team.++ Team-scoped Stats API keys can be used to+ - access the stats of sites of that team,+ - access data about the sites of that team.++ Team-scoped Sites API keys allow the above and additionally+ - to provision sites for that team,+ - to configure sites for that team.++ Only team members can use team-scoped keys.+ """+use Plausibleuse Ecto.Schemaimport Ecto.Changeset@@ -8,7 +39,7 @@ defmodule Plausible.Auth.ApiKey do@required [:user_id, :name]@optional [:key, :scopes]- @hourly_request_limit on_ee(do: 600, else: 1_000_000)+ @default_hourly_request_limit_per_team on_ee(do: 600, else: 1_000_000)schema "api_keys" dofield :name, :string@@ -26,25 +57,27 @@ defmodule Plausible.Auth.ApiKey dotimestamps()end- def hourly_request_limit(), do: @hourly_request_limit+ def default_hourly_request_limit(), do: @default_hourly_request_limit_per_team+ def limit_key(team), do: "api_request:team:#{team.identifier}"- def changeset(struct, team, attrs) do+ def legacy_hourly_request_limit() do+ Application.fetch_env!(:plausible, __MODULE__)+ |> Keyword.fetch!(:legacy_per_user_hourly_request_limit)+ end++ def legacy_limit_key(user), do: "api_request:legacy_user:#{user.id}"++ def changeset(struct, team, attrs) when not is_nil(team) dostruct|> cast(attrs, @required ++ @optional)|> validate_required(@required)|> maybe_put_key()|> process_key()- |> maybe_put_team(team)+ |> put_assoc(:team, team)|> unique_constraint(:key_hash, error_key: :key)|> unique_constraint([:team_id, :user_id], error_key: :team)end- def update(struct, attrs \\ %{}) do- struct- |> cast(attrs, [:name, :user_id, :scopes])- |> validate_required([:user_id, :name])- end-def do_hash(key) do:crypto.hash(:sha256, [secret_key_base(), key])|> Base.encode16()@@ -62,12 +95,6 @@ defmodule Plausible.Auth.ApiKey dodef process_key(changeset), do: changeset- defp maybe_put_team(changeset, nil), do: changeset-- defp maybe_put_team(changeset, team) do- put_assoc(changeset, :team, team)- end-defp maybe_put_key(changeset) doif get_change(changeset, :key) dochangesetdiff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.exindex 4c6ab1b5f7..7831aa3752 100644--- a/lib/plausible/auth/auth.ex+++ b/lib/plausible/auth/auth.ex@@ -239,13 +239,22 @@ defmodule Plausible.Auth doendend- @spec find_api_key(String.t(), Keyword.t()) ::+ @spec find_api_key(String.t()) ::+ {:ok, %{api_key: Auth.ApiKey.t(), team: Teams.Team.t() | nil}}+ | {:error, :invalid_api_key}+ def find_api_key(raw_key) do+ find_api_key(raw_key, nil, nil)+ end++ @spec find_api_key_for_team_of_site(String.t(), String.t() | nil) ::{:ok, %{api_key: Auth.ApiKey.t(), team: Teams.Team.t() | nil}}| {:error, :invalid_api_key | :missing_site_id}- def find_api_key(raw_key, opts \\ []) do- {team_scope, id} = Keyword.get(opts, :team_by, {nil, nil})+ def find_api_key_for_team_of_site(_raw_key, site_domain) when site_domain in [nil, ""] do+ {:error, :missing_site_id}+ end- find_api_key(raw_key, team_scope, id)+ def find_api_key_for_team_of_site(raw_key, site_domain) do+ find_api_key(raw_key, :site, site_domain)enddefp scope_api_keys_by_team(query, nil) dodiff --git a/lib/plausible/teams/team.ex b/lib/plausible/teams/team.exindex 0c0b3862ae..bd4872c3df 100644--- a/lib/plausible/teams/team.ex+++ b/lib/plausible/teams/team.ex@@ -49,8 +49,8 @@ defmodule Plausible.Teams.Team dofield :setup_complete, :boolean, default: falsefield :setup_at, :naive_datetime- # Field kept in sync with current subscription plan, if any- field :hourly_api_request_limit, :integer, default: Auth.ApiKey.hourly_request_limit()+ # Field synced from current subscription plan, if any. The value of this field is treated as the source of truth when out of sync+ field :hourly_api_request_limit, :integer, default: Auth.ApiKey.default_hourly_request_limit()# Field for purely informational purposes in CRM contextfield :notes, :stringdiff --git a/lib/plausible_web/plugs/authorize_public_api.ex b/lib/plausible_web/plugs/authorize_public_api.exindex e2cae4f850..d14538c4eb 100644--- a/lib/plausible_web/plugs/authorize_public_api.ex+++ b/lib/plausible_web/plugs/authorize_public_api.ex@@ -66,9 +66,10 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPI do### Verification dispatched by scopedefp find_api_key(conn, token, :site) do- case Auth.find_api_key(token, team_by: {:site, conn.params["site_id"]}) do+ case Auth.find_api_key_for_team_of_site(token, conn.params["site_id"]) do{:ok, %{api_key: api_key, team: nil}} ->- {:ok, api_key, limit_key(api_key, nil), Auth.ApiKey.hourly_request_limit()}+ {:ok, api_key, Auth.ApiKey.legacy_limit_key(api_key.user),+ Auth.ApiKey.legacy_hourly_request_limit()}{:ok, %{api_key: api_key, team: team}} ->team_role_result = Plausible.Teams.Memberships.team_role(team, api_key.user)@@ -91,7 +92,7 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPI do:passend- {:ok, api_key, limit_key(api_key, team.identifier), team.hourly_api_request_limit}+ {:ok, api_key, Auth.ApiKey.limit_key(team), team.hourly_api_request_limit}{:error, _} = error ->error@@ -101,24 +102,17 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPI dodefp find_api_key(_conn, token, _) docase Auth.find_api_key(token) do{:ok, %{api_key: api_key, team: nil}} ->- {:ok, api_key, limit_key(api_key, nil), Auth.ApiKey.hourly_request_limit()}+ {:ok, api_key, Auth.ApiKey.legacy_limit_key(api_key.user),+ Auth.ApiKey.legacy_hourly_request_limit()}{:ok, %{api_key: api_key, team: team}} ->- {:ok, api_key, limit_key(api_key, team.identifier), team.hourly_api_request_limit}+ {:ok, api_key, Auth.ApiKey.limit_key(team), team.hourly_api_request_limit}{:error, _} = error ->errorendend- defp limit_key(api_key, nil) do- "api_request:#{api_key.id}"- end-- defp limit_key(api_key, team_id) do- "api_request:#{api_key.id}:#{team_id}"- end-defp verify_by_scope(conn, api_key, "stats:read:" <> _ = scope) dowith :ok <- check_scope(api_key, scope),{:ok, site} <- find_site(conn.params["site_id"]),diff --git a/test/plausible/data_migration/backfill_teams_hourly_request_limit_test.exs b/test/plausible/data_migration/backfill_teams_hourly_request_limit_test.exsindex bc44efd9ed..400c972ddc 100644--- a/test/plausible/data_migration/backfill_teams_hourly_request_limit_test.exs+++ b/test/plausible/data_migration/backfill_teams_hourly_request_limit_test.exs@@ -59,9 +59,14 @@ defmodule Plausible.DataMigration.BackfillTeamsHourlyRequestLimitTest doassert Repo.reload(team1).hourly_api_request_limit == 5000- assert Repo.reload(team2).hourly_api_request_limit == Auth.ApiKey.hourly_request_limit()- assert Repo.reload(team3).hourly_api_request_limit == Auth.ApiKey.hourly_request_limit()- assert Repo.reload(team4).hourly_api_request_limit == Auth.ApiKey.hourly_request_limit()+ assert Repo.reload(team2).hourly_api_request_limit ==+ Auth.ApiKey.default_hourly_request_limit()++ assert Repo.reload(team3).hourly_api_request_limit ==+ Auth.ApiKey.default_hourly_request_limit()++ assert Repo.reload(team4).hourly_api_request_limit ==+ Auth.ApiKey.default_hourly_request_limit()endendenddiff --git a/test/plausible/rate_limit_test.exs b/test/plausible/rate_limit_test.exsindex a9c2e35778..f95307c5c6 100644--- a/test/plausible/rate_limit_test.exs+++ b/test/plausible/rate_limit_test.exs@@ -1,6 +1,7 @@defmodule Plausible.RateLimitTest douse ExUnit.Case, async: truealias Plausible.RateLimit+ use Plausible.TestUtils@table __MODULE__@@ -64,5 +65,23 @@ defmodule Plausible.RateLimitTest doassert {:allow, 3} = RateLimit.check_rate(@table, key, scale, limit)assert {:deny, 3} = RateLimit.check_rate(@table, key, scale, limit)end++ test "denies any concurrent iterations over the limit" do+ key = key()+ scale = :timer.seconds(10)+ limit = 3++ assert eventually(fn ->+ results =+ 1..4+ |> Enum.map(fn _ ->+ Task.async(fn -> RateLimit.check_rate(@table, key, scale, limit) end)+ end)+ |> Task.await_many()+ |> Enum.sort()++ {[{:allow, 1}, {:allow, 2}, {:allow, 3}, {:deny, 3}] == results, results}+ end)+ endendenddiff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exsindex 50a1827183..49b1b61a6b 100644--- a/test/plausible_web/controllers/api/external_sites_controller_test.exs+++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs@@ -211,7 +211,9 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do})res = json_response(conn, 400)- assert res["error"] == "Parameter `site_id` is required to create a shared link"++ assert res["error"] ==+ "Missing site ID. Please provide the required site_id parameter with your request."endtest "returns 404 when site id is non existent", %{conn: conn} do@@ -398,7 +400,9 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do})res = json_response(conn, 400)- assert res["error"] == "Parameter `site_id` is required to create a custom property"++ assert res["error"] ==+ "Missing site ID. Please provide the required site_id parameter with your request."endtest "returns 404 when site id is non existent", %{conn: conn} do@@ -592,7 +596,9 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do})res = json_response(conn, 400)- assert res["error"] == "Parameter `site_id` is required to create a goal"++ assert res["error"] ==+ "Missing site ID. Please provide the required site_id parameter with your request."endtest "returns 404 when site id is non existent", %{conn: conn} do@@ -1425,7 +1431,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest doconn = get(conn, "/api/v1/sites/custom-props")assert json_response(conn, 400) == %{- "error" => "Parameter `site_id` is required to list custom properties"+ "error" =>+ "Missing site ID. Please provide the required site_id parameter with your request."}end@@ -1609,7 +1616,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest doconn = get(conn, "/api/v1/sites/goals")assert json_response(conn, 400) == %{- "error" => "Parameter `site_id` is required to list goals"+ "error" =>+ "Missing site ID. Please provide the required site_id parameter with your request."}enddiff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exsindex a2d4fc9202..d3f76c99ad 100644--- a/test/plausible_web/controllers/auth_controller_test.exs+++ b/test/plausible_web/controllers/auth_controller_test.exs@@ -1808,16 +1808,14 @@ defmodule PlausibleWeb.AuthControllerTest dopassword: password})|> recycle()- |> Map.put(:secret_key_base, secret_key_base())- |> Plug.Conn.put_req_header("x-forwarded-for", random_ip())+ |> prepare_conn()enddefp set_remember_2fa_cookie(conn, user) doconn|> PlausibleWeb.TwoFactor.Session.maybe_set_remember_2fa(user, "true")|> recycle()- |> Map.put(:secret_key_base, secret_key_base())- |> Plug.Conn.put_req_header("x-forwarded-for", random_ip())+ |> prepare_conn()enddefp mock_captcha_success() do@@ -1842,10 +1840,4 @@ defmodule PlausibleWeb.AuthControllerTest doend)end-- defp secret_key_base() do- :plausible- |> Application.fetch_env!(PlausibleWeb.Endpoint)- |> Keyword.fetch!(:secret_key_base)- endenddiff --git a/test/plausible_web/controllers/sso_controller_sync_test.exs b/test/plausible_web/controllers/sso_controller_sync_test.exsindex 631f6a745e..9b7711955a 100644--- a/test/plausible_web/controllers/sso_controller_sync_test.exs+++ b/test/plausible_web/controllers/sso_controller_sync_test.exs@@ -192,7 +192,7 @@ defmodule PlausibleWeb.SSOControllerSyncTest doconn =conn|> recycle()- |> Map.put(:secret_key_base, secret_key_base())+ |> prepare_conn(){:ok,team: team,@@ -473,11 +473,5 @@ defmodule PlausibleWeb.SSOControllerSyncTest do)endend-- defp secret_key_base() do- :plausible- |> Application.fetch_env!(PlausibleWeb.Endpoint)- |> Keyword.fetch!(:secret_key_base)- endendenddiff --git a/test/plausible_web/plugs/authorize_public_api_test.exs b/test/plausible_web/plugs/authorize_public_api_test.exsindex 43a34a300c..cbb622cdd1 100644--- a/test/plausible_web/plugs/authorize_public_api_test.exs+++ b/test/plausible_web/plugs/authorize_public_api_test.exs@@ -7,12 +7,7 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPITest doalias Plausible.Reposetup %{conn: conn} do- conn =- conn- |> put_private(PlausibleWeb.FirstLaunchPlug, :skip)- |> bypass_through(PlausibleWeb.Router)-- {:ok, conn: conn}+ {:ok, conn: prepare_conn_for_auth(conn)}endtest "halts with error when bearer token is missing", %{conn: conn} do@@ -50,25 +45,32 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPITest doassert json_response(conn, 401)["error"] =~ "Invalid API key."end- test "halts with error on missing site ID when request made to Stats API", %{conn: conn} do- api_key = insert(:api_key, user: build(:user))+ for query_string <- ["?foo=bar", "?site_id", "?site_id="] do+ test "halts with error when requesting site context API without site_id parameter (query string: #{query_string})",+ %{conn: conn} do+ # `site_id` param is checked for nil or empty+ # for `api_context: :site` APIs before the key is verified,+ # therefore the key here doesn't matter+ key = "123"- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/")- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)+ conn =+ conn+ |> put_req_header("authorization", "Bearer #{key}")+ |> get("/#{unquote(query_string)}")+ |> assign(:api_context, :site)+ |> assign(:api_scope, "sites:read:*")+ |> AuthorizePublicAPI.call(nil)- assert conn.halted- assert json_response(conn, 400)["error"] =~ "Missing site ID."+ assert conn.halted+ assert json_response(conn, 400)["error"] =~ "Missing site ID."+ endend@tag :ee_only- test "halts with error when site's team lacks feature access", %{conn: conn} do+ test "401 error has priority over 402 error", %{conn: conn} douser = new_user()_site = new_site(owner: user)- api_key = insert(:api_key, user: user)+ api_key = insert_api_key(:team_scope_api_key, user: user)another_owner =new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: "123321", features: [])@@ -77,315 +79,463 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPITest doconn =conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => another_site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)+ |> authorize(api_key, site: another_site, api_context: :site, api_scope: "stats:read:*")assert conn.halted- assert json_response(conn, 402)["error"] =~- "The account that owns this API key does not have access"+ assert json_response(conn, 401)["error"] =~+ "Invalid API key or site ID. Please make sure you're using a valid API key with access to the site you've requested."end- on_ee do- test "rejects access to a site that is a consolidated view (unless instructed otherwise)", %{+ for key_type <- [:legacy_api_key, :team_scope_api_key] do+ describe "#{key_type} ::" do+ test "halts with error on missing site ID when request made for stats:read:* scope API, even without :site API context set",+ %{conn: conn} do+ api_key = insert_api_key(unquote(key_type), user: new_user())++ conn =+ conn+ |> authorize(api_key, api_scope: "stats:read:*")++ assert conn.halted+ assert json_response(conn, 400)["error"] =~ "Missing site ID."+ end++ @tag :ee_only+ test "halts with error when site's team lacks feature access", %{conn: conn} do+ user = new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: "123321", features: [])+ site = new_site(owner: user)+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn+ |> authorize(api_key, api_scope: "stats:read:*", site: site)++ assert conn.halted++ assert json_response(conn, 402)["error"] =~+ "The account that owns this API key does not have access"+ end++ on_ee do+ test "rejects access to a site that is a consolidated view (unless instructed otherwise)",+ %{+ conn: conn+ } do+ user = new_user()+ api_key = insert_api_key(unquote(key_type), user: user, scopes: ["sites:provision:*"])+ {:ok, team} = new_user() |> Plausible.Teams.get_or_create()+ new_site(team: team)+ new_site(team: team)+ consolidated_view = new_consolidated_view(team)++ conn =+ conn+ |> authorize(api_key, site: consolidated_view, api_scope: "sites:provision:*")++ assert conn.halted++ assert json_response(conn, 400)["error"] =~+ "This operation is unavailable for a consolidated view"+ end+ end++ @tag :ee_only+ test "halts with error when upgrade is required", %{conn: conn} do+ user = new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: "123321", features: [])+ site = new_site(owner: user)+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn |> authorize(api_key, site: site, api_scope: "stats:read:*")++ assert conn.halted++ assert json_response(conn, 402)["error"] =~+ "The account that owns this API key does not have access"+ end++ test "halts with error when site is locked", %{conn: conn} do+ user = new_user()+ site = new_site(owner: user)+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn+ |> authorize(api_key, site: site, api_scope: "stats:read:*")++ assert conn.halted+ assert json_response(conn, 402)["error"] =~ "This Plausible site is locked"+ end++ test "halts with error when site ID is invalid", %{conn: conn} do+ user = new_user(trial_expiry_date: Date.utc_today())+ _site = new_site(owner: user)+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn+ |> authorize(api_key, site: %{domain: "invalid.domain"}, api_scope: "stats:read:*")++ assert conn.halted+ assert json_response(conn, 401)["error"] =~ "Invalid API key or site ID."+ end++ test "halts with error when API key owner does not have access to the requested site", %{+ conn: conn+ } do+ user = new_user(trial_expiry_date: Date.utc_today())+ site = new_site()+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn+ |> authorize(api_key, site: site, api_scope: "stats:read:*")++ assert conn.halted+ assert json_response(conn, 401)["error"] =~ "Invalid API key or site ID."+ end++ test "halts with error when API lacks required scope", %{conn: conn} do+ user = insert(:user)+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn |> authorize(api_key, api_scope: "sites:provision:*")++ assert conn.halted+ assert json_response(conn, 401)["error"] =~ "Invalid API key."+ end++ test "passes and sets current user when valid API key with required scope provided", %{+ conn: conn+ } do+ user = insert(:user)+ api_key = insert_api_key(unquote(key_type), user: user, scopes: ["sites:provision:*"])++ conn =+ conn+ |> authorize(api_key, api_scope: "sites:provision:*")++ refute conn.halted+ assert conn.assigns.current_user.id == user.id+ end++ test "passes and sets current user and site when valid API key and site ID provided", %{+ conn: conn+ } do+ user = new_user()+ site = new_site(owner: user)+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn+ |> authorize(api_key, site: site, api_scope: "stats:read:*")++ refute conn.halted+ assert conn.assigns.current_user.id == user.id+ assert conn.assigns.site.id == site.id+ end++ @tag :ee_only+ test "passes for super admin user even if not a member of the requested site", %{conn: conn} do+ user = new_user()+ patch_env(:super_admin_user_ids, [user.id])+ site = new_site()+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()+ api_key = insert_api_key(unquote(key_type), user: user)++ conn =+ conn |> authorize(api_key, site: site, api_scope: "stats:read:*")++ refute conn.halted+ assert conn.assigns.current_user.id == user.id+ assert conn.assigns.site.id == site.id+ end++ test "passes for subscope match", %{conn: conn} do+ user = insert(:user)+ api_key = insert_api_key(unquote(key_type), user: user, scopes: ["funnels:*"])++ conn =+ conn+ |> authorize(api_key, api_scope: "funnels:read:*")++ refute conn.halted+ assert conn.assigns.current_user.id == user.id+ end+ end+ end++ describe "no context API ::" do+ test "legacy API key request passes validation", %{conn: conn} do- user = new_user()- api_key = insert(:api_key, user: user, scopes: ["sites:provision:*"])- {:ok, team} = new_user() |> Plausible.Teams.get_or_create()- new_site(team: team)- new_site(team: team)- consolidated_view = new_consolidated_view(team)+ legacy_api_key_user = new_user()+ legacy_api_key = insert_api_key(:legacy_api_key, user: legacy_api_key_user)conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => consolidated_view.domain})- |> assign(:api_scope, "sites:provision:*")- |> AuthorizePublicAPI.call(nil)-- assert conn.halted+ conn |> authorize(legacy_api_key, api_scope: "sites:read:*")- assert json_response(conn, 400)["error"] =~- "This operation is unavailable for a consolidated view"+ refute conn.halted+ assert conn.assigns.current_user.id == legacy_api_key_user.idend- end-- @tag :ee_only- test "halts with error when upgrade is required", %{conn: conn} do- user = new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: "123321", features: [])- site = new_site(owner: user)- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)-- assert conn.halted- assert json_response(conn, 402)["error"] =~- "The account that owns this API key does not have access"- end-- test "halts with error when site is locked", %{conn: conn} do- user = new_user()- site = new_site(owner: user)- site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)-- assert conn.halted- assert json_response(conn, 402)["error"] =~ "This Plausible site is locked"- end-- test "halts with error when site ID is invalid", %{conn: conn} do- user = new_user(trial_expiry_date: Date.utc_today())- _site = new_site(owner: user)- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => "invalid.domain"})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)+ @legacy_hourly_request_limit Plausible.Auth.ApiKey.legacy_hourly_request_limit()+ test "legacy API key requests are rate limited to configured #{@legacy_hourly_request_limit} requests per hour _per user_, but their team's team-scoped keys are on an independent rate limit" do+ legacy_api_key_user = new_user()+ _site = new_site(owner: legacy_api_key_user)+ legacy_api_key = insert_api_key(:legacy_api_key, user: legacy_api_key_user)- assert conn.halted- assert json_response(conn, 401)["error"] =~ "Invalid API key or site ID."- end-- test "halts with error when API key owner does not have access to the requested site", %{- conn: conn- } do- user = new_user(trial_expiry_date: Date.utc_today())- site = new_site()- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)-- assert conn.halted- assert json_response(conn, 401)["error"] =~ "Invalid API key or site ID."- end-- test "halts with error when API lacks required scope", %{conn: conn} do- user = insert(:user)- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/")- |> assign(:api_scope, "sites:provision:*")- |> AuthorizePublicAPI.call(nil)+ 1..@legacy_hourly_request_limit+ |> Enum.map(fn _ ->+ conn =+ get_fresh_conn()+ |> authorize(legacy_api_key, api_scope: "sites:read:*")- assert conn.halted- assert json_response(conn, 401)["error"] =~ "Invalid API key."- end+ refute conn.halted+ assert conn.assigns.current_user.id == legacy_api_key_user.id+ end)- test "halts with error when API rate limit hit", %{conn: conn} do- user = new_user(team: [hourly_api_request_limit: 1])- site = new_site(owner: user)+ # the hardcoded limit applies per user, not per api key+ [legacy_api_key, insert_api_key(:legacy_api_key, user: legacy_api_key_user)]+ |> Enum.map(fn api_key ->+ conn =+ get_fresh_conn()+ |> authorize(api_key, api_scope: "sites:read:*")- api_key = insert(:api_key, user: user)+ assert conn.halted+ assert json_response(conn, 429)["error"] =~ "Too many API requests."+ end)- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/?site_id=#{site.domain}")- |> assign(:api_scope, "sites:read:*")- |> assign(:api_context, :site)+ # no context API requests made with legacy API keys don't count towards team limits+ team_scope_api_key_user = legacy_api_key_user |> team_of() |> add_member(role: :editor)- first_resp = AuthorizePublicAPI.call(conn, nil)- second_resp = AuthorizePublicAPI.call(conn, nil)+ conn =+ get_fresh_conn()+ |> authorize(+ insert_api_key(:team_scope_api_key,+ user: team_scope_api_key_user+ ),+ api_scope: "sites:read:*"+ )- refute first_resp.halted- assert second_resp.halted- assert json_response(second_resp, 429)["error"] =~ "Too many API requests."+ refute conn.halted+ endend- test "does not rate limit when not in applicable context", %{conn: conn} do- user = new_user(team: [hourly_api_request_limit: 1])- site = new_site(owner: user)+ describe("site context API ::") do+ @tag :capture_log+ test "legacy API key requests pass validation for sites of all their teams",+ %{+ conn: conn+ } do+ legacy_api_key_user = new_user()+ legacy_api_key = insert_api_key(:legacy_api_key, user: legacy_api_key_user)+ site_from_first_team = new_site(owner: legacy_api_key_user)+ site_from_second_team = new_site()+ add_member(site_from_second_team.team, user: legacy_api_key_user, role: :editor)++ [site_from_first_team, site_from_second_team]+ |> Enum.map(fn site ->+ conn =+ conn+ |> authorize(legacy_api_key,+ api_context: :site,+ api_scope: "sites:read:*",+ site: site+ )++ refute conn.halted+ assert conn.assigns.current_user.id == legacy_api_key_user.id+ end)+ end- api_key = insert(:api_key, user: user)+ for guest_role <- [:viewer, :editor] do+ @tag :capture_log+ test "legacy API key requests pass validation for sites where they are a guest #{guest_role} at",+ %{+ conn: conn+ } do+ legacy_api_key_user = new_user()+ legacy_api_key = insert_api_key(:legacy_api_key, user: legacy_api_key_user)+ site = new_site()+ add_guest(site, user: legacy_api_key_user, role: unquote(guest_role))++ conn =+ conn+ |> authorize(legacy_api_key,+ api_context: :site,+ api_scope: "sites:read:*",+ site: site+ )++ refute conn.halted+ assert conn.assigns.current_user.id == legacy_api_key_user.id+ end+ end- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/?site_id=#{site.domain}")- |> assign(:api_scope, "sites:read:*")+ for {key_type, expected_halted} <- [{:legacy_api_key, false}, {:team_scope_api_key, true}] do+ test "logs a warning on using #{key_type} against site with guest access", %{conn: conn} do+ user = new_user(team: [hourly_api_request_limit: 1])+ site = new_site()+ add_guest(site, user: user, role: :editor)- first_resp = AuthorizePublicAPI.call(conn, nil)- second_resp = AuthorizePublicAPI.call(conn, nil)+ api_key = insert_api_key(unquote(key_type), user: user)- refute first_resp.halted- refute second_resp.halted- end+ capture_log(fn ->+ conn =+ conn |> authorize(api_key, site: site, api_context: :site, api_scope: "sites:read:*")- test "logs a warning on using API key against site with guest access", %{conn: conn} do- user = new_user(team: [hourly_api_request_limit: 1])- site = new_site()- add_guest(site, user: user, role: :editor)+ assert conn.halted == unquote(expected_halted)+ end) =~ "API key #{api_key.id} user accessing #{site.domain} as a guest"+ end+ end- api_key = insert(:api_key, user: user)+ for {key_type, expected_halted} <- [{:legacy_api_key, true}, {:team_scope_api_key, true}] do+ test "logs a warning on using #{key_type} against site with no access", %{conn: conn} do+ user = new_user(team: [hourly_api_request_limit: 1])+ site = new_site()- capture_log(fn ->- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/?site_id=#{site.domain}")- |> assign(:api_scope, "sites:read:*")- |> assign(:api_context, :site)- |> AuthorizePublicAPI.call(nil)+ api_key = insert_api_key(unquote(key_type), user: user)- refute conn.halted- end) =~ "API key #{api_key.id} user accessing #{site.domain} as a guest"- end+ capture_log(fn ->+ conn =+ conn |> authorize(api_key, site: site, api_context: :site, api_scope: "stats:read:*")- test "logs a warning on using API key against site with no access", %{conn: conn} do- user = new_user(team: [hourly_api_request_limit: 1])- site = new_site()+ assert conn.halted == unquote(expected_halted)+ end) =~ "API key #{api_key.id} user trying to access #{site.domain} as a non-member"+ end+ end- api_key = insert(:api_key, user: user)+ @tag :capture_log+ test "legacy API key request doesn't pass validation for sites where they are not a guest at",+ %{+ conn: conn+ } do+ legacy_api_key_user = new_user()+ legacy_api_key = insert_api_key(:legacy_api_key, user: legacy_api_key_user)+ site_with_no_guests = new_site()- capture_log(fn ->conn =conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/?site_id=#{site.domain}")- |> assign(:api_scope, "stats:read:*")- |> assign(:api_context, :site)- |> AuthorizePublicAPI.call(nil)+ |> authorize(legacy_api_key,+ api_context: :site,+ api_scope: "sites:read:*",+ site: site_with_no_guests+ )assert conn.halted- end) =~ "API key #{api_key.id} user trying to access #{site.domain} as a non-member"- end-- test "passes and sets current user when valid API key with required scope provided", %{- conn: conn- } do- user = insert(:user)- api_key = insert(:api_key, user: user, scopes: ["sites:provision:*"])+ assert json_response(conn, 401)["error"] =~ "Invalid API key."+ end- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/")- |> assign(:api_scope, "sites:provision:*")- |> AuthorizePublicAPI.call(nil)+ for guest_role <- [:viewer, :editor] do+ test "team-scope API key request doesn't pass validation for sites where they are a guest #{guest_role} at",+ %{+ conn: conn+ } do+ user = new_user()+ _site = new_site(owner: user)+ site = new_site()+ add_guest(site, user: user, role: unquote(guest_role))+ team_scope_api_key = insert_api_key(:team_scope_api_key, user: user)++ conn =+ conn+ |> authorize(+ team_scope_api_key,+ api_context: :site,+ api_scope: "sites:read:*",+ site: site+ )++ assert conn.halted+ assert json_response(conn, 401)["error"] =~ "Invalid API key."+ end+ end- refute conn.halted- assert conn.assigns.current_user.id == user.id+ @tag :capture_log+ test "legacy API key requests count towards the rate limit of the team of the site" do+ user = new_user()+ legacy_api_key = insert_api_key(:legacy_api_key, user: user)++ team_hourly_request_limit = 5+ user_of_other_team = new_user(team: [hourly_api_request_limit: team_hourly_request_limit])++ team_scope_api_key_of_other_team =+ insert_api_key(:team_scope_api_key, user: user_of_other_team)++ site = new_site(owner: user_of_other_team)++ add_guest(site, user: user, role: :viewer)++ 1..team_hourly_request_limit+ |> Enum.map(fn _ ->+ conn =+ get_fresh_conn()+ |> authorize(legacy_api_key,+ api_context: :site,+ api_scope: "sites:read:*",+ site: site+ )++ refute conn.halted+ assert conn.assigns.current_user.id == user.id+ end)++ [+ legacy_api_key,+ team_scope_api_key_of_other_team+ ]+ |> Enum.map(fn api_key ->+ conn =+ get_fresh_conn()+ |> authorize(api_key,+ api_context: :site,+ api_scope: "sites:read:*",+ site: site+ )++ assert json_response(conn, 429)["error"] =~ "Too many API requests."+ assert conn.halted+ end)+ endend- test "passes and sets current user and site when valid API key and site ID provided", %{- conn: conn- } do- user = new_user()- site = new_site(owner: user)- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)-- refute conn.halted- assert conn.assigns.current_user.id == user.id- assert conn.assigns.site.id == site.id+ defp prepare_conn_for_auth(conn) do+ conn+ |> put_private(PlausibleWeb.FirstLaunchPlug, :skip)+ |> bypass_through(PlausibleWeb.Router)end- test "passes for team-bound API key when team matches", %{conn: conn} do- user = new_user()- _site = new_site(owner: user)- another_site = new_site()- another_team = another_site.team |> Plausible.Teams.complete_setup()- add_member(another_team, user: user, role: :editor)- api_key = insert(:api_key, user: user, team: another_team)+ defp authorize(conn, api_key, opts) do+ context = opts |> Keyword.get(:api_context)+ scope = opts |> Keyword.fetch!(:api_scope)+ site = opts |> Keyword.get(:site)conn =conn|> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => another_site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)+ |> get("/", if(site, do: %{"site_id" => site.domain}, else: %{}))+ |> assign(:api_scope, scope)- refute conn.halted- assert conn.assigns.current_team.id == another_team.id- assert conn.assigns.current_user.id == user.id- assert conn.assigns.site.id == another_site.id+ conn = if context, do: assign(conn, :api_context, context), else: conn+ AuthorizePublicAPI.call(conn, nil)end- test "halts for team-bound API key when team does not match", %{conn: conn} do- user = new_user()- site = new_site(owner: user)- another_site = new_site()- another_team = another_site.team |> Plausible.Teams.complete_setup()- add_member(another_team, user: user, role: :editor)- api_key = insert(:api_key, user: user, team: another_team)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)-- assert conn.halted-- assert json_response(conn, 401)["error"] =~ "Invalid API key or site ID."+ defp insert_api_key(:legacy_api_key, opts) do+ insert(:api_key, opts |> Keyword.drop([:team, :team_id]))end- @tag :ee_only- test "passes for super admin user even if not a member of the requested site", %{conn: conn} do- user = new_user()- patch_env(:super_admin_user_ids, [user.id])- site = new_site()- site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()- api_key = insert(:api_key, user: user)-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/", %{"site_id" => site.domain})- |> assign(:api_scope, "stats:read:*")- |> AuthorizePublicAPI.call(nil)-- refute conn.halted- assert conn.assigns.current_user.id == user.id- assert conn.assigns.site.id == site.id+ defp insert_api_key(:team_scope_api_key, opts) do+ team = opts |> Keyword.fetch!(:user) |> team_of()+ insert(:api_key, opts |> Keyword.put(:team, team))end- test "passes for subscope match", %{conn: conn} do- user = insert(:user)- api_key = insert(:api_key, user: user, scopes: ["funnels:*"])-- conn =- conn- |> put_req_header("authorization", "Bearer #{api_key.key}")- |> get("/")- |> assign(:api_scope, "funnels:read:*")- |> AuthorizePublicAPI.call(nil)-- refute conn.halted- assert conn.assigns.current_user.id == user.id- end+ defp get_fresh_conn(),+ do:+ build_conn()+ |> prepare_conn()+ |> prepare_conn_for_auth()enddiff --git a/test/support/conn_case.ex b/test/support/conn_case.exindex 99432208bf..13344a8bcf 100644--- a/test/support/conn_case.ex+++ b/test/support/conn_case.ex@@ -42,19 +42,7 @@ defmodule PlausibleWeb.ConnCase doPlausible.Test.Support.Sandbox.allow_salts_process()- # randomize client ip to avoid accidentally hitting- # rate limiting during tests- conn =- Phoenix.ConnTest.build_conn()- |> Map.put(:secret_key_base, secret_key_base())- |> Plug.Conn.put_req_header("x-forwarded-for", Plausible.TestUtils.random_ip())-+ conn = Phoenix.ConnTest.build_conn() |> Plausible.TestUtils.prepare_conn(){:ok, conn: conn}end-- defp secret_key_base() do- :plausible- |> Application.fetch_env!(PlausibleWeb.Endpoint)- |> Keyword.fetch!(:secret_key_base)- endenddiff --git a/test/support/test_utils.ex b/test/support/test_utils.exindex bd47111161..c8dd107555 100644--- a/test/support/test_utils.ex+++ b/test/support/test_utils.ex@@ -374,4 +374,10 @@ defmodule Plausible.TestUtils do}endend++ def prepare_conn(conn) do+ conn+ |> Map.put(:secret_key_base, secret_key_base())+ |> Plug.Conn.put_req_header("x-forwarded-for", random_ip())+ endend