Authentication vs. authorization is the split between proving who is calling and deciding what that caller may do. Authentication checks the session, the password, or the signed token. Authorization checks the object: this invoice, this file, this project, for this user, on this action. Teams expose user data when they stop at the first check. The request is logged in, the handler loads id from the URL, and the response is someone else's row. The UI never linked there. The API did not care.
If you are building an app with accounts and you are also the only person reviewing the code, this is the bug to test before launch. The OWASP Authorization Cheat Sheet is the reference. The rest of this guide is the two-account test and the code shape that passes it.
What each word is responsible for
Authentication answers who is calling. It fails with 401 when the credential is missing or forged. Authorization answers whether that identity may act on this object, and it fails with 403 or 404 when the person is real and the row is not theirs. Use both on every sensitive route.
Keep the sources separate. The user id inside a verified session is authentication output you may trust. A user id in the query string, a hidden form field, or a JSON body is input the caller can set to any value. Use the session id to look up what they own. Do not compare the session id to a client-supplied id and call that a permission check. An attacker sends the same number in both places.
| Question | Authentication | Authorization |
|---|---|---|
| What it proves | The caller is user 42 | User 42 may read invoice 900 |
| Typical input | Password, passkey, OAuth code, session cookie | Object id plus the action (read, update, delete) |
| Trusted identifier | Subject inside a verified session or token | Rows in your database tying that subject to the object |
| Failure | 401 Unauthorized | 403 Forbidden or 404 Not Found |
| Common false comfort | "The dashboard loads, so we are good" | "The button is hidden for other people's items" |
Broken access control stays at the top of the OWASP Top 10 because this table is easy to half-implement. Login is visible. The missing where owner_id = ? is one line you notice only when you try to fetch a foreign id.
How an IDOR exposes another user's data
IDOR means insecure direct object reference: the client supplies an id, and the server uses it without checking that this caller may use it. The id might be an integer, a UUID, or a filename. Unguessable ids only slow casual browsing. The owner check still has to live in the query, because ids leak through logs and the network panel.
A concrete case: a freelance tool has /api/invoices/1842. The handler does this:
const session = await requireUser(req)
const invoice = await db.invoice.findUnique({ where: { id: req.params.id } })
return invoice
requireUser proves authentication. The query has no authorization. User A, working as a logged-in customer, replaces 1842 with 1843 and downloads user B's line items, address, and email. The page for 1843 was never shown to A. The route still answered.
The repair is to bind the row to the session:
const session = await requireUser(req)
const invoice = await db.invoice.findFirst({
where: { id: req.params.id, accountId: session.accountId },
})
if (!invoice) {
return res.status(404).json({ error: 'Not found' })
}
return res.json(toInvoiceResponse(invoice))
Use the same filter on update and delete. A GET that checks ownership and a DELETE that does not is a common partial fix. Return 404 when the caller should not learn that the invoice exists. Return 403 when they can see it and may not change it, such as a read-only teammate. Pick the rule per resource and stick to it.
Horizontal access is user A touching user B's object. Vertical access is a normal user hitting an admin route. Test both. An admin flag that lives in localStorage or in a claim the client can rewrite is not a role. Store the role on the server and read it from the session you issued.
Check every route that accepts an id
Do this on staging with two accounts you control. Include server actions, GraphQL resolvers, and jobs that accept a user-supplied id. The list below is the whole test. A 200 that the UI later hides still fails, because the JSON already left the server.
- List routes that take an object id, a file key, an email, or a team slug. Exports, search, and webhooks belong on the list.
- For each route, write down the owner column and the action.
- As user A, call the route with an id you know belongs to user B. Repeat for GET, PATCH, and DELETE.
- Expect 404 or 403 and a body with none of B's fields. A 200 with a filtered client render still fails, because the JSON already left the server.
- Call an admin path with A's normal session. Expect a refusal.
- Send A's id in the body while the session is A's, then send B's id in the body while the session is still A's. The second call must not retarget the action.
- If you have teams, add a user who is in team 1 and an object that belongs to team 2. Membership in some team is not membership in the object's team.
- Add a regression test that fails the build when step 3 returns B's data. The test is the part that survives the next feature.
When a coding agent adds a route, say in the prompt that the owner comes from the session and that the two-account test is part of done. Generated handlers copy findUnique({ where: { id } }) because that snippet is the shortest way to "fetch the invoice." The API security guide places this check next to validation. The database guide covers row policies when the database enforces the same rule. A checkout must load the invoice as the current user before it charges anything.
A file download that ignored the session
A portfolio app stored files in a bucket and exposed /api/files/8f2c. The list page requested only the current user's files. The download route signed a URL for whatever key arrived. User A pasted a key from a support chat and received the object. The list filter had never applied to that second route.
The fix checked owner_id before signing. Review duplicate, export, and email-this actions the same way. A signed URL is safe after that check.
FAQ
If every object uses a UUID, am I protected?
A UUID stops people from incrementing an integer and walking the table. It does not stop anyone who can see an id they should not use. Ids appear in logs, emails, and the network panel. Keep unguessable ids if you like, and still filter on the owner. The check is what protects the row. The id format only reduces lucky guesses.
Can I enforce this only in the frontend?
The frontend can hide buttons so the product makes sense. It cannot enforce authorization, because the caller can talk to your API with curl and skip the component. Put the check in the server handler or in a database policy the handler cannot bypass. Then keep the UI honest so people do not click into errors.
Where should the check live in the code?
Put it as close to the data as you can: in the query, a policy, or a single loader function that every handler uses. A middleware that only checks "is logged in" cannot know the object yet. A helper named loadInvoiceFor(session, id) is easier to review than a copy of the filter in twelve files. If one handler queries the table directly, the helper is optional and the bug comes back.
What about public pages and shared links?
Public pages should use a separate query that returns only public fields, not the private loader with a flag. Shared links should use an unguessable token stored on the share row, with a scope (view or comment) and an expiry. Accepting the object's normal id on an unauthenticated route publishes the object to anyone who has seen the id.
What to do next
Pick the route that loads the most sensitive object in your app. Call it as user A with user B's id and read the raw JSON. If any of B's data comes back, add the owner constraint to every method on that resource and add a test that locks the behavior in.
Before you announce the product, run the same authentication vs. authorization test across the route table. The pre-launch security checklist puts this check near the top because it leaks customer data on day one. StaySecure can scan the repo for risky access patterns and explain them in plain language. The two-account request is still the test that tells you the scan and the code agree.