This document explains how CSRF (Cross-Site Request Forgery) protection is configured in TimeTracker when running in Docker containers.
TimeTracker uses Flask-WTF's CSRFProtect extension to protect against CSRF attacks. CSRF tokens are cryptographic tokens that ensure forms are submitted by legitimate users from your application, not from malicious third-party sites.
- When a user visits a page with a form, the server generates a unique CSRF token
- This token is embedded in the form (usually as a hidden field)
- When the form is submitted, the token is sent back to the server
- The server validates the token matches what was originally generated
- If the token is invalid or missing, the request is rejected with a 400 error
CSRF tokens are signed using the Flask SECRET_KEY. This means:
- ✅ The same
SECRET_KEYmust be used across container restarts - ✅ The same
SECRET_KEYmust be used if you run multiple app replicas ⚠️ IfSECRET_KEYchanges, all existing CSRF tokens become invalid⚠️ Users will get CSRF errors on form submissions if the key changes
Generate a cryptographically secure random key:
python -c "import secrets; print(secrets.token_hex(32))"Create a .env file (do not commit this to git):
SECRET_KEY=your-generated-key-hereThen run docker-compose:
docker-compose up -dexport SECRET_KEY="your-generated-key-here"
docker-compose up -dFor production deployments with Docker Swarm or Kubernetes, use secrets management:
secrets:
secret_key:
external: true
services:
app:
secrets:
- secret_key
environment:
- SECRET_KEY_FILE=/run/secrets/secret_keyControls whether CSRF protection is enabled.
- Default in Production:
true - Default in Development:
false(for easier testing) - Recommended: Keep enabled in production
Set in docker-compose:
environment:
- WTF_CSRF_ENABLED=trueTime in seconds before a CSRF token expires.
- Default:
3600(1 hour) - Range: Set to
nullfor no expiration, or any positive integer
Set in docker-compose:
environment:
- WTF_CSRF_TIME_LIMIT=3600environment:
# CSRF enabled by default for security testing
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-true}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}
- SECRET_KEY=${SECRET_KEY:-your-secret-key-change-this}environment:
# CSRF always enabled in production
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-true}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}
- SECRET_KEY=${SECRET_KEY:-your-secret-key-change-this}Important: The app will refuse to start in production mode with the default SECRET_KEY.
environment:
# CSRF can be disabled for local testing
- WTF_CSRF_ENABLED=${WTF_CSRF_ENABLED:-false}
- WTF_CSRF_TIME_LIMIT=${WTF_CSRF_TIME_LIMIT:-3600}
- SECRET_KEY=${SECRET_KEY:-local-test-secret-key}Look at the application logs when starting:
docker-compose logs app | grep -i csrf- Open your browser's developer tools
- Navigate to a form in TimeTracker
- Look for a hidden input field:
<input type="hidden" name="csrf_token" value="..."> - Try submitting the form without the token (should fail with 400 error)
Cause: One of the following:
SECRET_KEYchanged between token generation and validation- Token expired (check
WTF_CSRF_TIME_LIMIT) - Clock skew between server and client
- Browser cookies disabled or blocked
Solution:
- Check
SECRET_KEYis consistent - Verify
WTF_CSRF_ENABLED=true - Ensure cookies are enabled
- Check system time is synchronized
Cause: Missing or misconfigured SECRET_KEY
Solution:
- Set a proper
SECRET_KEYin your.envfile - Verify the environment variable is passed to the container:
docker-compose exec app env | grep SECRET_KEY
Cause: WTF_CSRF_TIME_LIMIT too short
Solution: Increase the time limit or disable expiration:
environment:
- WTF_CSRF_TIME_LIMIT=7200 # 2 hoursThe /api/* endpoints are exempted from CSRF protection because they use JSON and are designed for programmatic access. They rely on other authentication mechanisms instead.
- ✅ Always use a strong SECRET_KEY in production
- ✅ Keep SECRET_KEY secret - never commit to version control
- ✅ Use the same SECRET_KEY across all app replicas
- ✅ Enable CSRF protection in production (
WTF_CSRF_ENABLED=true) - ✅ Use HTTPS in production for secure cookie transmission
- ✅ Set appropriate cookie security flags:
SESSION_COOKIE_SECURE=true(HTTPS only)SESSION_COOKIE_HTTPONLY=true(no JavaScript access)SESSION_COOKIE_SAMESITE=Lax(CSRF defense)
For CSRF tokens to work correctly in Docker:
- Set a strong SECRET_KEY and keep it consistent
- Enable CSRF protection with
WTF_CSRF_ENABLED=true - Configure timeout appropriately with
WTF_CSRF_TIME_LIMIT - Use HTTPS in production with secure cookie flags
- Never change SECRET_KEY without understanding the impact
All docker-compose files have been updated with these settings and include helpful comments.