The idea is to embed a LiveView page in an iframe on a website with different origin.
Has anyone had success running Phoenix LiveView in iframe from different domain? I’m getting LiveView session was misconfigured or the user token is outdated. warning and 403 error response on web socket connect (the server-rendered html gets returned fine) even though csrf_meta_tag/0 is set, most likely since Connect Info: %{session: nil} where session is not a map. The “iframed” page’s headers are:
Thanks - I was also looking to put a Liveview in an iFrame and your solution helped. Do you know what the security implications of doing this are? I use Liveview across the site, but for the iframe content have created a new, separate socket without the session info.
Try to remove :put_secure_browser_headers from the :browser pipeline in router.ex. This seems to work for me.
For best security practice I think one must look into modifying the response headers set (by put_secure_browser_headers and others) to make them as restrictive as possible but still allow iframe from other sources. I think Content-Security-Policy header looks like a good place to start.
It should only be necessary to clear the x-frame-options header while keeping the rest of the secure headers. Which you can do by adding a plug after secure headers implemented like so:
defmodule MyApp.Plugs.ClearXFrameOptions do
import Plug.Conn
def init(default), do: default
def call(conn, _opts) do
delete_resp_header(conn, "x-frame-options")
end
end
This thread, along with others, also came up in my search results when I was trying to solve embedding a LiveView in an iframe.
So linking here to my current solution/conclusion:
After trying several things I gathered around the internet, I found the 3 minimal steps which limit security-related changes to the embeddable LiveViews only.
Separate LiveView Socket.
Separate Router Pipeline replacing x-frame-options HTTP header with a restrictive CSP.
Separate layout for embeddable LiveViews independent from session-based assigns.