{"success":true,"name":"recompute-prices-and-totals-on-the-backend-never-trust-numbe","description":"Handling a request where the client sends amounts, prices, totals, or discounts","category":"ai-brimind","path":"ai-brimind/recompute-prices-and-totals-on-the-backend-never-trust-numbe/SKILL.md","file":"SKILL.md","content":"---\nname: recompute-prices-and-totals-on-the-backend-never-trust-numbe\ndescription: \"Handling a request where the client sends amounts, prices, totals, or discounts\"\n---\n\n# Recompute Prices and Totals on the Backend: Never Trust Numbers From the Client\n\n## When to use\nHandling a request where the client sends amounts, prices, totals, or discounts\n\n## Example request\nCheckout endpoint saves the 'total' field from the request body → anyone can edit the request and buy for 1; the total must be recomputed server-side from real DB prices. Discount percentage arrives from the frontend → the backend must re-check the coupon's validity and value itself; the frontend copy was for display only. Quantity arrives from the client → quantity is a legitimate user CHOICE, but price-per-unit is not; take choices from the client, take facts from the database. Points or credits balance sent in the request 'to save a lookup' → the client can send any balance; balances live in the DB and only the DB copy counts.\n\n## Instructions\nAct as a backend developer who treats every number arriving in a request body as typed by a stranger with an editor — because with browser dev tools, it effectively is. For any endpoint touching money, credits, or consequential values, do this: (1) Split the incoming fields into two lists and say them out loud: CHOICES the user is allowed to make (which product, what quantity, which coupon code) and FACTS about the world (unit price, discount value, total, current balance). Choices may come from the client; facts must never. (2) For every fact, fetch the authoritative value from the database or server config at request time: load the product's real price, validate the coupon and read ITS discount, read the user's actual balance. (3) Recompute the total on the backend from those server-side facts plus the client's choices, and use YOUR computed number for saving and charging. If the client also sent a total, you may compare it to yours and reject on mismatch — a nice tripwire — but never adopt theirs. (4) Validate the choices too: quantity is a positive integer within stock, the coupon is active and applies to this cart — a 'choice' of -5 quantity is an attack on your math. (5) This is one instance of a bigger rule worth naming: the client's copy of any data is for display; the server's copy is the truth. Only mark the endpoint done once you can point to the exact line where the total is rebuilt from database values, and explain why editing the request body in dev tools can no longer change what gets charged or saved.\n\n## Metadata\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #194\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}