Defence in depth
Defence in depth (American spelling defense in depth, also called layered defence) is the security principle of protecting a system with several separate controls, so that an attack which gets past one control can still be stopped or contained by another.1 The aim is to remove or reduce "single points of complete compromise".1 The industrial standard ISA/IEC 62443 defines it as countermeasures applied in layers, with different kinds of technology placed on the common paths of attack so that each catches attacks another lets through.2
How it works
Every control can fail: a check is missing from one route, a setting is wrong, a key leaks, a framework has a vulnerability. Defence in depth assumes one of them will, and puts a second control behind it that does not depend on the first. People and operations can form barriers as well as technology.2 Physical, network, application and data security each count as a layer, and so does detecting and responding to incidents, which limits the damage once an attack has succeeded.3
Layers protect each other only when they fail for different reasons. When one layer proves inadequate, another may prevent a full breach "if diverse defensive strategies are in place".1 A second copy of the same check adds little, because a flaw in the first is also a flaw in the copy. Secure design guidance lists the principle beside Principle of least privilege, zero trust and secure defaults.3
| Layer | Control |
|---|---|
| Request filter | Next.js Proxy reads the session cookie and redirects signed-out visitors (an optimistic check) |
| Server code | A data access layer verifies the session before every read; Server Actions and Route Handlers check the user as public API endpoints would |
| Database grants | Postgres decides whether a role may run an operation on a table at all |
| Row level security | Policies decide which rows that operation applies to |
| Keys | The key that bypasses row level security stays on the server |
Example: CVE-2025-29927
Next.js middleware runs before routing and caching, and some applications used it to check for a sign-in cookie and then redirect the visitor or block the route.4 To stop middleware from calling itself in a loop, Next.js marked its own internal requests with an undocumented header, x-middleware-subrequest, which told the router that middleware had already run.4 The GitHub advisory published on 21 March 2025 stated that authorization checks could be bypassed "if the authorization check occurs in middleware", and rated the flaw 9.1 (Critical) in CVSS 3.1 under CWE-285, Improper Authorization.5 Its workaround for versions that could not be updated was to block outside requests carrying that header before they reached the application.5
Fixed versions were 15.2.3, 14.2.25, 13.5.9 and 12.3.5. Self-hosted applications started with next start or built with output: 'standalone' were affected; Vercel, Netlify and Cloudflare Workers were not, because their routing runs in a separate system.4 The bypass removed one layer. The advisory limits it to checks made in middleware, so an application whose only check was there exposed its protected pages, while one that verified the session again in server code or in the database still refused the request. Vercel's postmortem states that it does not recommend middleware as the sole protection for routes.4 The same flaw appears in Authorization as an access control failure.
Applying it in Next.js and Supabase
Next.js documents two kinds of check. An optimistic check reads the session from the cookie, which is enough for a redirect or for hiding part of the interface. A secure check compares the session with the database and guards sensitive data and actions.6 Proxy is an optional first filter that, in the framework's own words, "should not be your only line of defense". Most checks belong as close as possible to the data source, in a data access layer that every data request passes through:6
// app/lib/dal.ts, from the Next.js authentication guide
import 'server-only'
import { cache } from 'react'
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { decrypt } from '@/app/lib/session'
export const verifySession = cache(async () => {
const cookie = (await cookies()).get('session')?.value
const session = await decrypt(cookie)
if (!session?.userId) {
redirect('/login')
}
return { isAuth: true, userId: session.userId }
})A function that fetches data calls verifySession() first, so a developer adding a new page cannot forget the check. A check placed in a layout does not run again on navigation, and Server Actions and Route Handlers need the same care as public API endpoints.6
In Supabase, row level security is the database layer. Because it is a Postgres feature, it protects rows even when the data is reached through third-party tools, and Supabase's documentation calls it defence in depth for that reason.7 Postgres applies two checks of its own: grants decide whether a role may run an operation on a table, and policies decide which rows it applies to. Adding policies does not withdraw grants. Views bypass row level security by default, because they are usually created by the postgres user, and the service role bypasses it too, so its key stays on the server.7 See Row level security for writing policies.
Layers in the infrastructure
AWS added version 2 of the EC2 Instance Metadata Service (IMDSv2) as further layers inside a server, against server-side request forgery, misconfigured open web application firewalls and open reverse proxies. A session starts with an HTTP PUT request, which most open firewalls and proxies do not pass on; no token is issued to a request carrying an X-Forwarded-For header; and the packet holding the token is sent with a time-to-live of 1, so it cannot travel beyond the instance.8 The measures sit beside restricted IAM roles and local firewall rules, and the time-to-live limit keeps the token on the instance even after an attacker has got past every other defence.8
In SecFlash
SecFlash reports a missing database layer on the Cloud page: SF-CLD-501 for a public table without row level security, SF-CLD-502 for a table whose policies exist while row level security is off, and SF-CLD-506 for a view that runs with its creator's rights and goes around the policies.
See also
- Principle of least privilege
- Authorization
- Authentication
- Row level security
- Attack surface
- Server-side request forgery
- Category: Security concepts
References
- a b c OWASP Foundation. "Security principles". OWASP Developer Guide.
- a b NIST. "defense-in-depth". Glossary. NIST Computer Security Resource Center.
- a b OWASP Foundation. "Secure Product Design Cheat Sheet". OWASP Cheat Sheet Series.
- a b c d Vercel (25 March 2025). "Postmortem on Next.js Middleware bypass". Next.js Blog.
- a b c "How to implement authentication in Next.js". Next.js Docs. Vercel.
- a b "Row Level Security". Supabase Docs. Supabase.
- a b "Add defense in depth against open firewalls, reverse proxies, and SSRF vulnerabilities with enhancements to the EC2 Instance Metadata Service". AWS Security Blog. AWS (19 November 2019).
External links
- defense-in-depth, NIST Glossary
- Authentication, Next.js documentation