1773ed5f95
Mirror to GitHub / mirror (push) Successful in 26s
Test and Lint / backend-test (push) Successful in 1m11s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m28s
Version and Release / version-bump (push) Successful in 32s
Version and Release / trigger-drone (push) Has been skipped
Original: feat: enhance security logging and ensure rate limit blocks are properly tracked - Add comprehensive logging for rate limit blocks with full request details - IP address (with proper proxy detection), user agent, headers, timestamps - Rate limit info (current count, limit, remaining, reset time) - Separate tracking for auth vs general endpoints - Enhance authentication failure logging - JWT validation failures with detailed error info - Admin auth attempts without token - Failed token validation with user context - All events include IP, path, method, user agent - Improve Winston logger configuration for production - Add automatic log rotation (10MB errors, 50MB combined) - Create separate security.log for auth/rate limit events - Ensure logs directory exists automatically - Add structured JSON format for log aggregation - Support container logging with LOG_TO_CONSOLE env var - Create comprehensive documentation - Security logging guide with examples - Monitoring recommendations - Configuration reference - Add test script to verify logging functionality All rate limit settings remain configurable via admin panel: - Window duration, max requests, auth limits - Skip authenticated requests option - Public endpoints only option 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
3.5 KiB
3.5 KiB
JWT_SECRET Security Fix - Migration Guide
Overview
A critical security vulnerability has been fixed where the application would fall back to a hardcoded JWT secret ('your-secret-key') if the JWT_SECRET environment variable was not set. This has been addressed by:
- Adding startup validation that requires
JWT_SECRETto be set - Removing all hardcoded fallback values
- Ensuring the secret meets minimum security requirements
Changes Made
1. Added Environment Validation (backend/src/config/validateEnv.js)
- The server now validates critical environment variables at startup
- If
JWT_SECRETis missing or set to the insecure default, the server will refuse to start - Warns if
JWT_SECRETis less than 32 characters (recommended minimum)
2. Updated Server Startup (backend/server.js)
- Added validation call immediately after loading environment variables
- Ensures all routes and middleware have access to validated configuration
3. Removed Hardcoded Fallbacks (backend/src/routes/protectedImages.js)
- Removed
|| 'your-secret-key'fallback from lines 15 and 27 - Functions now rely on the validated
JWT_SECRETfrom environment
Migration Steps for Production
Before Deployment
-
Verify JWT_SECRET is set in production:
# Check if JWT_SECRET is set echo $JWT_SECRET -
Ensure JWT_SECRET is secure:
- Must NOT be
'your-secret-key' - Should be at least 32 characters long
- Should be randomly generated
- Must NOT be
-
Generate a secure JWT_SECRET if needed:
# Generate a secure 64-character secret openssl rand -hex 32
Deployment Process
-
Update environment variables (if needed):
# Example for .env file JWT_SECRET=your-secure-64-character-random-string-here -
Deploy the updated code
-
Monitor startup logs to ensure no validation errors:
✓ Environment validation passed ✓ Server running on port 3000
Rollback Plan
If the deployment fails due to missing JWT_SECRET:
-
Quick Fix (temporary):
- Set
JWT_SECRETenvironment variable to a secure value - Restart the application
- Set
-
Full Rollback (if needed):
- Revert to previous version
- Set
JWT_SECRETproperly before attempting deployment again
Verification
After deployment, verify the fix is working:
- Check server logs for successful startup
- Test authentication to ensure JWT tokens are working
- Verify image protection routes are functioning
Security Considerations
- Never commit JWT_SECRET to version control
- Rotate JWT_SECRET periodically
- Use different secrets for different environments (dev, staging, production)
- Monitor for authentication failures that might indicate token issues
Troubleshooting
Server won't start
- Error: "Missing required environment variable: JWT_SECRET"
- Solution: Set the JWT_SECRET environment variable
JWT_SECRET rejection
- Error: "JWT_SECRET is set to the insecure default value"
- Solution: Change JWT_SECRET from 'your-secret-key' to a secure value
Authentication failures after deployment
- Cause: Existing tokens were signed with old secret
- Solution: Users will need to re-authenticate to get new tokens
Support
If you encounter issues during migration:
- Check the server logs for specific error messages
- Verify environment variables are properly set
- Ensure the JWT_SECRET value doesn't contain special characters that might need escaping