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
Dynamically set up session domain (#3089)
hq1eb397a6c2574
diff --git a/config/runtime.exs b/config/runtime.exsindex 22b6490296..cfcf85ff43 100644--- a/config/runtime.exs+++ b/config/runtime.exs@@ -233,7 +233,7 @@ if byte_size(websocket_url) > 0 andCross-domain websocket authentication is not supported for this server.WEBSOCKET_URL=#{websocket_url} - host must be: '#{base_url.host}',- because BASE_URL=#{base_url} so the host is ``.+ because BASE_URL=#{base_url}."""enddiff --git a/lib/plausible_web/endpoint.ex b/lib/plausible_web/endpoint.exindex a0f5411e07..8009ccdc17 100644--- a/lib/plausible_web/endpoint.ex+++ b/lib/plausible_web/endpoint.ex@@ -9,6 +9,7 @@ defmodule PlausibleWeb.Endpoint do# 5 years, this is super long but the SlidingSessionTimeout will log people out if they don't return for 2 weeksmax_age: 60 * 60 * 24 * 365 * 5,extra: "SameSite=Lax"+ # domain added dynamically via RuntimeSessionAdapter, see below]# Serve at "/" the static files from "priv/static" directory.@@ -52,7 +53,7 @@ defmodule PlausibleWeb.Endpoint doplug Plug.MethodOverrideplug Plug.Head- plug Plug.Session, @session_options+ plug PlausibleWeb.Plugs.RuntimeSessionAdapter, @session_optionssocket "/live", Phoenix.LiveView.Socket,websocket: [diff --git a/lib/plausible_web/plugs/runtime_session_adapter.ex b/lib/plausible_web/plugs/runtime_session_adapter.exnew file mode 100644index 0000000000..ae5b1f2217--- /dev/null+++ b/lib/plausible_web/plugs/runtime_session_adapter.ex@@ -0,0 +1,30 @@+defmodule PlausibleWeb.Plugs.RuntimeSessionAdapter do+ @moduledoc """+ A `Plug.Session` adapter that allows configuration at runtime.+ Sadly, the plug being wrapped has no MFA option for dynamic+ configuration.++ This is currently used so we can dynamically pass the :domain+ and have cookies planted across one root domain.+ """++ @behaviour Plug++ @impl true+ def init(opts) do+ Plug.Session.init(opts)+ end++ @impl true+ def call(conn, opts) do+ Plug.Session.call(conn, patch_cookie_domain(opts))+ end++ defp patch_cookie_domain(%{cookie_opts: cookie_opts} = runtime_opts) do+ Map.replace(+ runtime_opts,+ :cookie_opts,+ Keyword.put_new(cookie_opts, :domain, PlausibleWeb.Endpoint.host())+ )+ end+end