Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

Why is Proxy not enough for authorization?

Questions in this chapter (5)
  • Draft
  • hard
  • Askedvery often
  • Verified November 2026

Answer

Three reasons, each of which I could show on Next.js 16.4.5 (next build && next start).

Coverage is a path pattern. Proxy runs only where matcher says, but the things worth protecting aren't paths. A Server Action is a POST to whichever page renders it. With matcher: ['/dashboard/:path*'], an action used by /dashboard and also imported by the public home page ran with no cookie when posted to /, while the same call to /dashboard got a 307 to /login. The docs' own auth matcher excludes /api, so every route handler is uncovered unless it checks. The Proxy docs warn that moving an action or editing the matcher "can silently remove Proxy coverage".

It sees the request, not the resource. Proxy can tell that a session cookie exists and is validly signed. It can't tell whether user 7 may delete project 42; that needs the record. A database lookup there would run on every matched request, including each <Link> prefetch, so the docs limit Proxy to optimistic checks from the cookie.

It's a routing layer, and routing has bugs. CVE-2025-29927 (2025) let a request carrying the internal x-middleware-subrequest header skip middleware entirely. In 2026, advisories covered prefetch-segment .rsc URLs, injected route params and a Turbopack i18n case, each reaching protected content without the check.

So: keep Proxy for redirecting signed-out users and protecting static pages shared by all users (the case the docs give it). Put the real check in a data access layer: every Server Action, route handler and data function verifies the session and authorizes the specific resource. Then a matcher mistake or a routing bug costs a redirect, not data.

Example

// proxy.js
import { NextResponse } from 'next/server';
export function proxy(request) {
  if (!request.cookies.has('session')) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
}
export const config = { matcher: ['/dashboard/:path*'] };

// app/dashboard/actions.js
'use server';
export async function deleteProject(formData) {
  console.log('[action] deleteProject', formData.get('id'));
  return { deleted: formData.get('id') };
}

// app/page.jsx: public; shows a quick-delete form to signed-in users
import { cookies } from 'next/headers';
import { deleteProject } from './dashboard/actions';
export default async function Home() {
  const signedIn = (await cookies()).has('session');
  return signedIn && <form action={deleteProject}><input name="id" /><button>Delete</button></form>;
}

With no cookie, curl -X POST localhost:3000/dashboard -F '$ACTION_ID_<id>=' -F id=p1 got 307 to /login. The same command against / got 200, and the server logged [action] deleteProject p1. GET /api/projects, outside the matcher, returned the data too.

Pitfalls

  • Copying the docs' matcher /((?!api|_next/static|_next/image|.*\.png$).*) and assuming handlers are covered. /api is excluded by design.
  • Trusting a header Proxy sets, such as x-user. A route outside the matcher read admin from a client-sent x-user: admin; Next.js didn't strip it.
  • Treating "has a valid session cookie" as authorization. It says who, not what they may touch, and a revoked session stays valid until the cookie expires unless something checks the store.
  • Assuming upgrading closed the class of bug. Five Proxy or middleware bypass advisories were published between May and July 2026.

Follow-ups

Couldn't an attacker post any action id to any public page?
Not on 16.4.5. While only /dashboard used the action, a form-encoded POST / got 409 Server Action unavailable, and one with a Next-Action header was forwarded internally to /dashboard, re-entering Proxy; neither ran it. Once / imported the action, it ran. Coverage depends on where actions are imported.
Which versions fixed CVE-2025-29927?
Per the advisory: 15.2.3, 14.2.25, 13.5.9 and 12.3.5; the workaround was dropping x-middleware-subrequest at the edge. On 16.4.5, a request with x-middleware-subrequest: proxy:proxy:proxy:proxy:proxy was still redirected by Proxy.
Then why have Proxy auth at all?
UX and static pages. A signed-out user is redirected before any rendering, and a prerendered page shared by all users has no per-request code where a data-layer check could run.

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.