Difficulty: Intermediate
What is IDOR (Insecure Direct Object Reference) and how do you prevent it as part of broken access control?
IDOR is an access control bug where an application uses an identifier from the request, such as /api/orders/1042, to fetch an object but never checks whether the logged-in user is allowed to access that object. An attacker simply changes the ID to 1043 and reads or modifies someone else's data. The fix is to enforce object-level authorization on the server for every request, usually by scoping the query to the current user or checking ownership and role before returning anything.
GET /api/invoices/5531 returns any invoice to any logged-in user.PATCH /api/users/77 with {"email": "attacker@x.com"} changes another user's email, leading to account takeover through password reset.GET /download?file=resume_8812.pdf serves other candidates' resumes.DELETE /api/admin/users/9 works for any token.The last case is function-level access control (wrong role can call an endpoint); the others are object-level (right endpoint, wrong object). OWASP API Security calls the object-level case BOLA (Broken Object Level Authorization), and Broken Access Control has been number one in the OWASP Top 10 since 2021, including the 2025 edition.
Authentication middleware proves who the user is, and developers assume that is enough. But authentication is not authorization. Each handler must also answer "may this user act on this object?" It is easy to forget on one endpoint out of hundreds, especially newer ones, bulk endpoints, exports, or GraphQL resolvers that load nested objects.
WHERE id = $1 AND user_id = $2 instead of WHERE id = $1. If nothing comes back, return 404.can(user, 'read', order) (CASL, Casbin, Pundit, Spring @PreAuthorize) so rules are not re-implemented in every handler.userId in the body; take it from the session or verified token.tenant_id, or use database row-level security (PostgreSQL RLS) as a safety net.Switching from sequential integers to UUIDs makes IDs harder to guess, which reduces mass enumeration, but it is not a fix. IDs leak through URLs, shared links, logs, emails and other API responses. Treat unguessable IDs as defence in depth, never as the access control itself.
Returning 404 Not Found for objects the user cannot access avoids confirming that the object exists. Use 403 Forbidden when the user knows the resource exists but lacks the role, such as a non-admin hitting an admin route.
| Vulnerable handler | Fixed handler | |
|---|---|---|
| Query | WHERE id = :id | WHERE id = :id AND user_id = :me |
| Who is checked | Only that a user is logged in | That the user owns the order |
| Request for order 1043 | Returns another user's data | Returns 404 Not Found |
| Source of user id | Sometimes trusted from body | Always from session or token |
// VULNERABLE: any logged-in user can read any order
app.get('/api/orders/:id', requireAuth, async (req, res) => {
const order = await prisma.order.findUnique({ where: { id: req.params.id } });
if (!order) return res.sendStatus(404);
res.json(order);
});
// FIXED: query is scoped to the caller; admins handled explicitly
app.get('/api/orders/:id', requireAuth, async (req, res) => {
const where = req.user.role === 'admin'
? { id: req.params.id }
: { id: req.params.id, userId: req.user.id };
const order = await prisma.order.findFirst({ where });
if (!order) return res.sendStatus(404); // do not reveal existence
res.json(order);
});
Ownership is enforced in the database query itself, so there is no window where another user's order is loaded into memory and accidentally returned.
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
-- Each request sets: SET app.user_id = '<uuid from verified token>';
CREATE POLICY orders_owner_only ON orders
USING (user_id = current_setting('app.user_id')::uuid);
-- Now even a forgotten WHERE clause cannot leak other users' rows
SELECT * FROM orders WHERE id = '6f1c...';
RLS enforces ownership in the database for the application role, catching mistakes in application code. Note that table owners and superusers bypass RLS unless FORCE ROW LEVEL SECURITY is set.
IDOR, Broken Access Control, Authorization, RBAC, Object-Level Authorization