If youāre accessing it from āoutsideā youāll probably have to unset the header, hereās a Plug for that (you can add it to a relevant Phoenix pipeline):
defmodule App.Plug.Restrict.AllowIframe do
@moduledoc """
Allows affected ressources to be open in iframe.
"""
alias Plug.Conn
def init(opts \\ %{}), do: Enum.into(opts, %{})
def call(conn, _opts) do
Conn.delete_resp_header(conn, "x-frame-options")
end
end
I dināt play with the options that much though, there maybe a way to whitelist your origin which would be a bit safer.
defmodule App.Restrict.AllowIframe do
@moduledoc """
Allows affected ressources to be open in iframe.
"""
alias Plug.Conn
def init(opts \\ %{}), do: Enum.into(opts, %{})
def call(conn, _opts) do
Conn.put_resp_header(conn,"x-frame-options","ALLOW-FROM https://example.com")
end
end
Iām working on allowing iframe embeds from another site to my phoenix server.
Hereās the trouble Iām running into:
doing Conn.put_resp_header(conn, "content-security-policy", "frame-src" 'self' https://mydomain")
results in the frame not displaying due to an error of āx-frame-optionsā being set to āSAMEORIGINā in chrome.
doing Conn.put_resp_header(conn, "x-frame-options", "ALLOW-FROM https://mydomain")
as suggested above allows the iframe to work. However, I still get an error, even though the frame displays.
Problem: This doesnāt seem to be a whitelist, but Iām not certain.
The iframe is properly displaying on my whitelisted domain, on a completely different webservice than my Phoenix server. However, I can use a different computer, not on the white list, to display an iframe containing the site when hosted locally with a simple index.html and <iframe> tag.
The error it displays when running the index.html on my local, non-whitelisted computer is: Invalid 'X-Frame-Options' header encountered when loading 'http://myPhoenixServer 'ALLOW-FROM https://MyOtherHost' is not a recognized directive. The header will be ignored.
But, it still pulls the webpage from the phoenix host and displays it in the iframe, CSS/JS/HTML and all.