Prevent exposure of valid users' list
defmodule MyApp.UserController do
use MyApp.Web, :controller
def index(conn, _params) do
users = Repo.all(User)
render(conn, "index.json", users: users)
end
end
This insecure code is exposing the list of all users without any checks or restrictions. This could allow attackers to obtain information about valid users in the system.
defmodule MyApp.UserController do
use MyApp.Web, :controller
def index(conn, _params) do
if authorized?(conn) do
users = Repo.all(User)
render(conn, "index.json", users: users)
else
send_resp(conn, :unauthorized, "")
end
end
defp authorized?(conn) do
# Add authorization checks here
end
end
The secure code only provides the list of users if the user is authorized. It ensures that only the right users can see the user list.