🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Database / Supabase RLS

Changing the id in the URL shows other users' data (IDOR)

LovableBoltCursorReplitv0

Published

Weakness type: CWE-639 (Authorization Bypass Through User-Controlled Key), CWE-862 (Missing Authorization)

The error

I changed /orders/123 to /orders/124 (or ?id=, or a UUID copied from somewhere else) and saw another customer's order, invoice, profile or file / an API route like /api/invoices/[id] returns any record for any logged-in user / a hidden user_id field in a form or request body lets me edit someone else's row / delete works on rows I do not own / a security review or pentest reports IDOR, BOLA (OWASP API1:2023), CWE-639 or broken access control

Why it happens

An IDOR (insecure direct object reference) needs three things, as OWASP's IDOR Prevention Cheat Sheet puts it: an object (an order, invoice, profile, file), a reference to it (an id, UUID, slug or file name in the URL, query string or request body) and a missing check that the current user is allowed to access that object. AI builders make the third one easy to miss. The page loads the record with something like select * from invoices where id = params.id, and nothing asks whose invoice it is. In vibe-coded apps it usually shows up in four ways. 1) RLS is off, or a policy uses (true), so the Supabase API returns any row to anyone with the public key. 2) The browser query adds .eq('user_id', user.id), but that filter is not security: anyone can call the same API without it. 3) An API route, Server Action or Edge Function uses the service_role or sb_secret_ key, which bypasses RLS, and then looks the row up by the id the caller sent. 4) The server trusts a user_id sent by the client, or reads the user from supabase.auth.getSession() on the server, which Supabase says reads the cookie without revalidating it. Random UUIDs make ids harder to guess, but they leak in links, shares and logs, and OWASP says the access check is still required.

How to fix it

  1. Prove it with two accounts. Create User A and User B, and give each one at least one record (order, invoice, project, file). Log in as A, open one of B's records by pasting B's id into the URL, then try the same id in every API call in DevTools > Network (GET, PATCH, DELETE, exports and downloads). Also try it logged out. Any response with B's data, or a successful change, is an IDOR.
  2. List every place an id comes from the client: route params (/orders/[id]), query strings (?id=), request bodies (including hidden user_id or owner_id fields), file paths and download links. For each one, write down which table it reads or changes and which code runs the query (browser Supabase client, API route, Server Action, Edge Function).
  3. For queries that run in the browser, let RLS do the ownership check. Turn on RLS on every table in an exposed schema and add owner policies to the authenticated role, for example: create policy "Owners can read" on invoices for select to authenticated using ((select auth.uid()) = user_id). Add matching insert (with check), update (using and with check, so nobody can move a row to another user_id) and delete policies. Never add an 'or auth.uid() is null' branch: Supabase warns it returns every row to logged-out requests.
  4. Do not count a frontend filter as security. .eq('user_id', user.id) in browser code is fine for performance, but the caller can remove it and call the API with your public key. Only the RLS policy (or a server-side check) protects the row. Keep an index on user_id so the policy stays fast.
  5. Fix server code that bypasses RLS. Search API routes, Server Actions and Edge Functions for service_role, SUPABASE_SERVICE_ROLE_KEY or sb_secret_. If one of them looks up a record by an id from the request, either switch it to a client created with the publishable (or anon) key and the user's session so RLS applies, or keep the secret key and add the owner filter yourself: .eq('id', id).eq('user_id', userId). Supabase notes a secret key bypasses RLS only when no user access token is sent with the request.
  6. Get the user id from a verified token, never from the request. On the server, call supabase.auth.getClaims() (or getUser()) and use the sub claim as the user id. Supabase says never to trust getSession() in server code, because the session cookie can be forged. Ignore any user_id, owner_id or role field in the body or query string; set the owner on the server instead.
  7. Check every entry point, not just the page. Next.js says a page-level auth check does not extend to the Server Actions defined in it, and that you must also check the user may act on that specific resource to prevent IDOR. Server Actions can be called with a direct POST request. Put the check inside each action, route handler and Edge Function, or in one server-only data access layer that every one of them uses.
  8. Return the same answer for 'not yours' and 'does not exist'. Scope the lookup to the current user (where id = ? and user_id = ?) and return 404 when nothing matches, so an attacker cannot tell which ids exist. Log repeated failures; OWASP suggests alerting on them.
  9. Cover shared data and files too. If records are shared with a team, check membership in the policy (a team_members table) instead of only user_id. For Supabase Storage, put files under a folder named after the user id and add storage.objects policies, or serve private files with short-lived signed URLs, so changing a file path in a link does not open someone else's file.
  10. Verify and keep it fixed. Repeat the two-account test for read, create, update, delete and export. Then add an automated test (or a Supabase policy test under supabase/tests) that signs in as User A and asserts that every request for User B's ids returns nothing or 404. OWASP recommends running such tests and not deploying changes that make them fail.

Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

Security fix: my app has an IDOR (broken object level authorization). Changing an id in the URL, query string or request body shows or changes another user's data. Fix it without breaking normal use. Rules: do not disable Row Level Security, do not add (true) policies or an 'auth.uid() is null' branch, do not move any service_role or sb_secret_ key into frontend code, and never trust a user_id, owner_id or role sent by the client. 1) List every page, API route, Server Action and Edge Function that takes an id from the client, with the table it reads or writes and whether it uses the browser client, a user-scoped server client or a service_role/secret key client. 2) For each table, make sure RLS is enabled and add owner policies to the authenticated role for select, insert (with check), update (using and with check) and delete using (select auth.uid()) = user_id, plus an index on user_id. Use a team membership check where records are shared. 3) On the server, get the user id from supabase.auth.getClaims() (not getSession()) and use a user-scoped client so RLS applies; where a secret key is really needed, add .eq('user_id', userId) to every lookup by id. 4) Return 404 when a record is not found or not owned. 5) Add the ownership check inside every Server Action and route handler, not only on the page. 6) Write a test that signs in as two users and proves User A cannot read, update, delete or export User B's records. Show me the SQL and code diffs and explain how to run the test.

Error codes this fixes

FAQ

What is an IDOR in a web app?

An insecure direct object reference: the app uses an id from the URL or request to load a record but never checks that the logged-in user may access it. Change /orders/123 to /orders/124 and you get someone else's order. OWASP calls the API version Broken Object Level Authorization (API1:2023).

Do random UUIDs stop IDOR?

No. They make ids harder to guess, but ids leak in shared links, emails, logs and screenshots. OWASP treats random ids only as defence in depth; the server or database must still check ownership on every request.

Is .eq('user_id', user.id) in my Supabase query enough?

Not in browser code. Anyone can call the Supabase API with your public key and leave the filter out. Use an RLS policy such as using ((select auth.uid()) = user_id), and keep the filter only for speed.

Why does my API route return any user's record even with RLS on?

It probably uses the service_role or sb_secret_ key, which bypasses RLS when no user token is sent. Use a client with the user's session so RLS applies, or add an owner filter based on the user id from getClaims().

Should I return 403 or 404 when a user requests someone else's record?

OWASP suggests that when existence is sensitive you scope the lookup to the current user and give the same answer, such as 404, for 'not found' and 'not allowed', so attackers cannot tell which ids exist.

How do I test my app for IDOR?

Create two accounts, give each its own data, log in as one and request the other's ids in every URL, API call, update, delete and export. Also try logged out. Then turn this into an automated test that runs before every deploy.

Sources

Based on the official documentation:

Related fixes

Want the full checklist for this area? Get a Fix Kit ($29)

Get one new fix a week

Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.

We only use your email to send the newsletter. Unsubscribe any time. See privacy.