Successfully configured CSRF token support across all Docker deployment configurations to ensure CSRF protection works reliably with built Docker images.
All docker-compose files now include inline troubleshooting guidance:
- ✅ Step-by-step checklist for common CSRF issues
- ✅ Clear diagnostic steps
- ✅ Reference to detailed documentation
- ✅ Context-specific advice for each deployment type
New Troubleshooting Resources:
CSRF_TROUBLESHOOTING.md- Quick reference guide with solutions- Inline comments in all docker-compose*.yml files
- Extended troubleshooting section in env.example
- ✅ Added environment variable support for
WTF_CSRF_ENABLED - ✅ Added environment variable support for
WTF_CSRF_TIME_LIMIT - ✅ Updated
DevelopmentConfigto allow CSRF override via environment - Impact: CSRF settings can now be controlled via environment variables
# Base Config
WTF_CSRF_ENABLED = os.getenv('WTF_CSRF_ENABLED', 'true').lower() == 'true'
WTF_CSRF_TIME_LIMIT = int(os.getenv('WTF_CSRF_TIME_LIMIT', 3600))
# Development Config
WTF_CSRF_ENABLED = os.getenv('WTF_CSRF_ENABLED', 'false').lower() == 'true'- ✅ Fixed default
WTF_CSRF_ENABLED=truefor production - ✅ Added comprehensive documentation about SECRET_KEY importance
- ✅ Added instructions for generating secure keys
- Impact: New deployments will have correct CSRF defaults
All four docker-compose files have been updated with consistent CSRF configuration:
environment:
# IMPORTANT: Change SECRET_KEY in production! Used for sessions and CSRF tokens.
# Generate a secure key: python -c "import secrets; print(secrets.token_hex(32))"
- SECRET_KEY=${SECRET_KEY:-your-secret-key-change-this}
# CSRF Protection (enabled by default for security)
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-true}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}environment:
# IMPORTANT: Change SECRET_KEY in production! Used for sessions and CSRF tokens.
# Generate a secure key: python -c "import secrets; print(secrets.token_hex(32))"
# The app will refuse to start with the default key in production mode.
- SECRET_KEY=${SECRET_KEY:-your-secret-key-change-this}
# CSRF Protection (enabled by default for security)
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-true}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}environment:
# IMPORTANT: Change SECRET_KEY in production! Used for sessions and CSRF tokens.
# Generate a secure key: python -c "import secrets; print(secrets.token_hex(32))"
- SECRET_KEY=${SECRET_KEY:-your-secret-key-change-this}
# CSRF Protection (enabled by default for security)
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-true}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}environment:
- SECRET_KEY=${SECRET_KEY:-local-test-secret-key}
# CSRF Protection (can be disabled for local testing)
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-false}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}Comprehensive documentation covering:
- ✅ How CSRF tokens work
- ✅ SECRET_KEY importance and generation
- ✅ Environment variable configuration
- ✅ Docker deployment scenarios
- ✅ Troubleshooting common issues
- ✅ Security best practices
- ✅ API endpoint exemptions
- Clear warnings added to all docker-compose files
- Generation instructions provided inline
- Consistency requirement documented
- Production validation already exists in
app/__init__.py(app refuses to start with weak key)
- Enabled by default in production environments
- Configurable via environment variables
- Proper defaults for different deployment scenarios
- Consistent across all docker-compose files
- Clear inline comments in docker-compose files
- Comprehensive documentation for troubleshooting
- Environment variable examples in env.example
- Flexible configuration for different use cases
┌─────────────────────────────────────────────────────────┐
│ 1. User visits form page │
│ → App generates CSRF token using SECRET_KEY │
│ → Token embedded in form │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ 2. User submits form │
│ → Browser sends CSRF token with request │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ 3. App validates token │
│ → Verifies signature using same SECRET_KEY │
│ → Checks expiration (WTF_CSRF_TIME_LIMIT) │
│ → ✓ Valid → Process request │
│ → ✗ Invalid → Return 400 error │
└─────────────────────────────────────────────────────────┘
- Across container restarts
- Across multiple app replicas/containers
- Between token generation and validation
WTF_CSRF_ENABLED=trueWTF_CSRF_TIME_LIMIT=3600 # 1 hour defaultSESSION_COOKIE_SECURE=true
REMEMBER_COOKIE_SECURE=truedocker-compose exec app env | grep -E "(SECRET_KEY|CSRF)"# View any form in the browser developer tools
# Look for: <input type="hidden" name="csrf_token" value="...">- Submit a form normally → Should work
- Remove csrf_token field → Should get 400 error
docker-compose logs app | grep -i csrf- Generate secure SECRET_KEY:
python -c "import secrets; print(secrets.token_hex(32))" - Set SECRET_KEY in
.envfile or environment - Verify
WTF_CSRF_ENABLED=true(default) - Enable HTTPS and set
SESSION_COOKIE_SECURE=true - Set
REMEMBER_COOKIE_SECURE=true - Test form submissions after deployment
- Monitor logs for CSRF errors
✅ All changes are backward compatible
- Default values match previous behavior
- Existing deployments continue to work
- No breaking changes to API
- ✅ CSRF enabled by default in production
- ✅ Clear documentation about SECRET_KEY importance
- ✅ Inline warnings in configuration files
- ✅ Consistent configuration across deployments
- ✅ Environment-based control for flexibility
app/__init__.py- CSRF initialization and error handlingapp/config.py- Configuration classesenv.example- Environment variable examples with troubleshootingdocker-compose*.yml- Docker deployment configurations (with inline troubleshooting)docs/CSRF_CONFIGURATION.md- Detailed documentationCSRF_TROUBLESHOOTING.md- Quick troubleshooting referenceCSRF_TOKEN_FIX_SUMMARY.md- Original CSRF implementationscripts/verify_csrf_config.sh- Automated configuration checker (Linux/Mac)scripts/verify_csrf_config.bat- Automated configuration checker (Windows)
CSRF tokens are now properly configured for Docker deployments with:
- ✅ Clear documentation and warnings
- ✅ Proper defaults for all deployment scenarios
- ✅ Environment variable control
- ✅ Consistent configuration across all docker-compose files
- ✅ Security best practices enforced
The application will now properly validate CSRF tokens in Docker deployments as long as a consistent SECRET_KEY is maintained.