IDOR and Broken Access Control: Examples, Code and Fixes
Security TestingHetz Team

IDOR and Broken Access Control: Examples, Code and Fixes

What IDOR is, why broken access control is so common, a code example of the bug and its fix, and how to test for it with two accounts before attackers do.

An insecure direct object reference, or IDOR, is one of the most common and most damaging bugs in web applications. It happens when an application takes an identifier from a request, such as an invoice number or a project ID, and returns or changes the matching record without checking that the person asking is allowed to access it. The fix is simple in principle: make every lookup depend on who is asking. The difficulty is that the check is easy to forget on any one endpoint.

This guide explains how the bug works, shows a short code example of the vulnerable version and the fixed one, and walks through how to test for it with two accounts. For the wider category, read our OWASP Top 10 explainer, which places IDOR under broken access control.

How an IDOR Works

Imagine an invoice page at a URL that ends with a number. The page loads invoice 1041 when you open it, and the number is visible in your address bar. Nothing looks wrong. A user who changes 1041 to 1042 may see someone else's invoice, if the server only checks that they are logged in and then loads whatever number was requested.

The server did the first half of its job. It confirmed a session. It skipped the second half, which is confirming that this session is allowed to see this particular invoice. That gap is the bug. OWASP describes this family of problems on its Broken Access Control page, where it sits at the top of the Top 10.

The same pattern appears in many forms: a user ID in a profile request, a document ID in a download link, a team ID in an invitation, an order ID in a refund request. The identifier can be a sequence number, a UUID or a slug. Changing the format does not change the bug.

A Code Example: The Bug and the Fix

Here is a simplified Express-style endpoint that returns an invoice. The vulnerable version looks reasonable, because it requires a logged-in user.

// Vulnerable: any signed-in user can fetch any invoice by changing the ID
app.get("/api/invoices/:id", requireLogin, async (req, res) => {
  const invoice = await db.invoices.findById(req.params.id);
  res.json(invoice);
});

The fixed version does the same job, but the lookup is limited to invoices that belong to the caller's organization. The organization comes from the verified session on the server, never from a value the client sends.

// Fixed: the lookup only matches invoices the caller's organization owns
app.get("/api/invoices/:id", requireLogin, async (req, res) => {
  const invoice = await db.invoices.findOne({
    id: req.params.id,
    orgId: req.user.orgId, // from the verified session, not from the request
  });
  if (!invoice) {
    return res.status(404).json({ error: "Not found" });
  }
  res.json(invoice);
});

Three details matter. First, the ownership condition is part of the query itself, so the database does the filtering and the check cannot be skipped by a later line of code. Second, a missing record returns the same response whether it does not exist or belongs to someone else, which makes it harder to guess which IDs are valid. Third, the value that defines ownership comes from req.user, which the server built from the token, not from a field the client can edit.

Where the Bug Hides

IDOR rarely shows up on the main screen. It hides in the places that were built quickly or that the interface does not show. The table below lists the endpoint types that most often lack the ownership check, and what to look for in each.

Endpoint typeWhy it is riskyWhat to try with a second account
Detail views and downloadsLoad one record by IDRequest the first account's ID directly
Updates and deletesChange data, not just read itSend a change using the other account's ID
Nested routesThe parent is checked, the child is notUse a child ID under another account's parent
Exports and reportsQuery in bulk, often differentlyExport as the second account and inspect the rows
Search and countsReturn partial data about other recordsSearch for a term that only the first account uses
Webhooks and background jobsRun without the user's sessionTrigger the job with another account's identifiers

Why It Keeps Happening

IDOR is rarely a single missing line. It usually comes from a few patterns that repeat across a codebase:

  • Checking login instead of ownership. The middleware confirms that someone is signed in, and every route assumes that is enough.
  • Trusting the client's tenant or organization ID. The request says which organization it belongs to, and the server believes it.
  • Checking the parent but not the child. The route checks the project, then loads a file in that project by its own ID without checking the link.
  • Forgetting the less visible endpoints. Exports, counts, bulk updates, search and background jobs often query the database in a different way from the main screen.
  • Relying on the interface. The button that performs the action is hidden for other users, so nobody on the team ever tries the endpoint with another account.

The OWASP Authorization Cheat Sheet recommends denying access by default and enforcing it on every request, which is exactly the pattern shown above.

How to Test for IDOR

Testing for IDOR is simple, and it works best when it is systematic.

  1. Create two accounts in the same role, ideally in different organizations, and give each some data.
  2. Log in as account A and record every identifier you see: in URLs, in JSON responses, in download links and in the names of files.
  3. Log in as account B and send the same requests using A's identifiers. Include reads, updates and deletes.
  4. Repeat across roles. A member should not reach admin objects, and a viewer should not change data through an endpoint the interface never uses.
  5. Check the response body, not only the status code. Some endpoints return 200 with an empty object or a partial record and still leak fields.
  6. Include hidden paths. Look at exports, search results, counts and any endpoint that appears in the traffic of the mobile app.

A pass means the second account receives a refusal or nothing. A failure means you have proof: the request, the response and the data it exposed.

Preventing IDOR in Practice

The most reliable prevention is structural. Put the ownership rule in one shared function that every data access goes through, so new endpoints inherit it by default. For example, a single helper that loads a record only through the caller's organization can be used by the read, update and delete routes alike. Where the database supports it, row-level policies can enforce the same rule at the data layer. Our guide to Supabase security and row level security covers that approach for a database that the browser reaches directly.

Code review helps, but it has limits. A reviewer reads the route they are shown and may not notice a sibling route that loads the same records through a different path. That is why the test matters as much as the review. An automated check that calls every route with two accounts finds the forgotten ones, including the endpoints that were added last year and nobody has opened since.

Review every new endpoint against three questions: who is allowed to call it, which objects can they reach, and where does the server get the caller's identity. Add a test that uses two accounts for each route that takes an identifier. That test is cheap to write and catches the regression the next time someone copies a route.

Common Misconceptions

"Our IDs are random, so nobody can guess them." Random IDs make guessing harder, but they do not stop an attacker who has seen an ID in a shared link, an email, an error message or another response. The server must check ownership even when the ID is hard to guess.

"The page doesn't show other users' data, so there is no bug." The interface is not the security boundary. Test the API underneath it, because the endpoint may return data the page never displays.

"Our frontend never calls that endpoint, so nobody can reach it." Anyone with a valid token can call any endpoint the server exposes, and old routes often remain live after the screen that used them was removed. If the route answers, it needs the same check as the rest.

"We only have a few users, so the risk is low." IDOR matters most in multi-tenant products, where every customer's data sits in the same system. A small number of users in one tenant can still reach another tenant if the check is missing.

Where This Shows Up in Testing

IDOR is one of the first things an automated, agent-driven test looks for, because it can be proven with a simple comparison. Hetz runs this check across the endpoints it discovers, using accounts you provide for each role, and only reports what it can reproduce with the exact request and response. Each finding comes with a fix written for your stack and a retest after the change. Our SaaS penetration testing page explains how the multi-tenant checks are scoped.

For the API side, see API security testing, and for the account side, see how to check if a website is secure.

Next steps

This week, pick one endpoint that takes an ID in your product and run the two-account test on it. If it fails, fix the lookup with the ownership condition in the query, then run the same request again. Then repeat for the rest of your API. You can see how a full run covers these checks on the home page, compare plans on pricing, or start a scan.

FAQ

Frequently asked questions

Keep reading