To prevent the listing and exposure of credit card information by implementing proper validation.
defmodule MyApp.PaymentController do
use MyApp.Web, :controller
def create(conn, %{"card" => card_params}) do
{:ok, card} = MyApp.Card.create_card(card_params)
render(conn, "card.json", card: card)
end
end
This code is vulnerable because there's no validation of the expiration date and security code of the credit card. This lack of validation could allow an attacker to list credit card information.
defmodule MyApp.PaymentController do
use MyApp.Web, :controller
def create(conn, %{"card" => card_params}) do
with :ok <- MyApp.Card.validate_card(card_params),
{:ok, card} <- MyApp.Card.create_card(card_params) do
render(conn, "card.json", card: card)
else
_ -> send_resp(conn, :bad_request, "Invalid card information.")
end
end
end
In this secure code example, we've added a card validation step using the
validate_card
function. This function checks the expiration date and security code of the credit card before creating it.