To ensure proper session management and invalidate sessions appropriately
defmodule MyApp.Accounts do
def get_user_email(conn) do
session = Plug.Conn.get_session(conn, :user)
if session == "auto", do: "test@test.com", else: session.email
end
end
In the insecure code example, the
get_user_email
function checks the session to find the user's email. However, there is debug code left in that allows anyone to pass in a session with the word 'auto' and get access to a testing email. This is insecure as it allows unauthorized access.
defmodule MyApp.Accounts do
def get_user_email(conn) do
session = Plug.Conn.get_session(conn, :user)
session.email
end
end
In the secure code example, the
get_user_email
function only checks the session for the user's email, and there is no debug code that can be exploited for unauthorized access.