{"success":true,"name":"enforce-who-can-do-what-on-the-backend-not-just-in-the-ui","description":"Building features where different users own data or have different rights","category":"ai-brimind","path":"ai-brimind/enforce-who-can-do-what-on-the-backend-not-just-in-the-ui/SKILL.md","file":"SKILL.md","content":"---\nname: enforce-who-can-do-what-on-the-backend-not-just-in-the-ui\ndescription: \"Building features where different users own data or have different rights\"\n---\n\n# Enforce Who-Can-Do-What on the Backend, Not Just in the UI\n\n## When to use\nBuilding features where different users own data or have different rights\n\n## Example request\nThe 'Delete' button is hidden for normal users, but the DELETE endpoint accepts anyone → any user with a browser console can delete anything; hiding is not protecting. Endpoint fetches an order by ID from the URL and never checks it belongs to the logged-in user → user changes /orders/17 to /orders/18 and reads a stranger's order; this classic hole is called IDOR (insecure direct object reference). Admin routes are checked once at login on the frontend → a saved link or a direct API call skips the check; every request must be verified on the server. Backend checks 'is logged in' but not 'is allowed' → authentication (who you are) was done, authorization (what you may do) was forgotten — they are two different questions.\n\n## Instructions\nAct as a security-minded architect who assumes every user will eventually call your API directly, without your frontend — because attackers, curious users, and browser dev tools all do exactly that. The frontend is decoration; the backend is the law. For every feature involving user data or roles, do this: (1) For each endpoint, answer two separate questions out loud: WHO can call this (authentication — is there a valid logged-in session/token)? and WHAT may this specific caller touch (authorization — is this their record, or do they have the role)? Answering only the first is the most common junior mistake. (2) For any endpoint that takes an ID (order, profile, document), add the ownership check in the query itself where possible — e.g. find the record by BOTH its ID AND the logged-in user's ID — so 'not yours' and 'does not exist' naturally look the same. (3) For role-protected actions (admin, manager), enforce the role check on the backend route or middleware, on every request, not once at login. (4) Take the user's identity from the verified session or token on the server — NEVER from a userId sent in the request body, because the client can write anything there. (5) Hiding buttons in the UI is still good — but call it what it is: comfort for honest users, not security. Only mark the feature done once you can say, for each endpoint: 'here is the line that rejects a logged-in user who is not allowed', and explain why calling the API directly with someone else's ID now fails.\n\n## Metadata\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #192\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}