Prevent malicious redirection and potential SSRF attacks
defmodule VulnerableController do
use MyApp.Web, :controller
def redirect(conn, _params) do
redirect_to = conn.host
conn
|> put_resp_header("location", redirect_to)
|> send_resp(302, "")
end
end
The following Elixir code is vulnerable because it uses the
host
from the
conn
object directly to construct a redirection URL. An attacker could provide a malicious host in the HTTP request's Host header to cause redirection to an external site or possibly exploit SSRF vulnerabilities.
defmodule SecureController do
use MyApp.Web, :controller
def redirect(conn, _params) do
redirect_to = "https://secure.example.com"
conn
|> put_resp_header("location", redirect_to)
|> send_resp(302, "")
end
end
The following Elixir code is secure because it does not use the
host
from the
conn
object directly. Instead, it uses a predefined host for the redirection, preventing potential misuse of the Host header.