The pattern

routes/invoices.jsExpress
router.get("/api/invoices/:id", requireAuth, async (req, res) => {
  const invoice = await Invoice.findById(req.params.id);
  if (!invoice) return res.sendStatus(404);
  res.json(invoice);
});

It looks complete. There's auth middleware, a not-found case, and clean async code. But requireAuth only proves the caller is some user. Any logged-in user can read any invoice by changing the number in the URL. If IDs are sequential, a short loop downloads all of them.

This is broken object-level authorization (BOLA, also called IDOR), number one on the OWASP API Security Top 10.

USER A token: A GET /invoices/1044 requireAuth valid token? yes owner check? none 200 OK invoice 1044 owned by user B Authentication answers "who are you?" Authorization answers "is this yours?" and that's the part that's missing.

Why models get this wrong so often

  • Tutorials show auth once and move on. Most examples add a login middleware at the router level and then write simple findById handlers. That's the pattern the model learned.
  • Ownership depends on your data model. Whether an invoice belongs to a user, an organisation or a team lives in another file the model may not have in context. Authentication is generic, authorization is specific, and models are much better at generic.
  • Nothing fails. Your own tests log in as one user and fetch that user's records. They pass. The bug only shows up when a second user tries someone else's ID, and few test suites do that.

Same bug, three stacks

FastAPI

api/invoices.pyPython
@router.get("/invoices/{invoice_id}")
def get_invoice(invoice_id: int, user=Depends(current_user), db=Depends(get_db)):
    return db.get(Invoice, invoice_id)    # user is loaded, then ignored

Supabase from the browser

lib/invoices.tsTypeScript
const { data } = await supabase
  .from("invoices").select("*").eq("id", id);
// safe ONLY if row-level security is on with an owner policy.
// With RLS off, the public anon key reads every invoice.

The fix, in all three

Scope every query by the owner, so the database can't return someone else's row even if the ID is right:

routes/invoices.jsExpress
const invoice = await Invoice.findOne({
  _id: req.params.id,
  orgId: req.user.orgId,
});

In FastAPI, add Invoice.org_id == user.org_id to the query. In Supabase, enable RLS and add a policy like using (org_id = auth.jwt() -> 'org_id'). Random UUIDs make IDs harder to guess, but they slow attackers down rather than stop them. IDs leak in URLs, logs and emails.

How to test for it

  • Two-user testing. The reliable way to find BOLA is to log in as user A and request user B's objects. AquilaX's API Security and DAST scanners do this automatically and report a finding only when the response actually contains data that isn't A's.
  • SAST for handlers that look up by a request parameter alone. A data access keyed only on req.params.id inside an authenticated route is a strong signal worth a human look.
  • A rule for your assistant: "every query on tenant data must filter by the current user or organisation". It helps. Two-user testing is what proves it.

Quick self-check: pick five read endpoints in your API, call each with a second account's token and the first account's IDs. If any returns 200 with data, you've found one.

Find BOLA before attackers do

AquilaX tests every object ID with two accounts and reports only confirmed access, with the request and response as proof.

See API Security scanning →