Admin API leaking in your JavaScript: why lazy loading doesn't help, and where to fix it
Updated · by the Bologu team
Sites built with AI often have a single API file, such as lib/api.ts, holding both the customer calls and the admin calls. Every page imports it, so the build ships the whole file to every visitor. Anyone who opens DevTools can read which back-office APIs exist, and which ones delete or refund.
// lib/api.ts: imported by every page, so the whole file ships to every visitor
export const getProducts = () => fetch("/api/products");
export const checkout = (cart) => fetch("/api/checkout", { method: "POST", ... });
export const admin = {
users: () => fetch("/api/admin/users"),
refund: (id) => fetch(`/api/admin/orders/${id}/refund`, { method: "POST" }),
deleteAll: () => fetch("/api/admin/purge", { method: "DELETE" }),
};We found this on our own site
Checking bologu.com the same way we check customer sites, we found that the home page, the pricing page and others shipped the full list of our admin API. Our admin area sits behind a separate login, so nobody could get in, but that list was a free map of our back office. We moved the admin code to a file only the admin pages use, and added a CI step that fails the build if those paths ever reach a public page again.
Why it matters
- Hiding buttons isn’t access control: OWASP describes a site whose admin check lives only in browser JavaScript; an attacker just sends the request to the admin URL directly. Access control works only on the server.
- An API list is where attacks start: finding admin pages and APIs is an early step in OWASP’s testing guide. If your JS spells them out, a person or an AI exploring your site doesn’t have to guess.
- If any admin API forgets its server-side check, it can be called right away, including the ones that delete data or refund money.
Why lazy loading doesn’t help
A common tip is to lazy-load the admin page (React.lazy or import()). That only moves the admin code into another file, which is still public, and whose name is written in the main file. We built a small Vite app to check:
// main JS file after "vite build", with the admin page lazy-loaded:
...import("./admin-BcREgPCl.js")...
// anyone can open https://your-site/assets/admin-BcREgPCl.js
// and read /api/admin/users, /api/admin/orders/refund ...Two things really hide it: serve the admin app from its own subdomain that requires login before its files load (e.g. behind Cloudflare Access), or, with the Next.js App Router, import admin code only under app/admin, so admin file names never appear on public pages.
Check it yourself in 2 minutes
- Open your home page in a private window, press F12 → Sources, then Ctrl+Shift+F and search for
/api/admin(or your admin path). - If it’s there, open one admin API URL in the same private window (not logged in). Expect 401, 403 or a login redirect. If data comes back, fix it now.
Fix it in this order
- Check permissions on the server for every admin endpoint: read the role from the session or database, never from a value the browser sends, and deny by default.
- Separate admin code from public pages as described above.
- Keep it from coming back: add a CI step after the build that fails if a public page’s JS contains
/api/admin.
Prompt to have your AI fix it
My public pages ship JavaScript that names my admin API (e.g. /api/admin/...).
1. List every admin endpoint and show me the line on the SERVER that checks the caller is an
admin (role read from the session or database, never from a value the browser sends).
Add the check where it is missing; deny by default.
2. Move the admin API calls out of the module that public pages import. Next.js App Router:
a module imported only under app/admin. React/Vite/Vue SPA: lazy-loading alone is NOT
enough (the chunk stays public); serve the admin app from its own subdomain behind login.
3. Add a CI step after the production build that fails if a JS file loaded by a public page
contains "/api/admin".
Show me every file you changed.Let Bologu check
The website scan (free) reads the JS your public pages load, lazy-loaded files included, and warns when it finds admin or internal APIs. If you only lazy-load the admin page, the finding stays, because the admin file can still be downloaded. The GitHub code scan helps find server-side APIs that forget their permission check.
Sources
Read next
- npm malware wave of 2026: why AI-built sites are at risk, and how to protect yours
- Next.js ships security patches almost monthly: which version you need now (Nov 2026)
- Lessons from Moltbook: a fully AI-built site that let anyone read and edit its database
- Leaked source maps: anyone can read your site's original code. How to check and close it
- Is Lovable safe? Security risks and how to check your app
- Vibe coding security checklist: 20 checks before you launch
- Supabase RLS explained: fixing “RLS Disabled in Public”
- API key leaked in your website: how to check, and what to do right away
- Forgotten subdomains: how strangers can take over pages on your domain
- Secure your Claude Code, Codex and Cursor settings: stop clicking "allow all"
