Auth middleware protects all private routes
Why it matters
Unauthenticated access to private routes is OWASP A01 (Broken Access Control) — the top-ranked web application vulnerability. CWE-285 (Improper Authorization) and CWE-287 (Improper Authentication) both apply when any private page or API route is reachable without a valid session. In Next.js, a middleware file placed at an incorrect path is silently ignored by the framework — every route it was intended to protect is exposed, with no error and no indication anything is wrong.
Severity rationale
Critical because exposed private routes allow unauthenticated access to user data, admin functions, or financial records with no credentials required.
Remediation
Place middleware.ts at src/middleware.ts or the project root — not in subdirectories. Ensure the matcher covers every private prefix, and validate auth in individual API routes as defense in depth:
// src/middleware.ts (or root middleware.ts) — correct location
export const config = {
matcher: ['/dashboard/:path*', '/settings/:path*', '/api/:path*']
}
export async function middleware(request: NextRequest) {
const session = await getSession(request)
if (!session) return NextResponse.redirect(new URL('/login', request.url))
}
// Also validate in each API route:
export async function GET(request: Request) {
const session = await getSession(request)
if (!session) return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
Do not list private routes and assume middleware will catch all gaps — enumerate every API route that returns user-specific data and confirm each has an independent auth check.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Authentication Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-285 — Improper Authorization
- cwe · CWE-287 — Improper Authentication
- owasp:2021 · A01
- soc2:2017 · CC6.1 — Logical access security software, infrastructure, and architectures
Taxons
History
- 2026-04-18·v1.0.0·Initial import from saas-authentication·automated
- 2026-07-06·v1.0.1·Add SOC 2 TSC mapping (CC6.1) from the verified compliance crosswalk register (docs/compliance/).·by soc2-mapping-enrichment-ship-2