Skip to content

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

Preserve query params for authenticated shared links (#5640)

Adam Rutkowskie3bef74cde29
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 91cde9f1ca..9c805d3da9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ All notable changes to this project will be documented in this file.
- Make clicking Compare / Disable Comparison in period picker menu close the menu
- Do not log page views for hidden pages (prerendered pages and new tabs), until pages are viewed
+- Password-authenticated shared links now carry over dashboard params properly
## v3.0.0 - 2025-04-11
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 6688125649..bde27f820f 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -287,6 +287,7 @@ defmodule PlausibleWeb.StatsController do
conn
|> render("shared_link_password.html",
link: shared_link,
+ query_string: conn.query_string,
dogfood_page_path: "/share/:dashboard"
)
end
@@ -326,12 +327,17 @@ defmodule PlausibleWeb.StatsController do
conn
|> put_resp_cookie(shared_link_cookie_name(slug), token)
- |> redirect(to: "/share/#{URI.encode_www_form(shared_link.site.domain)}?auth=#{slug}")
+ |> redirect(
+ to:
+ Routes.stats_path(conn, :shared_link, shared_link.site.domain, auth: slug) <>
+ qs_appendix(conn)
+ )
else
conn
|> render("shared_link_password.html",
link: shared_link,
error: "Incorrect password. Please try again.",
+ query_string: conn.query_string,
dogfood_page_path: "/share/:dashboard"
)
end
@@ -340,6 +346,13 @@ defmodule PlausibleWeb.StatsController do
end
end
+ def qs_appendix(conn)
+ when is_nil(conn.query_string) or
+ (is_binary(conn.query_string) and byte_size(conn.query_string)) == 0,
+ do: ""
+
+ def qs_appendix(conn), do: "&#{conn.query_string}"
+
defp render_shared_link(conn, shared_link) do
shared_links_feature_access? =
SharedLinks.check_availability(shared_link.site.team) == :ok or
diff --git a/lib/plausible_web/templates/stats/shared_link_password.html.heex b/lib/plausible_web/templates/stats/shared_link_password.html.heex
index a389994d45..a2ed6ed0bf 100644
--- a/lib/plausible_web/templates/stats/shared_link_password.html.heex
+++ b/lib/plausible_web/templates/stats/shared_link_password.html.heex
@@ -3,7 +3,12 @@
<:subtitle>
This link is password-protected. Please enter the password to continue to the dashboard.
</:subtitle>
- <.form :let={f} for={@conn} action={"/share/#{@link.slug}/authenticate"}>
+ <.form
+ :let={f}
+ for={@conn}
+ action={Routes.stats_path(@conn, :authenticate_shared_link, @link.slug) <> "?#{@query_string}"}
+ method="post"
+ >
<div class="my-6">
<.input type="password" field={f[:password]} />
diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs
index e6bc9ecaa9..5ee37dfd82 100644
--- a/test/plausible_web/controllers/stats_controller_test.exs
+++ b/test/plausible_web/controllers/stats_controller_test.exs
@@ -1487,6 +1487,78 @@ defmodule PlausibleWeb.StatsControllerTest do
conn = get(conn, "/share/#{site2.domain}?auth=#{link2.slug}")
assert html_response(conn, 200) =~ "Enter password"
end
+
+ test "preserves query parameters during password authentication", %{conn: conn} do
+ site = new_site()
+
+ link =
+ insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
+
+ filters = "f=is,country,EE&l=EE,Estonia&f=is,browser,Firefox"
+
+ conn =
+ get(
+ conn,
+ "/share/#{site.domain}?auth=#{link.slug}&#{filters}"
+ )
+
+ assert html_response(conn, 200) =~ "Enter password"
+ html = html_response(conn, 200)
+
+ assert html =~ ~s(action="/share/#{link.slug}/authenticate?)
+ assert html =~ "f=is,browser,Firefox"
+ assert html =~ "f=is,country,EE"
+ assert html =~ "l=EE,Estonia"
+
+ conn =
+ post(
+ conn,
+ "/share/#{link.slug}/authenticate?#{filters}",
+ %{password: "password"}
+ )
+
+ expected_redirect =
+ "/share/#{URI.encode_www_form(site.domain)}?auth=#{link.slug}&#{filters}"
+
+ assert redirected_to(conn, 302) == expected_redirect
+
+ conn =
+ post(
+ conn,
+ "/share/#{link.slug}/authenticate?#{filters}",
+ %{password: "WRONG!"}
+ )
+
+ html = html_response(conn, 200)
+ assert html =~ "Enter password"
+ assert html =~ "Incorrect password"
+
+ assert text_of_attr(html, "form", "action") =~ "?#{filters}"
+
+ conn =
+ post(
+ conn,
+ "/share/#{link.slug}/authenticate?#{filters}",
+ %{password: "password"}
+ )
+
+ redirected_url = redirected_to(conn, 302)
+ assert redirected_url =~ filters
+
+ conn =
+ post(
+ conn,
+ "/share/#{link.slug}/authenticate?#{filters}",
+ %{password: "password"}
+ )
+
+ redirect_path = redirected_to(conn, 302)
+
+ conn = get(conn, redirect_path)
+ assert html_response(conn, 200) =~ "stats-react-container"
+ assert redirect_path =~ filters
+ assert redirect_path =~ "auth=#{link.slug}"
+ end
end
describe "dogfood tracking" do