CSRF Protection

Configure CSRF (Cross-Site Request Forgery) protection and trusted proxies for PteroCA.

Overview

CSRF protection is a security measure that prevents unauthorized commands from being transmitted from a user that the application trusts. PteroCA uses Symfony's built-in CSRF protection.

When CSRF Configuration is Needed

You need to configure CSRF settings if:

  • Using Cloudflare or other CDN in front of your server

  • Behind a reverse proxy (NGINX proxy, load balancer, etc.)

  • Experiencing "Invalid CSRF token" errors

  • Need to disable CSRF for testing/development (not recommended for production)

CSRF with Cloudflare

If your server is behind Cloudflare, you may encounter "Invalid CSRF token" errors. This happens because Symfony needs to recognize Cloudflare as a trusted proxy.

Configure Trusted Proxies

Step 1: Get Cloudflare IP Ranges

Use the official list of Cloudflare IP ranges from Cloudflare Documentation.

Current Cloudflare IPv4 ranges (verify these periodically):

Step 2: Update .env File

Add the TRUSTED_PROXIES variable to your .env file:

Add this line with Cloudflare IP ranges:

For quick testing (less secure), you can use:

Warning: Using 0.0.0.0/0 trusts all IPs and should only be used for testing.

Step 3: Clear Cache

After updating .env, clear the Symfony cache:

Configure Trusted Hosts

To prevent Host header injection attacks, also configure trusted hosts:

Use regex to match your domains:

  • ^example\.com$ - Exact match for example.com

  • ^.*\.example\.com$ - Match all subdomains

  • Separate multiple patterns with |

Example with multiple domains:

Disabling CSRF Protection

For Development Only

In development or testing environments, you may want to disable CSRF protection temporarily:

After updating, clear cache:

Production Warning

Never disable CSRF in production! This creates serious security vulnerabilities:

  • Users vulnerable to CSRF attacks

  • Attackers can perform unauthorized actions

  • Compliance violations (PCI-DSS, GDPR, etc.)

Reverse Proxy Configuration

If using a reverse proxy (NGINX, Apache, HAProxy, etc.), you need to configure trusted proxies.

NGINX Reverse Proxy

If PteroCA is behind NGINX reverse proxy:

NGINX Configuration

PteroCA Configuration

Add proxy server IP to trusted proxies:

Load Balancer

For load balancers, add all load balancer IPs:

Troubleshooting

"Invalid CSRF Token" Errors

Symptom: Forms fail with "Invalid CSRF token" error

Solutions:

  1. Check trusted proxies configuration

  2. Verify Cloudflare IP ranges are current

    • Check https://www.cloudflare.com/ips/

    • Update .env if ranges changed

  3. Clear cache after changes

  4. Check session configuration

  5. Verify cookies are being set

    • Check browser developer tools → Application → Cookies

    • Look for session cookie

Sessions Not Persisting

Symptom: User logged out frequently, sessions don't persist

Solutions:

  1. Check session lifetime

  2. Verify session storage

    • Check var/sessions/ directory exists and is writable

    • Set proper permissions:

  3. Check cookie domain

Trusted Host Errors

Error: "Untrusted Host" or "Invalid Host header"

Solutions:

  1. Add domain to TRUSTED_HOSTS

  2. Escape dots in regex

    • Use \. not .

    • Example: example\.com not example.com

  3. Test regex pattern

Security Best Practices

Production Environment

  1. Never disable CSRF in production

  2. Use specific IP ranges for trusted proxies

  3. Keep Cloudflare IP ranges updated

  4. Configure trusted hosts

  5. Enable HTTPS only

  6. Monitor security logs

Regular Maintenance

  1. Update Cloudflare IPs quarterly

    • Cloudflare occasionally adds new IP ranges

    • Subscribe to Cloudflare announcements

  2. Audit trusted proxies

    • Review quarterly

    • Remove unused entries

    • Verify all IPs are still valid

  3. Test after updates

    • Test forms after configuration changes

    • Verify CSRF tokens work

    • Check session persistence

Advanced Configuration

Custom CSRF Token TTL

Adjust CSRF token lifetime if needed:

Per-Environment Configuration

Use different settings for dev/staging/production:

Last updated