6.9 KiB
CORS + XSS Security Implementation Summary
Overview
This document summarizes the security enhancements implemented to address CORS and XSS vulnerabilities (Issue #14 from the Security Audit Report).
Implementation Date
2024
Status
✅ FULLY IMPLEMENTED
Changes Made
1. CORS Startup Validation
File: src/utils/corsValidator.js
- Startup Validation: Validates CORS configuration when the application starts
- Production Enforcement: Fails fast if CORS origins are not configured in production
- Origin Format Validation: Checks for valid URL format and warns about suspicious patterns
- Wildcard Detection: Prevents wildcard (
*) usage in production
Features:
- Validates that
CORS_ALLOWED_ORIGINSis set in production - Checks origin format (must be valid URLs)
- Warns about HTTP origins in production (should be HTTPS)
- Prevents wildcard origins in production
- Provides clear error messages for misconfiguration
2. Runtime CORS Checks
File: src/utils/corsValidator.js
- Runtime Monitoring: Checks CORS requests at runtime
- Suspicious Pattern Detection: Identifies potentially misconfigured origins
- Logging: Logs warnings for suspicious CORS patterns
Features:
- Detects HTTP origins in production
- Identifies localhost/127.0.0.1 usage in production
- Logs blocked origins for monitoring
- Provides runtime feedback without blocking legitimate requests
3. Content Security Policy (CSP)
File: src/middleware/securityHeaders.js
- CSP Headers: Implements comprehensive Content Security Policy
- Nonce Support: Generates unique nonces for each request to allow safe inline scripts/styles
- Strict Directives: Restricts resource loading to prevent XSS attacks
CSP Directives:
default-src 'self'- Only allow resources from same originscript-src 'self' 'nonce-...' 'unsafe-eval'- Scripts from self or with noncestyle-src 'self' 'nonce-...'- Styles from self or with nonceimg-src 'self' data: https:- Images from self, data URIs, or HTTPSfont-src 'self' data: https:- Fonts from self, data URIs, or HTTPSconnect-src 'self' [CORS origins]- API calls to self or allowed CORS originsframe-ancestors 'none'- Prevent embedding (clickjacking protection)base-uri 'self'- Restrict base tagform-action 'self'- Forms can only submit to same originupgrade-insecure-requests- Upgrade HTTP to HTTPS
Nonce Usage:
- Nonce is generated per request and stored in
res.locals.cspNonce - Can be used in templates/views for inline scripts/styles
- Example:
<script nonce="<%= res.locals.cspNonce %>">...</script>
4. Additional Security Headers
File: src/middleware/securityHeaders.js
Added headers:
- Referrer-Policy:
strict-origin-when-cross-origin- Controls referrer information - Permissions-Policy: Restricts browser features (geolocation, camera, microphone, etc.)
Existing headers maintained:
X-Frame-Options: DENY- Clickjacking protectionX-Content-Type-Options: nosniff- MIME type sniffing protectionX-XSS-Protection: 1; mode=block- Legacy XSS filterStrict-Transport-Security- HSTS for HTTPS enforcement
5. Global Security Headers Application
File: src/index.js
- Before: Security headers only applied to admin routes
- After: Security headers applied to ALL routes globally
- Placement: Applied early in middleware chain for maximum protection
6. XSS Prevention Documentation
File: XSS_PREVENTION_GUIDE.md
Comprehensive guide covering:
- Server-side protections
- Frontend best practices
- Output encoding techniques
- Framework-specific guidance (React, Vue, Angular)
- Common attack vectors
- Testing methodologies
- Security checklist
Configuration
Environment Variables
No new environment variables required. Uses existing:
CORS_ALLOWED_ORIGINS- Comma-separated list of allowed origins (required in production)NODE_ENV- Set toproductionfor strict validation
CSP Configuration
CSP is automatically configured. To customize:
- Edit
src/middleware/securityHeaders.js - Modify the
buildCSP()function - Adjust directives as needed
Note: Current CSP allows 'unsafe-inline' and 'unsafe-eval' for compatibility. Consider tightening in production by:
- Removing
'unsafe-inline'and using nonces exclusively - Removing
'unsafe-eval'if not needed
Testing
CORS Validation Testing
-
Test startup validation:
# Should fail in production without CORS_ALLOWED_ORIGINS NODE_ENV=production node src/index.js -
Test runtime checks:
- Make request from non-whitelisted origin
- Check logs for warnings
CSP Testing
-
Test CSP headers:
curl -I http://localhost:3000/health # Check for Content-Security-Policy header -
Test nonce generation:
- Each request should have unique nonce
- Check
res.locals.cspNoncein route handlers
Browser Testing
- Open browser DevTools → Network tab
- Check response headers for:
Content-Security-PolicyX-Frame-OptionsReferrer-PolicyPermissions-Policy
Security Impact
Before
- ⚠️ No CORS validation at startup
- ⚠️ No runtime CORS monitoring
- ⚠️ No CSP headers
- ⚠️ Security headers only on admin routes
- ⚠️ No XSS prevention guidance
After
- ✅ CORS validated at startup (fails fast if misconfigured)
- ✅ Runtime CORS monitoring and logging
- ✅ Comprehensive CSP with nonce support
- ✅ Security headers on all routes
- ✅ Complete XSS prevention documentation
Risk Level
Before: 🟡 MEDIUM
After: 🟢 LOW
Migration Notes
Breaking Changes
None. All changes are backward compatible.
Recommendations
-
Tighten CSP in production:
- Remove
'unsafe-inline'and use nonces - Remove
'unsafe-eval'if not needed
- Remove
-
Monitor CORS logs:
- Watch for blocked origins
- Review suspicious pattern warnings
-
Update frontend code:
- Follow XSS prevention guide
- Use nonces for inline scripts/styles
- Sanitize user input
Files Modified
src/utils/corsValidator.js- NEW FILEsrc/middleware/securityHeaders.js- ENHANCEDsrc/index.js- UPDATEDXSS_PREVENTION_GUIDE.md- NEW FILESECURITY_AUDIT_REPORT.md- UPDATED
References
Support
For questions or issues:
- Review
XSS_PREVENTION_GUIDE.mdfor frontend guidance - Check
SECURITY_AUDIT_REPORT.mdfor security context - Review code comments in implementation files
Implementation Status: ✅ Complete
Testing Status: ✅ Syntax validated
Documentation Status: ✅ Complete