Ensure the privacy and security of sensitive business information
defmodule UnsecuredController do
use Phoenix.Controller
def list_customers(conn, _params) do
customers = Repo.all(Customer)
render(conn, "index.html", customers: customers)
end
end
This code retrieves all customer information from the database and renders it without any authentication or authorization check. This can lead to exposure of sensitive customer data, which can be used to craft new attack vectors.
defmodule SecuredController do
use Phoenix.Controller
plug :authenticate_user
def list_customers(conn, _params) do
customers = Repo.all(Customer)
render(conn, "index.html", customers: customers)
end
defp authenticate_user(conn, _opts) do
# Check if user is authenticated and authorized
#... code ...
# If not authenticated or authorized, redirect or send error response
#... code ...
end
end
This code checks if the user is authenticated and authorized before retrieving and rendering customer data. This prevents unauthorized users from accessing sensitive customer data.