Security Headers
Security headers are configured innext.config.ts and applied to all routes:
next.config.ts
Header Explanations
Controls DNS prefetching, allowing browsers to proactively resolve domain names for improved performance.
Prevents browsers from MIME-sniffing responses away from the declared content type, mitigating content-type confusion attacks.
Prevents the application from being embedded in frames from other origins, protecting against clickjacking attacks. Only allows framing from the same origin.
Controls how much referrer information is sent with requests. Sends full URL for same-origin requests, only origin for cross-origin HTTPS requests, and nothing for downgraded (HTTPS→HTTP) requests.
Disables browser features that the application doesn’t use, reducing attack surface by preventing access to camera, microphone, and geolocation APIs.
Disables the
X-Powered-By: Next.js header to avoid advertising the framework being used.Middleware Protection
The application uses Next.js middleware to protect routes and enforce authentication:src/middleware.ts
Middleware Behavior
The middleware implements the following security logic:Expired Token Handling
If the token is expired, deletes both
token and userData cookies and redirects unauthenticated users to login.Route Protection
Redirects unauthenticated users trying to access protected routes to the login page.
Public Routes
The following routes are publicly accessible without authentication:/auth/login- Login page/auth/sign-up- Registration page/auth/reset- Password reset page- Any route matching
/auth/*
Excluded Paths
The middleware matcher excludes:/api/*- API routes/_next/static/*- Static assets/_next/image/*- Image optimization/assets/*- Public assetsfavicon.icorobots.txtsitemap.xmlmanifest.webmanifest
JWT Token Management
Tokens are managed using thejose library:
src/libs/jwt.ts
Token Payload Structure
JWT tokens contain the following claims:Unique identifier for the authenticated user.
User’s email address.
User’s role name (e.g., “admin”, “operator”).
Unique identifier for the user’s role.
Array of permission strings the user has access to.
Organization identifier the user belongs to.
Token expiration timestamp (Unix epoch time).
Token issued-at timestamp (Unix epoch time).
Authentication Flow
MicroCBM implements a secure OTP-based authentication flow:OTP Verification
Backend sends a 6-digit OTP code to the user’s email. User must enter the OTP to complete login.
Cookie Storage
The token is stored in an HTTP-only cookie named
token, and user data is stored in a userData cookie.Image Security
Remote images are restricted to allowed domains:next.config.ts
- Only allows HTTPS images
- Restricts images to Cloudflare R2 storage domains
- Prevents loading images from untrusted sources
SEO Security
The application controls search engine indexing throughrobots.ts:
src/app/robots.ts
- Allows indexing of public authentication pages
- Disallows indexing of API routes and Next.js internals
- Prevents sensitive routes from appearing in search results
Content Security Policy (Optional)
For enhanced security, consider adding a Content Security Policy header:next.config.ts
HTTPS Enforcement
Always deploy to production with HTTPS enabled:- Most platforms (Vercel, Netlify, AWS Amplify) provide automatic HTTPS
- For custom deployments, use Let’s Encrypt or your certificate provider
- Configure your platform to redirect HTTP to HTTPS automatically
Security Checklist
Before deploying to production:Environment Variables
Verify all environment variables are set securely and not exposed in logs or error messages.
Security Headers
Test security headers using securityheaders.com.
Authentication
Test the complete authentication flow including token expiration and OTP verification.
Monitoring Security Events
Implement logging for security-relevant events:- Failed authentication attempts
- Token expiration and refresh events
- Unauthorized access attempts to protected routes
- API connection failures
- Changes to user roles and permissions
Security Updates
Keep dependencies up to date:Subscribe to security advisories for Next.js and your key dependencies to stay informed about critical updates.
Additional Security Measures
Rate Limiting
Implement rate limiting on the backend API to prevent brute force attacks.
Input Validation
All user input is validated using Zod schemas before processing.
XSS Protection
React automatically escapes output to prevent XSS attacks.
CSRF Protection
Use SameSite cookie attributes to mitigate CSRF attacks.
Next Steps
Deployment Overview
Learn about deployment platforms and strategies
Environment Variables
Configure required environment variables