Production Security Architecture: HttpOnly Tokens, Throttling & CSRF Mitigation
A comprehensive guide to securing full-stack Next.js and NestJS applications using defense-in-depth, token family rotation, and rate limiting.
Security cannot be treated as an isolated plugin or an afterthought added just before launch. A single missing security header, an exposed JWT secret, or an unthrottled authentication endpoint can expose an entire user database or allow malicious attackers to hijack administrative sessions.
In this architecture guide, we review the layered defense-in-depth principles implemented across our NestJS API and Next.js frontend proxy.
1. Secure Authentication: HttpOnly Cookies vs LocalStorage
Storing JWT access tokens in browser localStorage is an anti-pattern. Any Cross-Site Scripting (XSS) vulnerability inside a third-party npm dependency can immediately read localStorage.getItem('token') and exfiltrate user credentials.
We implement double-cookie token rotation:
- Access Token (15-minute TTL): Stored in a
Secure,HttpOnly,SameSite=Laxcookie namedpf_access. - Refresh Token (7-day TTL): Stored in a
Secure,HttpOnly,Path=/api/authcookie namedpf_refresh. - Token Family Rotation: Whenever a refresh token is exchanged, a new token family hash is generated. If an old refresh token is reused, all active sessions for that user are revoked immediately (detecting token theft).
// Cookie configuration in NestJS Authentication Service
export function setAuthCookies(res: Response, access: string, refresh: string) {
const isProd = process.env.NODE_ENV === 'production';
res.cookie('pf_access', access, {
httpOnly: true,
secure: isProd,
sameSite: 'lax',
maxAge: 15 * 60 * 1000,
path: '/',
});
res.cookie('pf_refresh', refresh, {
httpOnly: true,
secure: isProd,
sameSite: 'lax',
maxAge: 7 * 24 * 60 * 60 * 1000,
path: '/api/auth',
});
}
2. Global Rate Limiting & Denial of Service Protection
To protect against credential stuffing, brute force login attempts, and API scraping, we implement multi-tier rate limiting using @nestjs/throttler.
// App Throttler Configuration
@Module({
imports: [
ThrottlerModule.forRoot([
{
name: 'short',
ttl: 1000,
limit: 10, // Max 10 requests per second
},
{
name: 'medium',
ttl: 60000,
limit: 300, // Max 300 requests per minute
},
]),
],
})
export class AppModule {}
Specific high-risk endpoints (such as /api/auth/login and password resets) apply specialized guards allowing a maximum of 5 attempts every 15 minutes before locking IP addresses.
3. Strict HTTP Security Headers
Every HTTP response dispatched by Next.js and NestJS carries industry-standard defensive headers:
Content-Security-Policy (CSP): Restricts script execution to trusted origins and nonces.X-Frame-Options: DENY: Prevents clickjacking attacks by blocking iframe embedding.X-Content-Type-Options: nosniff: Stops MIME-type sniffing.Referrer-Policy: strict-origin-when-cross-origin: Prevents leaking private query paths in HTTP Referer headers.
Building secure applications is an ongoing commitment to paranoia, automated validation, and continuous auditing.


