Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

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.ex
index 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 do
conn
|> 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."
+ })
end
end
diff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exs
index 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} do
api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])