CCyberwiki

Insecure direct object reference

Insecure direct object reference
ClassAccess control
Also known asIDOR; Broken Object Level Authorization (BOLA); horizontal authorization
CWECWE-639: Authorization Bypass Through User-Controlled Key
OWASP Top 10A01:2025 Broken Access Control
OWASP API Top 10API1:2023 Broken Object Level Authorization
Typical severityHigh
AffectsAPI routes and server actions that load records by ID; Supabase tables without row level security

An insecure direct object reference (IDOR) is an access control vulnerability: the application loads a record by an identifier the client sent, such as an order number in a URL, and never checks that the requesting user may access that record.1 A user who changes the identifier can read or change records that belong to other users. The same weakness goes by Broken Object Level Authorization (BOLA), horizontal authorization and, in its formal name, Authorization Bypass Through User-Controlled Key.1

Description

Web applications identify records by keys: sequential numbers, UUIDs or short names. A route such as /api/orders/1041 takes the key from the URL and loads the matching row. Such a route needs two checks. Authentication settles who is making the request, and authorization whether that user may access this particular record. In an IDOR only the first one runs: the route confirms that the user is signed in, then returns whatever record the key names.

Unpredictable keys such as UUIDs make records harder to guess and leave the weakness in place, since keys turn up in URLs, shared links, logs and other API responses.

Broken Access Control, the category IDOR belongs to, heads the OWASP Top 10:2025 as A01,2 and the API Security Top 10 puts the weakness first on its own, as API1:2023 under the name BOLA.3

Mechanism

User Anna is signed in and opens her order at /api/orders/1041. The route runs SELECT * FROM orders WHERE id = 1041 and returns the row. When Anna requests /api/orders/1040, the route runs the same query with the new number and returns order 1040, which belongs to another user, Ben.

Browser (user Anna)Application routeDatabaseGET /api/orders/1040SELECT * FROM orders WHERE id = 1040row 1040 (owner: Ben)200 OK: Ben's orderThe route checks that a session exists,not that order 1040 belongs to Anna.
Figure 1. A request by user Anna for order 1040, handled by a route that looks the order up by its ID alone. The database returns the row, which belongs to another user.
Table 1. Responses of the same route without and with an ownership check.
RequestSigned in asNo ownership checkWith an ownership check
GET /api/orders/1041Anna, owner of 1041Anna's orderAnna's order
GET /api/orders/1040AnnaBen's order404 Not Found
PATCH /api/orders/1040AnnaBen's order, changed404 Not Found
GET /api/orders/1040NobodyBen's order, where the route also omits the sign-in check401 Unauthorized

Impact

An ordinary account in the application is enough. With sequential keys, a script can request every number in turn and collect every record of every user. If the route also accepts PATCH or DELETE, the attacker can change or remove other users' records, and a route that skips the sign-in check as well needs no account at all.

Detection

In the traffic of a single account every response looks valid, so a scan of one account misses the weakness. Test with two accounts: sign in as the first, open the browser's developer tools (F12), and on the Network tab copy the URL of a request that loads that account's data. In a private window, sign in as the second account and open the copied URL. If the first account's data is returned, the route lacks an ownership check.

Prevention

Ownership check in the query

Include the signed-in user's identifier in the query itself, so that a record owned by another user is never found:4

// app/api/orders/[id]/route.ts, Next.js with Prisma
export async function GET(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const user = await getUser(); // the application's sign-in check
  if (!user) return new Response("Unauthorized", { status: 401 });
  const order = await prisma.order.findFirst({ where: { id, userId: user.id } });
  if (!order) return new Response("Not found", { status: 404 });
  return Response.json(order);
}

Where the existence of a record should not be revealed, return the same response, 404 Not Found, whether the record does not exist or belongs to someone else.4

Row level security in Supabase

A Supabase frontend queries the database directly with the anon key (the publishable key in newer projects), which is included in the JavaScript sent to every browser. Row level security policies decide which rows that key can reach. Once row level security is on, no data is returned through the API until policies are created.5

Table 2. What a request with the anon key receives, by table setting.
Table settingResult for a request with the anon keySecFlash issue
Row level security offEvery row; rows can be added, changed and deletedSF-CLD-501
Policies written, row level security offEvery row: the policies have no effectSF-CLD-502
Row level security on, no policyNo rows, including for the application's own frontendSF-CLD-503
Row level security on, policy on user_idOnly the signed-in user's own rows

Enable row level security and add one policy for each permitted action. The following policy lets each user read only their own orders:5

alter table public.orders enable row level security;

create policy "Owners read their orders"
on public.orders for select
to authenticated
using ( (select auth.uid()) = user_id );

Requests made with the service role key bypass row level security.5 Keep that key on the server; see Privileged key in client-side code.

Prompt for a coding agent

Go through every API route, server action and database query in this project that reads or changes a record by an ID taken from the request. For each one, check that the query is also limited to records the signed-in user owns or may see, for example by adding the user's ID to the where clause. Where that check is missing, add it and return 404 when nothing matches. If the project uses Supabase, list every table in the public schema, say whether row level security is on, and write the missing policies using auth.uid(). Show me each change before you make it.

In SecFlash

With two test accounts configured, SecFlash reports SF-EXP-501 on the Exposure page when one account can read another account's data, with the URL and both responses. For Supabase projects it reports SF-CLD-501, SF-CLD-502 and SF-CLD-503 on the Cloud page, for the table settings in Table 2.

See also

References

  1. a b "CWE-639: Authorization Bypass Through User-Controlled Key". Common Weakness Enumeration. MITRE.
  2. back "A01:2025 Broken Access Control". OWASP Top 10:2025. OWASP Foundation.
  3. back "API1:2023 Broken Object Level Authorization". OWASP API Security Top 10. OWASP Foundation.
  4. a b "Insecure Direct Object Reference Prevention Cheat Sheet". OWASP Cheat Sheet Series.
  5. a b c "Row Level Security". Supabase Docs. Supabase.