| name | nextjs-react |
| description | This skill should be used when the user asks about "Next.js security", "React security", "Server Components", "Server Actions", "Route Handlers", "RSC vulnerabilities", "SSR security", or needs comprehensive Next.js/React security analysis during whitebox security review. |
Next.js/React Security Analysis
Comprehensive security patterns for Next.js and React applications, covering both client-side and server-side attack surfaces.
Architecture Overview
┌─────────────────────────────────────────────────────────────┐
│ Next.js Application │
├─────────────────────────────────────────────────────────────┤
│ │
│ Client-Side (Browser) Server-Side (Node.js) │
│ ┌──────────────────┐ ┌──────────────────────┐ │
│ │ React Components │ │ Server Components │ │
│ │ Client Actions │◄────────►│ Server Actions │ │
│ │ useEffect/State │ │ Route Handlers │ │
│ └──────────────────┘ │ Middleware │ │
│ │ getServerSideProps │ │
│ └──────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Attack Surface Categories
1. Server Actions ("use server")
- SSRF via redirect() with Host header manipulation
- Insecure direct object references
- Missing authentication/authorization
- SQL injection in database operations
2. Route Handlers (app/api/**/route.ts)
- Unauthenticated API endpoints
- Mass assignment vulnerabilities
- Rate limiting bypass
- CORS misconfiguration
3. Server Components
- Data exposure in serialized props
- Sensitive data in
__NEXT_DATA__
- Server-side XSS in rendered content
4. Middleware
- Path-based bypass (normalization)
- Authentication bypass
- Header injection
5. Client Components
- XSS via unsanitized rendering
- Prototype pollution
- Open redirects
Detection Workflow
Step 1: Map the Application
grep -rn '"use server"' --include="*.ts" --include="*.tsx"
find . -path "*/app/api/*" \( -name "route.ts" -o -name "route.js" \)
find . -name "middleware.ts" -o -name "middleware.js"
find ./app -name "page.tsx" -o -name "page.js"
Step 2: Identify Entry Points
grep -rn "action=" --include="*.tsx" | grep -v node_modules
grep -rn "startTransition\|useTransition" --include="*.tsx"
grep -rn "export.*function\s\+\(GET\|POST\|PUT\|DELETE\|PATCH\)" --include="route.ts"
Step 3: Check Authentication
grep -rn "getServerSession\|getSession\|auth\(\)" --include="*.ts" --include="*.tsx"
for f in $(find . -path "*/app/api/*" -name "route.ts"); do
if ! grep -q "getServerSession\|auth\|verify" "$f"; then
echo "Potentially unprotected: $f"
fi
done
Step 4: Trace Data Flow
grep -rn "request\.json\|request\.formData\|request\.text" --include="*.ts"
grep -rn "prisma\.\|db\.\|sql\`\|query\(" --include="*.ts"
grep -rn "fetch\(\|axios\.\|got\(" --include="*.ts"
Key Patterns
SSRF via Server Action Redirect
See framework-patterns/nextjs-patterns.md for detailed pattern.
Unprotected Route Handler
export async function DELETE(req: Request) {
const { id } = await req.json();
await db.user.delete({ where: { id } });
return Response.json({ success: true });
}
Server Component Data Leak
async function UserProfile({ userId }: { userId: string }) {
const user = await db.user.findUnique({ where: { id: userId } });
return <ClientProfile user={user} />;
}
Middleware Bypass
export const config = {
matcher: '/admin/:path*'
}
Integration with Chain Detection
Next.js vulnerabilities often enable chains:
| Next.js Vulnerability | Chains To |
|---|
| Server Action SSRF | Internal Flask/Django SSTI |
| Server Action SSRF | Cloud metadata (169.254.169.254) |
| Route Handler SQLi | Data exfiltration, auth bypass |
| Middleware Bypass | Admin panel access |
| Data Leak | Credential theft, session hijacking |
Remediation Checklist