Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 2 · Lesson 11 · 4 min read

IDOR and Broken Access Control Explained

On this page
  1. What is an IDOR?
  2. IDOR example: vulnerable and fixed code
  3. Where IDOR bugs hide
  4. What doesn't fix IDOR
  5. How to test for IDOR and enforce tenant isolation
  6. Check yourself

What is an IDOR?

A user opens their invoice at /api/invoices/1042. Out of curiosity they try 1041. It loads: someone else's name, address and purchases. No hacking tools, no special skills. Insecure direct object references (IDOR) are this simple, and they're behind many real breaches, from telecom customer records to public sector data.

IDOR is the most common case of broken access control, ranked first in the OWASP Top 10 (Day 6), and the same bug that APIs call BOLA (see the API design course).

IDOR example: vulnerable and fixed code

JavaScript
// VULNERABLE: logged in is the only check
app.get('/api/invoices/:id', requireLogin, async (req, res) => {
  const invoice = await db.invoices.findById(req.params.id);
  res.json(invoice);                       // any invoice, for any logged-in user
});

// FIXED: ownership is part of the lookup
app.get('/api/invoices/:id', requireLogin, async (req, res) => {
  const invoice = await db.invoices.findOne({
    where: { id: req.params.id, customerId: req.user.customerId },
  });
  if (!invoice) return res.status(404).json({ title: 'Not found' });   // same answer for "missing" and "not yours"
  res.json(invoice);
});

Putting the owner condition in the query is better than loading the record and checking afterwards: it can't be forgotten, and unauthorized data never enters memory. Returning 404 for both "doesn't exist" and "not yours" stops attackers learning which IDs are real.

A request passes authentication, then an object-level check that the record belongs to the caller; without it, changing the ID returns someone else's data.
A request passes authentication, then an object-level check that the record belongs to the caller; without it, changing the ID returns someone else's data.

Where IDOR bugs hide

  • Every place an ID comes from the client: URL paths, query strings, JSON bodies, hidden form fields, headers.
  • Writes, not just reads: PATCH /addresses/88, DELETE /comments/12, POST /orders/77/refund.
  • Indirect references: an invoiceId inside a "send invoice by email" request, a fileKey in a download link.
  • Nested resources: /projects/5/tasks/900, where the project check passes but task 900 belongs to another project.
  • Batch endpoints and exports: GET /reports?ids=1,2,3,....
  • Function-level access: admin endpoints that only the UI hides (POST /api/users/42/role).

What doesn't fix IDOR

  • Random UUIDs instead of numbers. They make guessing harder, but IDs leak through shared links, logs, emails and other API responses. Use them as defense in depth, not as the control.
  • Hiding buttons in the UI. Attackers call the API directly.
  • Checking the role only. "Is a customer" says nothing about which customer (Day 5).

How to test for IDOR and enforce tenant isolation

  • Two-account tests: for every endpoint, create data as user A and try to read, change and delete it as user B. Automate this in integration tests.
  • Tools: Burp Suite extensions like Autorize replay each request with another user's session and flag those that succeed.
  • Code review checklist: every query that takes an ID from the request must be scoped to the caller (user, tenant or permission).
  • Multi-tenant systems: enforce the tenant filter centrally, for example with PostgreSQL row-level security or an ORM-level default scope, so one missed WHERE can't leak across customers.

Here's row-level security on PostgreSQL 16. The policy adds the tenant condition to every query the app role runs:

SQL
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
  USING (tenant_id = current_setting('app.tenant_id')::int);

-- per request, as the application's role:
BEGIN;
SET LOCAL app.tenant_id = '10';
SELECT * FROM invoices;                 -- no WHERE clause: returns only tenant 10's invoices 1 and 2
SELECT * FROM invoices WHERE id = 3;    -- tenant 20's invoice: 0 rows
COMMIT;

The developer forgot the tenant filter in the first query, and it still couldn't leak. Two details: the table's owner and superusers bypass policies unless you add ALTER TABLE invoices FORCE ROW LEVEL SECURITY, so the app must connect as a separate role; and SET LOCAL keeps the setting inside one transaction, so a pooled connection can't carry one tenant's ID into the next request.

Check yourself

It doesn't confirm the object exists, so attackers can't use the API to map valid ids or learn about other users' data. To the requester, an object they can't access simply isn't there.

IDs leak in links, logs and other responses; anyone who obtains one can still access the object without an ownership check.

Create resources as one user and attempt every read, update and delete on them as a different user, ideally in automated tests.

With row-level security: a policy adds the tenant condition to every query the application role runs, so a missing WHERE clause can't return other tenants' rows.

In updates and deletes, bulk and export endpoints, file downloads, nested resources, and ids inside request bodies or GraphQL arguments.