Next.js 16 proxy.ts: Where Authorization Goes Now

- Share:





3366 Members
Next.js 16 deprecates the middleware.ts file convention in favor of proxy.ts, and the exported function is now proxy. Proxy runs on the Node.js runtime. The new name matches how the docs describe this layer: a network boundary in front of the app, used for redirects, rewrites, and coarse request filtering. Your full permission model belongs somewhere else.
If you are migrating, the question is simple: where do permission checks live now? Short answer: use proxy.ts for coarse preflight checks, and enforce real authorization in the data access layer, Server Actions, and route handlers.
This post targets Next.js 16 and proxy.ts specifically. For general Next.js RBAC setup, see How to add RBAC in Next.js and Implementing authentication and authorization in Next.js.
From the upgrade guide and proxy docs:
middleware.ts to proxy.ts (or run npx @next/codemod@canary middleware-to-proxy .).middleware to proxy.Middleware were renamed (for example skipProxyUrlNormalize).// proxy.ts at the project root (or in src/ if your app uses src/)
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const session = request.cookies.get("session");
if (!session && request.nextUrl.pathname.startsWith("/app")) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/app/:path*"],
};
That is a coarse gate. It answers "is there a session cookie?" and says nothing about whether this user may delete invoice 889.
The Next.js docs and the OWASP Next.js Security Cheat Sheet point to the same split:
Two things make this matter in production.
First, matcher drift. A small change to config.matcher can silently stop covering a Server Function path. The framework docs warn about that class of miss.
Second, defense in depth. CVE-2025-29927 showed the risk of putting authorization only in middleware: on affected self-hosted Next.js versions, a crafted request header could skip middleware execution entirely. Upgrade to a patched version, and keep authorization checks in the data path so a proxy or matcher miss does not expose data.
A proxy check can confirm a session cookie exists and maybe read a JWT expiry. The authoritative check is a PDP decision right next to the database read or mutation, ideally from a locally deployed PDP when latency matters.
Treat every Server Action and route handler as a client-callable endpoint. Validate input, resolve the user, then check the resource.
IDOR example people still ship by accident:
"use server";
import { getSessionUser } from "@/lib/auth";
import { db } from "@/lib/db";
// Vulnerable: trusts the client-supplied invoice id after login only
export async function getInvoice(invoiceId: string) {
const user = await getSessionUser();
if (!user) throw new Error("Unauthorized");
return db.invoices.findUnique({ where: { id: invoiceId } });
}
A logged-in user from tenant B can still read tenant A's invoice if they guess the id. The code checked who the user is and never asked whether they may see this record.
Here is the fixed shape, using a shared, server-only can() helper. The PDP decision does not replace tenant-scoped database queries. Do both.
// lib/can.ts
import { Permit } from "permitio";
const permit = new Permit({
token: process.env.PERMIT_API_KEY!,
pdp: process.env.PERMIT_PDP_URL ?? "http://localhost:7766",
});
export async function can(
userId: string,
action: string,
resource: { type: string; key: string; tenant?: string },
) {
return permit.check(userId, action, resource);
}
"use server";
import { getSessionUser } from "@/lib/auth";
import { can } from "@/lib/can";
import { db } from "@/lib/db";
export async function getInvoice(invoiceId: string) {
const user = await getSessionUser();
if (!user) throw new Error("Unauthorized");
const allowed = await can(user.id, "read", {
type: "invoice",
key: invoiceId,
tenant: user.tenantId,
});
if (!allowed) throw new Error("Forbidden");
// Tenant scope in the query is required even after a PDP allow.
return db.invoices.findFirst({
where: { id: invoiceId, tenantId: user.tenantId },
});
}
Server Components that load protected data should call the same helper (or a data access module that always checks) before returning props. Client components only hide UI. See also Frontend authorization with Next.js and CASL for the UI half.
Prefer a locally deployed PDP beside the app so Server Actions avoid a remote SaaS call on every click. In Permit's hybrid model, an Edge PDP you run next to the app evaluates decisions locally, while OPAL keeps its policy and authorization data in sync with the control plane (see the PDP overview). When the PDP runs as a sidecar or on the same host, the decision path has no remote SaaS hop; actual latency still depends on your deployment and policy shape. On serverless platforms where a sidecar is awkward, point the SDK at a nearby Edge PDP or Nexus PDP service instead of skipping the check. The managed Cloud PDP also works for RBAC and ReBAC checks; today it doesn't evaluate ABAC yet (support is coming soon), so for attribute conditions use the Edge PDP or Nexus PDP.
If your Next.js 15 app stuffed role checks into middleware.ts, walk this list:
proxy.ts.route.ts handlercurl or a forged client while logged in as a low-privilege user.Does proxy replace authorization libraries?
No. It replaces the old middleware file convention. Your application code still asks a PDP or policy engine for a decision where data is read or mutated.
Can I keep Edge middleware?
Next.js 16 still allows the deprecated middleware file for Edge cases, with a removal planned later. Do not rename Edge-only middleware to proxy and assume it behaves the same.
Where should tenant isolation run?
In the same place as resource checks: data access and handlers. Proxy can route by hostname. It should not be the only tenant fence.
The proxy.ts rename makes the boundary easier to reason about. Use it to redirect anonymous traffic early, and make every protected data path check authorization before it returns or changes anything.

Co-Founder & VP of R&D at Permit.io