To prevent clickjacking attacks
defmodule MyApp.Router do
use Plug.Router
plug :match
plug :dispatch
match _ do
send_resp(conn, 200, "Hello, world!")
end
defp put_headers(conn) do
put_resp_header(conn, "x-frame-options", "SAMEORIGIN")
end
end
The Elixir code sets the X-Frame-Options header to SAMEORIGIN. This header is deprecated and can be bypassed using several iframe layers, making it vulnerable to clickjacking attacks.
defmodule MyApp.Router do
use Plug.Router
plug :match
plug :dispatch
match _ do
send_resp(conn, 200, "Hello, world!")
end
defp put_headers(conn) do
put_resp_header(conn, "content-security-policy", "frame-ancestors 'self'")
end
end
The secure Elixir code sets the Content-Security-Policy header with the frame-ancestors 'self' directive, which is a more secure replacement for the X-Frame-Options header.