🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Auth

Admin page is hidden but not protected (anyone can open /admin or call admin APIs)

LovableBoltCursorReplitv0

Published

Weakness type: CWE-602 (Client-Side Enforcement of Server-Side Security), CWE-862 (Missing Authorization)

The error

A normal user (or a logged-out visitor) types https://your-app.com/admin and the admin dashboard loads / the Admin link is hidden in the menu but the page still opens by URL / the admin page shows a 'not authorized' message but DevTools > Network shows the full list of users, orders or emails was already downloaded / curl to an admin API route or Edge Function works without an admin login / a scanner or security review reports broken access control, force browsing or missing server-side authorization

Why it happens

When you ask Lovable, Bolt, Cursor, Replit or v0 for an admin area, the quick version usually checks the role in the browser: something like if (user.role !== 'admin') return null, a hidden menu item, or a redirect in a React component or route guard. That only changes what the screen shows. The page code, and any API it calls, are still reachable by anyone who types the URL, edits the JavaScript, or sends the request with curl. OWASP lists exactly this in A01:2025 Broken Access Control, the number one risk in its Top 10: access control is only effective in trusted server-side code, and an app that puts all of its access control in its front end can be bypassed by calling the admin URL from the command line. In a Supabase app the real gate is the database: if the admin page reads tables with the publishable key and Row Level Security lets any signed-in user (or anon) read every row, then hiding the page protects nothing, because the same data can be fetched directly. Next.js adds two traps. Its authentication guide says returning null in a layout is not recommended because nested routes and Server Actions have their own entry points, that Server Actions must be treated like public API endpoints with their own checks, and that middleware (called Proxy in newer versions) should not be your only line of defense; in March 2025 advisory GHSA-f82v-jwr5-mffw (CVE-2025-29927) showed authorization done only in middleware could be bypassed on self-hosted Next.js before 15.2.3, 14.2.25, 13.5.9 and 12.3.5 (Vercel-hosted deployments were automatically protected). The second trap is where the role comes from: Supabase warns that user_metadata can be changed by the signed-in user, and that the session returned by getSession() on the server should not be trusted for authorization decisions on its own.

How to fix it

  1. Prove it first. Open a private window, stay logged out, and go straight to /admin (and any other admin URLs such as /dashboard/admin or /admin/users). Then log in as a normal test user and try again. Watch DevTools > Network: if any request returns other users' rows, emails or orders, the data is exposed even if the screen says 'not authorized'.
  2. Store the admin role where users cannot change it. Use a separate table such as public.user_roles (user_id, role) that only you or a server-side process can write, or app_metadata set with the service role. Never decide admin rights from user_metadata, a localStorage flag, a query parameter, or a role column the user can update on their own profile row.
  3. Protect the data in the database, not the page. Create a security definer helper such as public.is_admin() that checks user_roles for (select auth.uid()), with set search_path = '', and use it in RLS policies: admins can read or change everything they need, normal users only their own rows, anon nothing. Revoke direct access to user_roles from anon and authenticated so nobody can grant themselves the role. Supabase's RBAC guide shows the same idea with a custom user_role claim added by a Custom Access Token Hook.
  4. Check the role on the server for every admin action. In each admin API route, Route Handler, Server Action or Supabase Edge Function, verify the caller's token on the server (Supabase recommends getClaims() or getUser(), not the user object from getSession()), look up the role, and return 401 if there is no valid session and 403 if the user is not an admin, before any database work. Do admin-only writes with the service role key only inside that server code, after the check, and never ship that key to the browser.
  5. Keep the frontend check, but only for comfort. Hiding the menu item and redirecting non-admins from /admin is fine for a clean UI, and Next.js calls this an optimistic check. It must never be the only check. In Next.js, do not rely on a layout that returns null or on middleware alone; put the check in a shared data access layer that every admin page, Route Handler and Server Action calls.
  6. If you self-host Next.js, make sure you are on a patched version (15.2.3, 14.2.25, 13.5.9, 12.3.5 or later) for the middleware bypass advisory, or block incoming requests that carry the x-middleware-subrequest header as the advisory suggests.
  7. Test it like an attacker, and keep the test. Repeat step 1 logged out and as a normal user, then call each admin API or Edge Function with curl using a normal user's token: every one must return 401 or 403 and no data. Add these as automated tests so a future AI edit cannot quietly remove the server-side check.

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

Security fix: my admin area is only protected in the frontend (hidden links, a React route guard or a client-side role check). Anyone can open /admin by URL or call the admin APIs directly. Fix it on the server and in the database without breaking the admin UI. Rules: do not disable RLS, do not use (true) policies, do not move the service_role or sb_secret_ key into frontend code, and never read the admin role from user_metadata, localStorage or a column the user can edit. 1) List every admin page, API route, Server Action and Supabase Edge Function, and every table they read or write. 2) Create public.user_roles (user_id uuid references auth.users, role text) if it does not exist, revoke all on it from anon and authenticated, and add a security definer function public.is_admin() with set search_path = '' that checks it for (select auth.uid()). 3) Update the RLS policies on the tables the admin area uses so admins get the access they need through is_admin(), normal users only see and change their own rows, and anon gets nothing. 4) In every admin API route, Server Action and Edge Function, verify the caller on the server (supabase.auth.getClaims() or getUser(), never the user object from getSession()), return 401 if not signed in and 403 if not an admin before doing any work. 5) Keep the frontend redirect and hidden links only as UI. 6) Give me curl commands and a short test plan proving that a logged-out visitor and a normal user get 401 or 403 and no rows from every admin endpoint and table, and that an admin still works. Explain each change in one line.

Error codes this fixes

FAQ

Is hiding the admin button or route enough to protect my admin page?

No. Hiding a link or redirecting in React only changes what the screen shows. Anyone can type the URL, change the JavaScript or call your API with curl. OWASP says access control is only effective in trusted server-side code. Check the admin role on the server and in your database rules on every request.

How do I check if my admin page is really protected?

Open a private window logged out and go to /admin, then repeat as a normal user. Watch DevTools > Network for any response that contains other users' data, and call each admin API or Edge Function with curl using a normal user's token. Every request should return 401 or 403 with no data.

Where should I store the admin role in Supabase?

In a separate table such as user_roles that normal users cannot write, or in app_metadata set from the server, optionally added to the JWT with a Custom Access Token Hook. Do not use user_metadata, because Supabase says the signed-in user can change it.

Is Next.js middleware enough to protect /admin?

Not on its own. The Next.js docs say middleware (Proxy) should not be your only line of defense and that Server Actions need their own checks. Advisory CVE-2025-29927 also showed middleware-only authorization could be bypassed on unpatched self-hosted versions. Check the role again in the data access layer, Route Handlers and Server Actions.

My admin page says 'not authorized' to normal users. Am I safe?

Only if the data never reaches them. Many AI-built admin pages download every user's data first and then hide it. If DevTools shows those rows in a response, Row Level Security or the API is letting the data out, and the message on the screen does not protect anything.

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.