Ensure confidentiality and integrity of sensitive user data
defmodule UserController do
def show(conn, %{'id' => id}) do
user = Repo.get(User, id)
conn
|> put_resp_content_type('application/json')
|> send_resp(200, Poison.encode!(user))
end
end
The code below is insecure because it directly uses user input to construct the response. Sensitive data like password is included in the response without being masked, which exposes the data to potential eavesdroppers or data breaches.
defmodule UserController do
def show(conn, %{'id' => id}) do
user = Repo.get(User, id) |> Map.drop([:password])
conn
|> put_resp_content_type('application/json')
|> send_resp(200, Poison.encode!(user))
end
end
The code below is secure because it masks the password field when sending the user data in the response. The
Map.drop/2
function is used to remove the sensitive data from the map before it is sent in the response.