Last Updated: 07/02/2026 Purpose: Security reference for Next.js applications based on observed attack patterns and implemented defenses.
- Base64-encoded shell scripts
- Malware downloads from suspicious IPs:
94.154.35.154,193.142.147.209,91.200.220.168,5.181.2.123 - Mitigation: Input validation, Server Actions with zod schemas
- Cryptocurrency miners (
xmrig,c3pool) - Botnet binaries (
yamaha.x86_64,cARM,cX86) - System persistence (cron jobs, systemd services)
- Mitigation: Container hardening, read-only filesystem
- Netcat reverse shells to
193.142.147.209:12323 - Persistent backdoor attempts
- Mitigation: Network policies, container isolation
ReferenceError: returnNaN is not defined- Permission denied errors (
/dev/lrt,//lrt) - Mitigation: Strict CSP, input sanitization
Docker Configuration:
- Non-root user:
1001:1001(nextjs:nodejs) - Read-only filesystem:
read_only: true - Capability dropping:
cap_drop: ALL - No new privileges:
no-new-privileges:true - Resource limits: Memory (256M), CPU (0.25)
- Temporary filesystem:
/app/.next/cachewithnoexec,nosuid - Node security:
--no-warnings --disable-proto=delete - Telemetry disabled
Dockerfile:
- Multi-stage build (deps, builder, runner)
- Minimal Alpine-based production image
- Standalone output for reduced attack surface
- Proper file ownership (nextjs:nodejs)
Encryption Key:
NEXT_SERVER_ACTIONS_ENCRYPTION_KEYconfigured- Passed as build arg and environment variable
- AES-GCM encrypted for consistent encryption across deployments
- Prevents sensitive data exposure in closures
Input Validation:
- All forms use Server Actions with zod validation
- Schema-based validation for all user inputs
- Type-safe form handling with error messages
- Server-side validation prevents injection attacks
Strict CSP with Nonces:
- Implemented in middleware with cryptographically secure nonces
- Nonce generated per request using
crypto.randomUUID() - Removed
unsafe-inlineandunsafe-evalfrom script-src (production) - Removed
unsafe-inlinefrom style-src (production) 'strict-dynamic'enabled for script loading- Development mode allows
unsafe-evalfor debugging - Next.js automatically applies nonces to Script components
CSP Directives:
default-src 'self'
script-src 'self' 'nonce-{nonce}' 'strict-dynamic'
style-src 'self' 'nonce-{nonce}'
img-src 'self' blob: data: https:
font-src 'self' data:
connect-src 'self'
object-src 'none'
base-uri 'self'
form-action 'self'
frame-ancestors 'none'
upgrade-insecure-requests
Implementation:
- Nonce generated in middleware per request
- CSP header set in middleware (not in next.config.mjs)
- Nonce passed via
x-nonceheader for reference - Middleware matcher excludes prefetches and static assets
X-Frame-Options: DENY- Prevents clickjackingX-Content-Type-Options: nosniff- Prevents MIME sniffingReferrer-Policy: origin-when-cross-origin- Limits referrer leakageX-XSS-Protection: 1; mode=block- XSS protectionPermissions-Policy: geolocation=()- Restricts browser APIs
- Secure cookies:
secure: true,httpOnly: true - Path filtering for static files and API routes
- Locale detection and routing
Server Actions Implementation:
- All forms use Server Actions (
'use server') - Zod validation schemas for all inputs
- Type-safe error handling with
useActionState - Client-side and server-side validation
- Input sanitization (regex patterns, length limits)
- Email normalization (lowercase, trim)
Validation Features:
- Required field validation
- Email format validation
- Phone number format validation
- Character whitelisting (names, company)
- Length limits on all fields
- Enum validation for select fields
- Container hardening (non-root, read-only, capabilities)
- Server Actions encryption key
- Strict CSP (no unsafe-inline/unsafe-eval)
- HTTP security headers
- Secure cookies in middleware
- Form validation with zod
- Server Actions for all forms
- Input sanitization
- Always validate input with zod schemas
- Never trust client input
- Use encryption keys for closures with sensitive data
- Return clear error messages for validation failures
- Validate all user inputs (params, searchParams, form data)
- Use parameterized queries for database access
- Sanitize user-generated content
- Never execute commands with user input
- Avoid
unsafe-inlineandunsafe-eval - Use
'self'for same-origin resources - Regularly review and tighten CSP policies
- Always run as non-root user
- Use read-only filesystem when possible
- Drop all unnecessary capabilities
- Set resource limits