Does Next.js middleware protect your API routes? The matcher that skips /api, proxy.ts and CVE-2025-29927
By Abbi Labs · Last updated 4 December 2026
Scan your app free and see what your live site exposes.
Abbi Labs checks common admin URLs reachable without signing in, secret keys in your JavaScript, Supabase tables readable with your public key, served /.env or /.git, source maps, security headers, CORS, cookie flags and TLS. Read-only. No sign-up for the quick check.
No app handy?
Example result · our deliberately broken demo app
6 issues found: 3 critical, 1 high, 1 medium, 1 low
- Critical: Stripe live secret key in public JavaScript
- Critical: /.env file readable by anyone
- High: Git repository exposed (/.git/HEAD)
The demo's problems are planted on purpose. Paste your own URL above to see yours.
No sign-up. Read-only public-resource and protocol metadata checks. No model prompts or tool execution. Scan only sites you own or are allowed to test.
Run on every deploy or use from Cursor / Claude Code via MCP →
Weekly AI-app security checklist
One short email a week: a practical check for apps built with Supabase, Next.js and AI tools (RLS, leaked keys, headers and more). Unsubscribe anytime.
You asked your AI tool to "protect the dashboard", and it wrote a middleware.ts that sends signed-out visitors to /login. You tried it in a private window, got redirected, and moved on. The pages are covered. Whether your /api routes and Server Actions are covered is a different question, and often the answer is no.
Sources for the facts below: Next.js docs on Proxy (formerly middleware) and its matcher, the authentication guide, the data security guide, and the CVE-2025-29927 advisory.
Why middleware often doesn't cover /api
- The matcher decides where it runs. Middleware only runs on paths its
config.matchermatches. A very common matcher is'/((?!api|_next/static|_next/image|.*\.png$).*)'. The(?!apipart is a "don't run here" rule for every path starting with/api. It appears in Next.js's own auth guide example (with the comment "Routes Proxy should not run on"), so AI tools reproduce it a lot. - Narrow matchers miss things too.
matcher: ['/dashboard/:path*']protects dashboard pages and nothing else. If the dashboard loads data from/api/projects, that route never sees the middleware. - Server Actions are their own door. Next.js's data security guide says an exported Server Action is reachable by a direct POST request, and a page-level auth check doesn't extend to the actions defined in it.
- Middleware can be skipped by bugs. CVE-2025-29927 (March 2025) let a request with the
x-middleware-subrequestheader skip middleware on unpatched self-hosted apps. Fixed in 12.3.5, 13.5.9, 14.2.25 and 15.2.3; Vercel-hosted apps were protected automatically, per the advisory. - Next.js says so itself. The authentication guide: Proxy "should not be your only line of defense in protecting your data". In Next.js 16,
middleware.tsis renamedproxy.ts, and the same advice applies.
What can go wrong
- An
/api/usersor/api/ordersroute that returns everyone's rows to anyone who asks. - A route using a Supabase service_role or secret key client, which bypasses RLS, so the database won't save you.
- An AI chat or image route that calls a paid API, open to anyone with curl.
- A Server Action like
deleteProject(id)orupdatePlan()that trusts whatever it's sent.
Check yours in 3 minutes
- Find the file.
middleware.tsorproxy.tsin the project root orsrc/. Readexport const config = { matcher }. If it contains(?!api, or lists only page paths, your API routes are not covered. - List your routes.
find app src/app pages -name 'route.ts' -o -name 'route.js' 2>/dev/nullfor App Router handlers, andpages/apifor older ones. Thengrep -rln "use server" app srcfor Server Actions. - Look for a check inside each one. Every handler and action that reads or changes user data should start by getting the user (for example
auth(),getServerSession()orsupabase.auth.getUser()) and returning 401 or 403 if there isn't one or it's the wrong user. - Test signed out, on your own app.
curl -i https://your-app.com/api/your-routewith no cookie, or open it in a private window. A 401 or 403 is good. Real data, or a 200 with JSON that should be private, means it's open. Try one POST route the same way with a harmless body if it only changes your own test data. - Check your Next.js version.
npm ls next. If you self-host on a version older than the fixed releases above, update.
The fix: check the user where the data is
Keep middleware for redirects and quick checks. Put the real check in each route handler and Server Action, ideally through one helper (Next.js calls this a Data Access Layer) so nobody forgets it:
// lib/auth.ts
import "server-only";
import { auth } from "@/auth"; // or your auth library / Supabase server client
export async function requireUser() {
const session = await auth();
if (!session?.user) throw new Response("Unauthorized", { status: 401 });
return session.user;
}
// app/api/projects/route.ts
import { requireUser } from "@/lib/auth";
export async function GET() {
try {
const user = await requireUser();
const projects = await db.project.findMany({ where: { ownerId: user.id } });
return Response.json(projects);
} catch (res) {
if (res instanceof Response) return res;
throw res;
}
}
// app/actions.ts
"use server";
import { requireUser } from "@/lib/auth";
export async function deleteProject(id: string) {
const user = await requireUser();
await db.project.deleteMany({ where: { id, ownerId: user.id } });
}- Filter by the user, not just "signed in".
where: { id, ownerId: user.id }stops one user reading or deleting another's rows by changing the id. - On Supabase, use the server client built from the request's cookies so RLS applies, and keep the service_role or secret key for code that has already checked the user and truly needs to bypass RLS. Use
supabase.auth.getUser()(orgetClaims()) on the server, which verifies the token, rather than trusting session data read from the cookie. - If you keep a broad matcher, that's fine for redirects. Just don't let it be the only thing between the internet and your data.
- Update Next.js and, if you can't, block incoming
x-middleware-subrequestheaders at your proxy, as the advisory suggests.
Paste this into your AI builder
Audit auth in this Next.js app. Don't change behaviour for signed-in users.
1. Show me config.matcher in middleware.ts / proxy.ts and list
which routes it does NOT run on (especially /api).
2. List every route handler (route.ts, pages/api) and every
"use server" function. For each, say whether it verifies the
session itself before reading or changing data.
3. Add a server-only requireUser() helper and call it at the top
of every handler and Server Action that touches user data.
Return 401 when signed out, 403 for the wrong user.
4. Make queries filter by the signed-in user's id, not an id
from the request alone.
5. Leave middleware for redirects only. Tell me the Next.js
version and whether it is older than 15.2.3 / 14.2.25.What Abbi Labs checks
Honest scope: Abbi Labs scans your live site from outside, signed out, so it can't read your middleware or prove every API route checks the user. Use the curl test above for that. What the free quick check (no sign-up) does here: it sends an anonymous GET to a short list of common admin paths (like /admin, /api/admin and /api/users) and flags responses that look like an admin panel or user list instead of a login redirect, and it sends OPTIONS preflights (never GET or POST) to /api routes found in your JavaScript to check CORS. It also flags secret keys in your home page JavaScript and Supabase tables readable with your public key, which is often what an open route would have exposed anyway.
FAQ
Does Next.js middleware run on API routes?
Only if its matcher includes them. With no matcher it runs on every request. But many projects copy a matcher like '/((?!api|_next/static|_next/image|.*\.png$).*)', which is the example in Next.js's authentication guide, and the (?!api ...) part means it never runs on anything under /api. Read config.matcher in your middleware.ts or proxy.ts to see which one you have.
Is middleware enough for authentication in Next.js?
No. Next.js's authentication guide says Proxy (the new name for middleware) can be useful for initial checks but should not be your only line of defense, and that most security checks should happen as close to your data as possible. It also says to treat Route Handlers and Server Actions like public-facing API endpoints and verify the user inside them.
What is proxy.ts? Is it different from middleware.ts?
Same feature, new name. In Next.js 16 the middleware file convention is deprecated and renamed to proxy (proxy.ts, exporting a proxy function), and Next.js provides a codemod to rename it. The matcher works the same way, so the same gap applies.
What was CVE-2025-29927?
A critical Next.js bug published in March 2025 where a request carrying the internal x-middleware-subrequest header could skip middleware, so auth checks done only in middleware could be bypassed. It's fixed in 12.3.5, 13.5.9, 14.2.25 and 15.2.3. The advisory says apps hosted on Vercel were automatically protected; self-hosted apps on older versions were exposed. Keep Next.js updated either way.
My page redirects to /login when I'm signed out. Doesn't that protect its Server Actions?
No. Next.js's data security guide says a page-level auth check doesn't extend to the Server Actions defined in it, and that actions are reachable by a direct POST request. Check the session inside each action.
Does this apply to Supabase apps?
Yes, if your Next.js API routes or Server Actions use a Supabase client with the service_role or secret key, because that bypasses RLS. If they only use the signed-in user's session with RLS on your tables, the database still blocks rows the user can't see. Supabase's Next.js middleware example mainly keeps the session cookie fresh (and can redirect signed-out visitors), so still check the user inside each route.
Can Abbi Labs tell me if my API routes are open?
Partly. The free quick check (no sign-up) sends an anonymous GET to a short list of common admin paths, such as /admin, /api/admin and /api/users, and flags responses that look like an admin panel or user list rather than a login redirect. It sends only OPTIONS preflights (for CORS) to other /api routes it finds in your JavaScript, so it can't tell whether those check the user. The curl test below can.