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
Protect the API endpoint from creating too many sites
Uku Taht7f9057818da1
diff --git a/lib/plausible_web/controllers/api/external_sites_controller.ex b/lib/plausible_web/controllers/api/external_sites_controller.exindex 0ed2a6a236..444477707f 100644--- a/lib/plausible_web/controllers/api/external_sites_controller.ex+++ b/lib/plausible_web/controllers/api/external_sites_controller.ex@@ -16,6 +16,14 @@ defmodule PlausibleWeb.Api.ExternalSitesController doconn|> put_status(400)|> json(serialize_errors(changeset))++ {:error, :limit, limit} ->+ conn+ |> put_status(403)+ |> json(%{+ error:+ "Your account has reached the limit of #{limit} sites per account. Please contact [email protected] to unlock more sites."+ })endenddiff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exsindex c67ca5e816..9dc48d5c9d 100644--- a/test/plausible_web/controllers/api/external_sites_controller_test.exs+++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs@@ -43,6 +43,24 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do}end+ test "does not allow creating more sites than the limit", %{conn: conn, user: user} do+ Application.put_env(:plausible, :site_limit, 3)+ insert(:site, members: [user])+ insert(:site, members: [user])+ insert(:site, members: [user])++ conn =+ post(conn, "/api/v1/sites", %{+ "domain" => "some-site.domain",+ "timezone" => "Europe/Tallinn"+ })++ assert json_response(conn, 403) == %{+ "error" =>+ "Your account has reached the limit of 3 sites per account. Please contact [email protected] to unlock more sites."+ }+ end+test "cannot access with a bad API key scope", %{conn: conn, user: user} doapi_key = insert(:api_key, user: user, scopes: ["stats:read:*"])