Block Stats API for locked sites (#2302)

* Extract Sites.locked? predicate

* Lock Stats API when dashboard is locked

* Tidy tests

* Don't pollute application env from tests

* Add changelog entry

* Revert "Add changelog entry"

This reverts commit 76346074f9.
pull/2305/head
Adam Rutkowski 4 years ago committed by GitHub
parent 24ed29fdee
commit bf2b6880c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
defmodule Plausible.Sites do
use Plausible.Repo
alias Plausible.Site
alias Plausible.Site.SharedLink
import Ecto.Query
@ -9,13 +10,13 @@ defmodule Plausible.Sites do
if owned_sites_count(user) >= limit do
{:error, :limit, limit}
else
site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params)
site_changeset = Site.changeset(%Site{}, params)
Ecto.Multi.new()
|> Ecto.Multi.insert(:site, site_changeset)
|> Ecto.Multi.run(:site_membership, fn repo, %{site: site} ->
membership_changeset =
Plausible.Site.Membership.changeset(%Plausible.Site.Membership{}, %{
Site.Membership.changeset(%Site.Membership{}, %{
site_id: site.id,
user_id: user.id
})
@ -45,7 +46,7 @@ defmodule Plausible.Sites do
start_date = Plausible.Stats.Clickhouse.pageview_start_date_local(site)
if start_date do
Plausible.Site.set_stats_start_date(site, start_date)
Site.set_stats_start_date(site, start_date)
|> Repo.update()
start_date
@ -83,8 +84,8 @@ defmodule Plausible.Sites do
do: Repo.one(get_for_user_q(user_id, domain, roles))
defp get_for_user_q(user_id, domain, roles) do
from(s in Plausible.Site,
join: sm in Plausible.Site.Membership,
from(s in Site,
join: sm in Site.Membership,
on: sm.site_id == s.id,
where: sm.user_id == ^user_id,
where: sm.role in ^roles,
@ -108,9 +109,13 @@ defmodule Plausible.Sites do
role(user_id, site) in [:admin, :owner]
end
def locked?(%Site{locked: locked}) do
locked
end
def role(user_id, site) do
Repo.one(
from sm in Plausible.Site.Membership,
from sm in Site.Membership,
where: sm.user_id == ^user_id and sm.site_id == ^site.id,
select: sm.role
)
@ -130,8 +135,8 @@ defmodule Plausible.Sites do
end
defp owned_sites_query(user) do
from(s in Plausible.Site,
join: sm in Plausible.Site.Membership,
from(s in Site,
join: sm in Site.Membership,
on: sm.site_id == s.id,
where: sm.role == :owner,
where: sm.user_id == ^user.id
@ -141,7 +146,7 @@ defmodule Plausible.Sites do
def owner_for(site) do
Repo.one(
from u in Plausible.Auth.User,
join: sm in Plausible.Site.Membership,
join: sm in Site.Membership,
on: sm.user_id == u.id,
where: sm.site_id == ^site.id,
where: sm.role == :owner

@ -28,4 +28,11 @@ defmodule PlausibleWeb.Api.Helpers do
|> Phoenix.Controller.json(%{error: msg})
|> halt()
end
def payment_required(conn, msg) do
conn
|> put_status(402)
|> Phoenix.Controller.json(%{error: msg})
|> halt()
end
end

@ -41,17 +41,19 @@ defmodule PlausibleWeb.StatsController do
use PlausibleWeb, :controller
use Plausible.Repo
alias PlausibleWeb.Api
alias Plausible.Sites
alias Plausible.Stats.{Query, Filters}
alias PlausibleWeb.Api
plug PlausibleWeb.AuthorizeSiteAccess when action in [:stats, :csv_export]
def stats(%{assigns: %{site: site}} = conn, _params) do
stats_start_date = Plausible.Sites.stats_start_date(site)
can_see_stats = !site.locked || conn.assigns[:current_user_role] == :super_admin
can_see_stats? = not Sites.locked?(site) or conn.assigns[:current_user_role] == :super_admin
cond do
stats_start_date && can_see_stats ->
stats_start_date && can_see_stats? ->
demo = site.domain == PlausibleWeb.Endpoint.host()
offer_email_report = get_session(conn, site.domain <> "_offer_email_report")
@ -70,13 +72,13 @@ defmodule PlausibleWeb.StatsController do
is_dbip: is_dbip()
)
!stats_start_date && can_see_stats ->
!stats_start_date && can_see_stats? ->
conn
|> assign(:skip_plausible_tracking, true)
|> render("waiting_first_pageview.html", site: site)
site.locked ->
owner = Plausible.Sites.owner_for(site)
Sites.locked?(site) ->
owner = Sites.owner_for(site)
conn
|> assign(:skip_plausible_tracking, true)
@ -269,7 +271,7 @@ defmodule PlausibleWeb.StatsController do
|> delete_resp_header("x-frame-options")
|> render("stats.html",
site: shared_link.site,
has_goals: Plausible.Sites.has_goals?(shared_link.site),
has_goals: Sites.has_goals?(shared_link.site),
stats_start_date: shared_link.site.stats_start_date,
title: "Plausible · " <> shared_link.site.domain,
offer_email_report: false,
@ -283,8 +285,8 @@ defmodule PlausibleWeb.StatsController do
is_dbip: is_dbip()
)
shared_link.site.locked ->
owner = Plausible.Sites.owner_for(shared_link.site)
Sites.locked?(shared_link.site) ->
owner = Sites.owner_for(shared_link.site)
conn
|> assign(:skip_plausible_tracking, true)

@ -2,6 +2,7 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
import Plug.Conn
use Plausible.Repo
alias Plausible.Auth.ApiKey
alias Plausible.Sites
alias PlausibleWeb.Api.Helpers, as: H
def init(options) do
@ -38,20 +39,32 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
conn,
"Invalid API key or site ID. Please make sure you're using a valid API key with access to the site you've requested."
)
{:error, :site_locked} ->
H.payment_required(
conn,
"This Plausible site is locked due to missing active subscription. In order to access it, the site owner should subscribe to a suitable plan"
)
end
end
defp verify_access(_api_key, nil), do: {:error, :missing_site_id}
defp verify_access(api_key, site_id) do
site = Repo.get_by(Plausible.Site, domain: site_id)
is_member = site && Plausible.Sites.is_member?(api_key.user_id, site)
is_super_admin = Plausible.Auth.is_super_admin?(api_key.user_id)
case Repo.get_by(Plausible.Site, domain: site_id) do
%Plausible.Site{} = site ->
is_member? = Sites.is_member?(api_key.user_id, site)
is_super_admin? = Plausible.Auth.is_super_admin?(api_key.user_id)
cond do
Sites.locked?(site) -> {:error, :site_locked}
is_member? -> {:ok, site}
is_super_admin? -> {:ok, site}
true -> {:error, :invalid_api_key}
end
cond do
site && is_member -> {:ok, site}
site && is_super_admin -> {:ok, site}
true -> {:error, :invalid_api_key}
nil ->
{:error, :invalid_api_key}
end
end

@ -5,102 +5,123 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do
setup [:create_user, :create_api_key]
test "unauthenticated request - returns 401", %{conn: conn} do
conn =
get(conn, "/api/v1/stats/aggregate", %{
"site_id" => "some-site.com",
"metrics" => "pageviews"
})
assert json_response(conn, 401) == %{
"error" => "Missing API key. Please use a valid Plausible API key as a Bearer Token."
}
conn
|> get("/api/v1/stats/aggregate", %{
"site_id" => "some-site.com",
"metrics" => "pageviews"
})
|> assert_error(
401,
"Missing API key. Please use a valid Plausible API key as a Bearer Token."
)
end
test "bad API key - returns 401", %{conn: conn} do
conn =
conn
|> Plug.Conn.put_req_header("authorization", "Bearer bad-key")
|> get("/api/v1/stats/aggregate", %{"site_id" => "some-site.com", "metrics" => "pageviews"})
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."
}
conn
|> with_api_key("Bad key")
|> get("/api/v1/stats/aggregate", %{"site_id" => "some-site.com", "metrics" => "pageviews"})
|> assert_error(
401,
"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
test "good API key but bad site id - returns 401", %{conn: conn, api_key: api_key} do
conn =
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key}")
|> get("/api/v1/stats/aggregate", %{"site_id" => "some-site.com", "metrics" => "pageviews"})
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."
}
conn
|> with_api_key(api_key)
|> get("/api/v1/stats/aggregate", %{"site_id" => "some-site.com", "metrics" => "pageviews"})
|> assert_error(
401,
"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
test "good API key but missing site id - returns 400", %{conn: conn, api_key: api_key} do
conn =
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key}")
|> get("/api/v1/stats/aggregate", %{"metrics" => "pageviews"})
conn
|> with_api_key(api_key)
|> get("/api/v1/stats/aggregate", %{"metrics" => "pageviews"})
|> assert_error(
400,
"Missing site ID. Please provide the required site_id parameter with your request."
)
end
test "locked site - returns 402", %{conn: conn, api_key: api_key, user: user} do
site = insert(:site, members: [user])
{1, _} = Plausible.Billing.SiteLocker.set_lock_status_for(user, true)
assert json_response(conn, 400) == %{
"error" =>
"Missing site ID. Please provide the required site_id parameter with your request."
}
conn
|> with_api_key(api_key)
|> get("/api/v1/stats/aggregate", %{"site_id" => site.domain, "metrics" => "pageviews"})
|> assert_error(402, "missing active subscription")
end
test "can access with correct API key and site ID", %{conn: conn, user: user, api_key: api_key} do
site = insert(:site, members: [user])
conn =
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key}")
|> get("/api/v1/stats/aggregate", %{"site_id" => site.domain, "metrics" => "pageviews"})
assert json_response(conn, 200) == %{
"results" => %{"pageviews" => %{"value" => 0}}
}
conn
|> with_api_key(api_key)
|> get("/api/v1/stats/aggregate", %{"site_id" => site.domain, "metrics" => "pageviews"})
|> assert_ok(%{
"results" => %{"pageviews" => %{"value" => 0}}
})
end
test "can access as an admin", %{conn: conn, user: user, api_key: api_key} do
Application.put_env(:plausible, :super_admin_user_ids, [user.id])
site = insert(:site)
describe "admin access" do
setup %{user: user} do
original_env = Application.get_env(:plausible, :super_admin_user_ids)
Application.put_env(:plausible, :super_admin_user_ids, [user.id])
on_exit(fn ->
Application.put_env(:plausible, :super_admin_user_ids, original_env)
end)
end
test "can access as an admin", %{conn: conn, api_key: api_key} do
site = insert(:site)
conn =
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key}")
|> with_api_key(api_key)
|> get("/api/v1/stats/aggregate", %{"site_id" => site.domain, "metrics" => "pageviews"})
assert json_response(conn, 200) == %{
"results" => %{"pageviews" => %{"value" => 0}}
}
|> assert_ok(%{
"results" => %{"pageviews" => %{"value" => 0}}
})
end
end
test "limits the rate of API requests", %{user: user} do
api_key = insert(:api_key, user_id: user.id, hourly_request_limit: 3)
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> with_api_key(api_key.key)
|> get("/api/v1/stats/aggregate")
build_conn()
|> with_api_key(api_key.key)
|> get("/api/v1/stats/aggregate")
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> with_api_key(api_key.key)
|> get("/api/v1/stats/aggregate")
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> with_api_key(api_key.key)
|> get("/api/v1/stats/aggregate")
|> assert_error(
429,
"Too many API requests. Your API key is limited to 3 requests per hour."
)
end
conn =
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> get("/api/v1/stats/aggregate")
defp with_api_key(conn, api_key) do
Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key}")
end
defp assert_error(conn, status, message) do
assert json_response(conn, status)["error"] =~ message
end
assert json_response(conn, 429) == %{
"error" => "Too many API requests. Your API key is limited to 3 requests per hour."
}
defp assert_ok(conn, payload) do
assert json_response(conn, 200) == payload
end
end

Loading…
Cancel
Save