Secure transmission of sensitive information
defmodule UserController do
use MyAppWeb, :controller
def show(conn, %{'id' => id}) do
user = Repo.get!(User, id)
render(conn, 'show.html', user: user)
end
end
The above code is vulnerable because it allows the user id to be passed as a URL parameter. This exposes the user's sensitive information as the user id is visible in the URL and can be stored in browser history, sent to external sites via the referrer HTTP header, or intercepted by malicious scripts.
defmodule UserController do
use MyAppWeb, :controller
def show(conn, _params) do
id = get_session(conn, :user_id)
user = Repo.get!(User, id)
render(conn, 'show.html', user: user)
end
end
In the secure code example, the user id is obtained from a secure session or a cookie, rather than being passed in the URL. This mitigates the risk of sensitive information being exposed via the URL.