Broken Access Control

Difficulty: Intermediate

Question

What is IDOR (Insecure Direct Object Reference) and how do you prevent it as part of broken access control?

Answer

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.

What it looks like in practice

  • 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.
  • A mobile app hides an "admin" button, but 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.

Why it is so common

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.

How to prevent it

  1. Scope queries to the caller: WHERE id = $1 AND user_id = $2 instead of WHERE id = $1. If nothing comes back, return 404.
  2. Centralize the policy: a helper or policy layer like can(user, 'read', order) (CASL, Casbin, Pundit, Spring @PreAuthorize) so rules are not re-implemented in every handler.
  3. Deny by default: new routes should require an explicit permission, not be open until someone remembers to lock them.
  4. Never trust client-supplied ownership: ignore userId in the body; take it from the session or verified token.
  5. Multi-tenant isolation: in B2B apps, also filter by tenant_id, or use database row-level security (PostgreSQL RLS) as a safety net.
  6. Test it: write integration tests where user A requests user B's resources and expects 403 or 404.

Do random IDs fix it?

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.

403 or 404?

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 vs fixed order lookup. The only difference is one extra ownership condition, applied on the server.
Vulnerable handlerFixed handler
QueryWHERE id = :idWHERE id = :id AND user_id = :me
Who is checkedOnly that a user is logged inThat the user owns the order
Request for order 1043Returns another user's dataReturns 404 Not Found
Source of user idSometimes trusted from bodyAlways from session or token

Code examples

IDOR and the fix in an Express + Prisma handler

// 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.

PostgreSQL row-level security as a safety net

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.

Key points

  • IDOR: the server trusts an ID from the request without checking ownership
  • Authentication proves identity; authorization must still be checked per object
  • Scope every query to the current user or tenant
  • UUIDs reduce guessing but are not access control
  • Broken Access Control is ranked number one in the OWASP Top 10

Common follow-up questions

  • What is the difference between object-level and function-level authorization?
  • How would you implement authorization in a GraphQL API with nested resolvers?
  • How would you test a large API for IDOR systematically?
  • When would you use RBAC versus ABAC?

Interview tips

  • Use a concrete example like changing /orders/1042 to /orders/1043; interviewers want to see you get the attack
  • Stress that the fix is server-side and per object, not hiding buttons in the UI
  • Say explicitly that UUIDs are not a fix; it is a common trap question

Concepts covered

IDOR, Broken Access Control, Authorization, RBAC, Object-Level Authorization

More Web Security (OWASP, JWT, XSS, CSRF) questions