To prevent unauthorized access to user's personal information.
defmodule MyApp.UserController do
use MyApp, :controller
def show(conn, %{'id' => id}) do
user = MyApp.Repo.get(MyApp.User, id)
render(conn, 'show.html', user: user)
end
end
This code is vulnerable because it fetches and returns user information based solely on the user ID provided in the request, without any authentication or authorization checks. An attacker could potentially iterate over user IDs to retrieve personal information about all users.
defmodule MyApp.UserController do
use MyApp, :controller
def show(conn, %{'id' => id}) do
current_user = get_authenticated_user(conn)
if current_user && current_user.id == id do
render(conn, 'show.html', user: current_user)
else
send_resp(conn, :unauthorized, '')
end
end
defp get_authenticated_user(conn) do
# Fetch the current user from the session
end
end
This secure code example introduces authentication and authorization checks. Before accessing a user's information, the current user is fetched from the session, and the user can only access their own information. If an unauthenticated user or a user trying to access someone else's information attempts to use the 'show' action, they receive an 'unauthorized' response.