To protect sensitive cookies from being sent over insecure channels
defmodule Vulnerable do
use Plug.Router
plug :match
plug :dispatch
post "" do
conn
|> put_resp_cookie("sensitive_info", "some_value")
|> send_resp(200, "OK")
end
match _ do
send_resp(conn, 404, "Not found")
end
end
In this Elixir code snippet, a cookie is being set without the Secure attribute, making it susceptible to being sent over insecure channels.
defmodule Secure do
use Plug.Router
plug :match
plug :dispatch
post "" do
conn
|> put_resp_cookie("sensitive_info", "some_value", secure: true)
|> send_resp(200, "OK")
end
match _ do
send_resp(conn, 404, "Not found")
end
end
In this Elixir code snippet, the cookie is set with the Secure attribute, ensuring it will only be sent over secure channels.