Prevent unauthorized changes to product data by validating permissions and data before processing requests
defmodule MyAppWeb.ProductController do
use MyAppWeb, :controller
def update(conn, params) do
# No permissions or date validation
MyApp.update_product(params)
send_resp(conn, 200, "Product updated")
end
end
This code is vulnerable because it doesn't validate the permissions or the dates in 'params' before updating the product. An attacker can ignore frontend restrictions and send a request that the server processes as valid, potentially causing data integrity and availability issues.
defmodule MyAppWeb.ProductController do
use MyAppWeb, :controller
def update(conn, params) do
if MyApp.check_permissions(conn) && validate_dates(params) do
MyApp.update_product(params)
send_resp(conn, 200, "Product updated")
else
send_resp(conn, 403, "Invalid request")
end
end
defp validate_dates(params) do
# Implement your date validation logic here
end
end
This code is safe because it checks permissions and validates dates before updating the product. If the request fails these checks, it returns an error response instead of processing an invalid or unauthorized request.