Broken access control and IDOR: the #1 web risk
Insecure direct object references let users read or change other people's data just by changing an ID. Why it's the most common serious bug in web apps, how to find it and how to fix it for good.
In one sentence
If /api/orders/1042 works for a user who doesn't own order 1042, that's an IDOR. Every query that loads a record by ID must also filter by who's asking.
The idea
The endpoint checks that you're logged in, loads the order with the ID in the URL and returns it. It never asks whether this order belongs to you. Changing 1041 to 1042 shows a stranger's name and address.
{
"id": 1041,
"owner": 7,
"name": "Ana López",
"address": "Calle Mayor 12, Madrid",
"total": "€48.90"
}Shapes it takes
- Horizontal: user A reads or edits user B's data (orders, messages, invoices, files).
- Vertical: a normal user calls an admin endpoint (
POST /api/admin/refund) that only hides its button in the UI. - Mass assignment:
PATCH /api/mewith{ "role": "admin" }because the server saves the whole body. - Indirect IDs: the ID is in a hidden field, a GraphQL argument, a file name or an export URL instead of the path.
Fixing it structurally
Vulnerable
- db.order.findUnique({ where: { id } })
- Checks login only; trusts the ID
- Role check only in the React component
Safe
- db.order.findFirst({ where: { id, userId: session.userId } })
- Scope every query by owner or tenant
- Authorization in a server-side policy, reused everywhere
- Deny by default: a new endpoint without a policy should fail closed.
- Scope queries, don't check after: filtering by owner in the query can't be forgotten in one branch.
- Allowlist writable fields to prevent mass assignment (
pick(body, ['name', 'avatar'])). - Test it: for every endpoint, an automated test that a second user gets 404.
Key takeaways
- 01IDOR = loading a record by ID without checking the requester may access it.
- 02Scope every query by owner or tenant on the server.
- 03Hiding UI isn't access control; check every endpoint, including admin ones.
- 04Allowlist writable fields and write a second-user test for each endpoint.
Check your understanding
1 of 3
What is an IDOR?
Related concepts
AuthN vs AuthZ
Authentication answers *who are you?* Authorization answers *are you allowed to do this, to this thing, right now?* Most breaches of web apps are the second one missing.
6 min read →Least privilege
Assume something will get hacked. Least privilege makes sure that when it does, the attacker gets the keys to one room, not the whole building.
6 min read →JWT
A JWT is base64, not encryption: anyone can read it. Its signature only proves the server issued it, and only if you verify it with a fixed algorithm and check the expiry.
8 min read →