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
Fixes password authentication issue with shared links
Uku Tahtb0b9b9212bb7
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.exindex f0d2b35730..8c87c612a0 100644--- a/lib/plausible_web/controllers/stats_controller.ex+++ b/lib/plausible_web/controllers/stats_controller.ex@@ -101,7 +101,7 @@ defmodule PlausibleWeb.StatsController do|> send_resp(200, zip_content)end- def shared_link(conn, %{"slug" => domain, "auth" => auth}) do+ def shared_link(conn, %{"domain" => domain, "auth" => auth}) doshared_link =Repo.get_by(Plausible.Site.SharedLink, slug: auth)|> Repo.preload(:site)@@ -109,8 +109,9 @@ defmodule PlausibleWeb.StatsController doif shared_link && shared_link.site.domain == domain doif shared_link.password_hash dowith conn <- Plug.Conn.fetch_cookies(conn),- {:ok, token} <- Map.fetch(conn.req_cookies, "shared-link-token"),- {:ok, _} <- Plausible.Auth.Token.verify_shared_link(token) do+ {:ok, token} <- Map.fetch(conn.req_cookies, shared_link_cookie_name(auth)),+ {:ok, %{slug: token_slug}} <- Plausible.Auth.Token.verify_shared_link(token),+ true <- token_slug == shared_link.slug dorender_shared_link(conn, shared_link)else_e ->@@ -149,7 +150,7 @@ defmodule PlausibleWeb.StatsController dotoken = Plausible.Auth.Token.sign_shared_link(slug)conn- |> put_resp_cookie("shared-link-token", token)+ |> put_resp_cookie(shared_link_cookie_name(slug), token)|> redirect(to: "/share/#{URI.encode_www_form(shared_link.site.domain)}?auth=#{slug}")elseconn@@ -201,4 +202,6 @@ defmodule PlausibleWeb.StatsController doconnendend++ defp shared_link_cookie_name(slug), do: "shared-link-" <> slugenddiff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex c295f00835..64080a6688 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -128,7 +128,7 @@ defmodule PlausibleWeb.Router doscope "/", PlausibleWeb dopipe_through [:shared_link]- get "/share/:slug", StatsController, :shared_link+ get "/share/:domain", StatsController, :shared_linkpost "/share/:slug/authenticate", StatsController, :authenticate_shared_linkenddiff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exsindex 4cbb2a6959..5ef6b7b5b6 100644--- a/test/plausible_web/controllers/stats_controller_test.exs+++ b/test/plausible_web/controllers/stats_controller_test.exs@@ -237,5 +237,22 @@ defmodule PlausibleWeb.StatsControllerTest doconn = post(conn, "/share/#{link.slug}/authenticate", %{password: "WRONG!"})assert html_response(conn, 200) =~ "Enter password"end++ test "only gives access to the correct dashboard", %{conn: conn} do+ site = insert(:site, domain: "test-site.com")+ site2 = insert(:site, domain: "test-site2.com")++ link =+ insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))++ link2 =+ insert(:shared_link, site: site2, password_hash: Plausible.Auth.Password.hash("password1"))++ conn = post(conn, "/share/#{link.slug}/authenticate", %{password: "password"})+ assert redirected_to(conn, 302) == "/share/#{site.domain}?auth=#{link.slug}"++ conn = get(conn, "/share/#{site2.domain}?auth=#{link2.slug}")+ assert html_response(conn, 200) =~ "Enter password"+ endendend