Reconfigure live websocket (#3087)

* Dynamically set :domain for the live socket

Ref: https://github.com/phoenixframework/phoenix_live_view/pull/2715

* Make runtime config raise before the user ends up in a reconnect loop

* hall of shame: remove console.info remnant

* Check origin on live websocket

* Get rid of single pipe
pull/3088/head r2023.06.28-135
hq1 3 years ago committed by GitHub
parent db5488efa6
commit 4ac83b6e5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,7 +13,6 @@ import LazyLoader from '../../components/lazy-loader'
export default function Funnel(props) {
console.info('funnah')
const [loading, setLoading] = useState(true)
const [visible, setVisible] = useState(false)
const [error, setError] = useState(undefined)

@ -227,6 +227,16 @@ log_failed_login_attempts =
websocket_url = get_var_from_path_or_env(config_dir, "WEBSOCKET_URL", "")
if byte_size(websocket_url) > 0 and
not String.ends_with?(URI.new!(websocket_url).host, base_url.host) do
raise """
Cross-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 ``.
"""
end
config :plausible,
environment: env,
mailer_email: mailer_email,

@ -54,7 +54,11 @@ defmodule PlausibleWeb.Endpoint do
plug Plug.Session, @session_options
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
socket "/live", Phoenix.LiveView.Socket,
websocket: [
check_origin: true,
connect_info: [session: {__MODULE__, :patch_session_opts, []}]
]
plug CORSPlug
plug PlausibleWeb.Router
@ -64,4 +68,12 @@ defmodule PlausibleWeb.Endpoint do
|> Application.fetch_env!(__MODULE__)
|> Keyword.fetch!(:websocket_url)
end
def patch_session_opts() do
# `host()` provided by Phoenix.Endpoint's compilation hooks
# is used to inject the domain - this way we can authenticate
# websocket requests within single root domain, in case websocket_url()
# returns a ws{s}:// scheme (in which case SameSite=Lax is not applicable).
Keyword.put(@session_options, :domain, host())
end
end

Loading…
Cancel
Save