Prevent unauthorized access to sensitive user information
defmodule MyApp.CreditCardController do
use MyApp, :controller
def show(conn, %{'id' => id}) do
credit_card = CreditCard.get!(id)
render(conn, 'show.json', credit_card: credit_card)
end
end
The code is vulnerable because it allows the querying of credit card information based on the encrypted product number, but it does not verify if the account making the request has the necessary permissions to access that specific credit card information. This opens a pathway for an attacker to probe and potentially obtain sensitive credit card information.
defmodule MyApp.CreditCardController do
use MyApp, :controller
import Plug.Conn
def show(conn, %{'id' => id}) do
credit_card = CreditCard.get!(id)
if conn.assigns.current_user.id == credit_card.user_id do
render(conn, 'show.json', credit_card: credit_card)
else
conn
|> put_status(:forbidden)
|> put_resp_header('content-type', 'application/json')
|> send_resp(403, '{"error": "Not authorized"}')
end
end
end
This code is secure because it verifies if the user making the request has the necessary permissions to access the requested credit card data before providing it. This is achieved by comparing the user id in the session with the user id associated with the credit card. If the ids match, the information is provided. Otherwise, an error message is returned.