Files
TimeTracker/docs/security/CSRF_DOCKER_CONFIGURATION_SUMMARY.md
Dries Peeters e4789cc26e feat: Add telemetry and analytics infrastructure with observability stack
Implement comprehensive analytics and monitoring system with PostHog integration,
complete observability stack (Prometheus, Grafana, Loki, Promtail), and CI/CD
workflows for automated builds.

Features:
- Add PostHog telemetry integration with privacy-focused event tracking
- Implement installation flow for opt-in telemetry configuration
- Add telemetry management UI in admin panel with detailed transparency
- Track key user events across all major features (projects, tasks, timer, etc.)

Infrastructure:
- Set up Prometheus for metrics collection
- Configure Grafana for visualization dashboards
- Integrate Loki and Promtail for log aggregation
- Add separate analytics docker-compose configuration

CI/CD:
- Add GitHub Actions workflows for building and publishing Docker images
- Implement separate dev and production build pipelines
- Configure automated image publishing to registry

Documentation:
- Restructure documentation into organized docs/ directory
- Add comprehensive guides for telemetry, analytics, and local development
- Create transparency documentation for tracked events
- Add CI/CD and build configuration guides

Code improvements:
- Integrate telemetry hooks across all route handlers
- Add feature flags and configuration management
- Refactor test suite for analytics functionality
- Clean up root directory by moving docs and removing test artifacts

Breaking changes:
- Requires new environment variables for PostHog configuration
- Docker compose setup now supports analytics stack

Changes: 73 files changed, 955 insertions(+), 14126 deletions(-)
2025-10-20 14:38:57 +02:00

9.3 KiB

CSRF Token Docker Configuration - Implementation Summary

Overview

Successfully configured CSRF token support across all Docker deployment configurations to ensure CSRF protection works reliably with built Docker images.

Changes Made

1. Troubleshooting Comments Added

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

2. Configuration Files Updated

app/config.py

  • Added environment variable support for WTF_CSRF_ENABLED
  • Added environment variable support for WTF_CSRF_TIME_LIMIT
  • Updated DevelopmentConfig to 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'

env.example

  • Fixed default WTF_CSRF_ENABLED=true for production
  • Added comprehensive documentation about SECRET_KEY importance
  • Added instructions for generating secure keys
  • Impact: New deployments will have correct CSRF defaults

2. Docker Compose Files Updated

All four docker-compose files have been updated with consistent CSRF configuration:

docker-compose.yml (Local Development with PostgreSQL)

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}

docker-compose.remote.yml (Production)

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}

docker-compose.remote-dev.yml (Remote Development)

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}

docker-compose.local-test.yml (Local Testing with SQLite)

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}

3. Documentation Created

docs/CSRF_CONFIGURATION.md

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

Key Improvements

1. SECRET_KEY Management

  • 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)

2. CSRF Protection

  • Enabled by default in production environments
  • Configurable via environment variables
  • Proper defaults for different deployment scenarios
  • Consistent across all docker-compose files

3. Developer Experience

  • Clear inline comments in docker-compose files
  • Comprehensive documentation for troubleshooting
  • Environment variable examples in env.example
  • Flexible configuration for different use cases

How CSRF Tokens Work in Docker

┌─────────────────────────────────────────────────────────┐
│ 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                       │
└─────────────────────────────────────────────────────────┘

Critical Requirements for CSRF Tokens

Same SECRET_KEY Must Be Used

  • Across container restarts
  • Across multiple app replicas/containers
  • Between token generation and validation

CSRF Protection Must Be Enabled

WTF_CSRF_ENABLED=true

Appropriate Timeout

WTF_CSRF_TIME_LIMIT=3600  # 1 hour default
SESSION_COOKIE_SECURE=true
REMEMBER_COOKIE_SECURE=true

Testing the Configuration

1. Verify Environment Variables

docker-compose exec app env | grep -E "(SECRET_KEY|CSRF)"

2. Check CSRF Token in Forms

# View any form in the browser developer tools
# Look for: <input type="hidden" name="csrf_token" value="...">

3. Test Form Submission

  • Submit a form normally → Should work
  • Remove csrf_token field → Should get 400 error

4. Verify Logs

docker-compose logs app | grep -i csrf

Production Deployment Checklist

  • Generate secure SECRET_KEY: python -c "import secrets; print(secrets.token_hex(32))"
  • Set SECRET_KEY in .env file 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

Backward Compatibility

All changes are backward compatible

  • Default values match previous behavior
  • Existing deployments continue to work
  • No breaking changes to API

Security Improvements

  1. CSRF enabled by default in production
  2. Clear documentation about SECRET_KEY importance
  3. Inline warnings in configuration files
  4. Consistent configuration across deployments
  5. Environment-based control for flexibility
  • app/__init__.py - CSRF initialization and error handling
  • app/config.py - Configuration classes
  • env.example - Environment variable examples with troubleshooting
  • docker-compose*.yml - Docker deployment configurations (with inline troubleshooting)
  • docs/CSRF_CONFIGURATION.md - Detailed documentation
  • CSRF_TROUBLESHOOTING.md - Quick troubleshooting reference
  • CSRF_TOKEN_FIX_SUMMARY.md - Original CSRF implementation
  • scripts/verify_csrf_config.sh - Automated configuration checker (Linux/Mac)
  • scripts/verify_csrf_config.bat - Automated configuration checker (Windows)

References

Conclusion

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.