S — Superior
Free
Next.js Middleware Security Patterns
Implement security middleware in Next.js: CSP headers, CSRF protection, IP rate limiting, geo-blocking, bot detection, and API key validation. Production-ready patterns.
DA
Demo Author
Joined 5/22/2026
Views: 2,220Copies: 393Purchases: 0
You are a web security specialist. Create Next.js middleware security patterns: **1. Content Security Policy**: - Nonce-based CSP with Next.js - strict-dynamic vs allowlist - Report-only mode for testing - SRI (Subresource Integrity) for script tags **2. CSRF Protection**: - Double-submit cookie pattern - SameSite: Strict + CSRF token backup - Origin/Referer header validation - Per-form token vs session token **3. Rate Limiting**: - IP-based: sliding window with Redis - Route-based: different limits per endpoint - User-based: after authentication, per-user limits - Response headers: X-RateLimit-* standard - Challenge: captcha after rate limit exceeded **4. Geo-Blocking & Bot Detection**: - Vercel's request.geo for country-level blocking - User-Agent analysis (known bots, scripts) - Challenge suspicious requests (JS challenge via Cloudflare-like pattern) **5. API Key / Bearer Token Validation**: - Validate before passing to route handler - Constant-time comparison (prevent timing attacks) - Scope validation (read vs write tokens) **6. Security Headers**: - X-Frame-Options: DENY - X-Content-Type-Options: nosniff - Referrer-Policy: strict-origin-when-cross-origin - Permissions-Policy: camera/mic/geolocation restrictions - Strict-Transport-Security (HSTS) **Output**: Complete middleware.ts file with all patterns implemented and documented.
coding
security
nextjs
middleware
web
$0
Free — earn ad revenue by sharing
SPREAD THE VOID
Similar Prompts
S
FREE
Senior Engineer Code Review — OWASP + Performance
An expert-level code reviewer that analyzes for bugs, OWASP Top 10 vulnerabilities, performance bottlenecks, and provides line-by-line fix suggestions with code examples.
DA
1590304
S
$5
System Design Architect — From Zero to Scale
Design production-grade distributed systems with capacity planning, database selection, API contracts, and failure scenarios. Used by FAANG engineers for interview prep and real-world architecture.
DA
403924
FREE.PROMPT
S
FREE
Professional Code Review Assistant
An expert code reviewer that analyzes your code for bugs, performance issues, and best practices. Provides detailed, actionable feedback in a structured format.
DA
40