IDOR: the most common API bug, and how to design it out
Change one number in a URL and read someone else's data. Why insecure direct object references keep appearing in bug bounty reports, and how to make object-level authorization the default in a Spring service instead of a per-endpoint chore.
The most common serious bug in APIs isn’t clever. It looks like this:
GET /api/orders/1042 → 200 OK, your order
GET /api/orders/1043 → 200 OK, someone else's order
That’s an insecure direct object reference, or IDOR: the API takes an identifier from the request and returns the object without checking that the caller is allowed to see it. OWASP’s API Security Top 10 ranks the general problem, broken object-level authorization, as the number one risk, and IDORs show up again and again in publicly disclosed bug bounty reports. They’re popular with researchers for a simple reason: they’re easy to find, and the impact is real data.
I’ve spent a lot of my career on the other side of this: building authentication and authorization into services with Apache Shiro and Keycloak, and writing the shared auth modules that a group of services all depend on. This post is how I’d design IDOR out of a Spring service, so that safety is the default rather than something every endpoint has to remember.


Why it keeps happening
Frameworks make authentication automatic. A filter checks the token on every request, and if it’s missing or expired, the request never reaches your code. That feels like security, and it is, for the question it answers: who is calling?
Object-level authorization asks a different question: may this caller see this particular object? No framework can answer that for you, because the answer lives in your data model: orders belong to customers, documents to workspaces, invoices to tenants. So it ends up as a line of code inside each endpoint, and a line of code that has to be remembered is a line of code that eventually gets forgotten.
The vulnerable version is completely ordinary code:
@GetMapping("/orders/{id}")
OrderDto get(@PathVariable long id) {
return orders.findById(id) // whose order? nobody asked
.map(OrderDto::from)
.orElseThrow(NotFoundException::new);
}
It passes code review, because nothing looks wrong. It passes tests, because the tests fetch their own order.
Fix 1: make the query carry the owner
The most reliable fix is in the data layer: there is no method that loads an order by id alone. Every lookup takes the owner, so forgetting the check isn’t possible, because there’s nothing to forget:
interface OrderRepository extends Repository<Order, Long> {
Optional<Order> findByIdAndCustomerId(long id, String customerId);
}
@GetMapping("/orders/{id}")
OrderDto get(@PathVariable long id, @AuthenticationPrincipal Jwt jwt) {
return orders.findByIdAndCustomerId(id, jwt.getSubject())
.map(OrderDto::from)
.orElseThrow(NotFoundException::new);
}
Extending Repository instead of JpaRepository matters here: it means there’s no inherited findById for someone to reach for later.
The owner comes from the token, never from the request. An API that accepts ?customerId=… or a tenantId field in the body and trusts it has the same bug with extra steps.
Fix 2: when ownership is more than one column
Real permissions are often richer: shared documents, team admins, support staff with read access. Then centralise the decision in one place and call it declaratively:
@Component("orderAccess")
class OrderAccess {
boolean canRead(long orderId, Authentication auth) {
return orders.ownerOf(orderId)
.map(owner -> owner.equals(auth.getName()) || hasRole(auth, "SUPPORT_READ"))
.orElse(false);
}
}
@PreAuthorize("@orderAccess.canRead(#id, authentication)")
@GetMapping("/orders/{id}")
OrderDto get(@PathVariable long id) { … }
One class holds the rules for orders. Reviewers can read it, and tests can exercise it directly. (Make sure method security is enabled with @EnableMethodSecurity, or the annotation silently does nothing.)
Fix 3: answer 404, not 403
If user B asks for order 1043 and gets 403 Forbidden, they’ve learned that order 1043 exists. Across millions of IDs that’s an information leak of its own: how many orders you have, how fast they grow. For objects the caller can’t see, return the same 404 Not Found you’d return for an object that doesn’t exist. The scoped query in Fix 1 does this naturally.
Fix 4: unguessable IDs help, but they aren’t the fix
Switching from sequential numbers to random UUIDs makes IDOR harder to exploit: an attacker can’t just count upwards. Do it; it’s good hygiene. But IDs leak: in URLs that get shared, in logs, in referrer headers, in other API responses, in emails. An API that’s only safe while its IDs stay secret isn’t safe. Treat unguessable IDs as defence in depth, never as the authorization check.
Fix 5: multi-tenant data, defence in depth
In a multi-tenant system, the worst version of IDOR crosses tenants: one customer company reading another’s data. There, add a second layer below the application:
- Take the tenant id only from the authenticated token, and set it once per request.
- Add the tenant to every query automatically (for example, a Hibernate filter), so individual repositories can’t skip it.
- If you’re on PostgreSQL, consider row-level security policies keyed on a session variable. Then even a query that forgets the tenant returns nothing from other tenants.
Both layers would have to fail for data to cross tenants.
The test that catches all of it
Every endpoint that takes an object id gets the same test: create the object as user A, request it as user B, expect 404.
@Test
void anotherCustomerCannotReadMyOrder() {
long orderId = createOrderAs("alice");
mvc.perform(get("/orders/{id}", orderId).with(jwt().jwt(j -> j.subject("bob"))))
.andExpect(status().isNotFound());
}
Write it once as a parameterised test over every route with an {id}, and new endpoints are covered the day they’re added. This is exactly what a security researcher does by hand with two accounts; doing it in CI means they don’t find it first.
The same bug in other clothes
| Where | What it looks like |
|---|---|
| Updates and deletes | PUT /addresses/77 changes someone else’s address. Writes need the same scoped lookup as reads. |
| Nested resources | /teams/5/members/91: the team check passes, but member 91 belongs to team 6 |
| GraphQL | node(id: "…") or order(id:) resolvers that load by id alone |
| File downloads | /files/invoice-1043.pdf served straight from storage without an ownership check |
| Bulk endpoints | ids=1,2,3,… checks the first id and returns all of them |
Takeaways
- Authentication answers who; object-level authorization answers which objects. Only the first is automatic.
- Put the owner in the query, and remove lookups by id alone from the repository.
- Take the user and tenant from the token, never from the request.
- Return 404 for objects the caller can’t see.
- One two-user test per endpoint catches the whole class of bug before anyone else does.