From b0b9b9212bb7900e6d4060ae3dc85167f99c9543 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 22 Feb 2022 13:46:57 -0600 Subject: [PATCH] Fixes password authentication issue with shared links --- .../controllers/stats_controller.ex | 11 +++++++---- lib/plausible_web/router.ex | 2 +- .../controllers/stats_controller_test.exs | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index f0d2b3573..8c87c612a 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}) do shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: auth) |> Repo.preload(:site) @@ -109,8 +109,9 @@ defmodule PlausibleWeb.StatsController do if shared_link && shared_link.site.domain == domain do if shared_link.password_hash do with 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 do render_shared_link(conn, shared_link) else _e -> @@ -149,7 +150,7 @@ defmodule PlausibleWeb.StatsController do token = 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}") else conn @@ -201,4 +202,6 @@ defmodule PlausibleWeb.StatsController do conn end end + + defp shared_link_cookie_name(slug), do: "shared-link-" <> slug end diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index c295f0083..64080a668 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -128,7 +128,7 @@ defmodule PlausibleWeb.Router do scope "/", PlausibleWeb do pipe_through [:shared_link] - get "/share/:slug", StatsController, :shared_link + get "/share/:domain", StatsController, :shared_link post "/share/:slug/authenticate", StatsController, :authenticate_shared_link end diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs index 4cbb2a695..5ef6b7b5b 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 do conn = 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" + end end end