Protecting user data and ensuring application integrity
def delete_user(conn, %{'id' => id}) do
Repo.delete!(User |> Repo.get!(id))
send_resp(conn, 204, "")
end
In this vulnerable code snippet, the application is deleting a user based on the provided id without checking if the authenticated user has the necessary permissions to perform the operation.
def delete_user(conn, %{'id' => id}) do
case conn.assigns.current_user.role do
:admin ->
Repo.delete!(User |> Repo.get!(id))
send_resp(conn, 204, "")
_ ->
send_resp(conn, 403, "Forbidden")
end
end
In this secure version, before deleting a user, the application checks if the current user has the 'admin' role. If the user doesn't have the necessary permissions, the application returns a 403 Forbidden status code.