How CSRF Protection Works
CSRF attacks trick authenticated users into submitting malicious requests. Framefox protects against this by:- Generating a unique token for each user session
- Storing the token in a secure cookie
- Requiring the token to be submitted with forms
- Validating tokens using constant-time comparison
- Rejecting requests with invalid or missing tokens
Using CSRF Tokens in Forms
Basic Form Protection
Add thecsrf_token() function to your form templates:
Login Forms
Login forms must include CSRF tokens:AJAX Requests
For AJAX requests, include the token in request headers or body:Alternative: Meta Tag
Store the token in a meta tag for easy access:CSRF Token Generation
Tokens are automatically generated by theCsrfTokenManager:
Token Validation
TheCsrfTokenBadge class validates tokens using constant-time comparison:
/home/daytona/workspace/source/framefox/core/security/passport/csrf_token_badge.py:18
Why Constant-Time Comparison?
Thesecrets.compare_digest() function prevents timing attacks by ensuring the comparison takes the same time regardless of where the difference occurs:
Automatic Validation in Firewall
TheFirewallHandler automatically validates CSRF tokens:
/home/daytona/workspace/source/framefox/core/security/handlers/firewall_handler.py:273
CSRF Token Lifecycle
1. Token Generation
Tokens are generated when:- A new session is created
- User first visits the site
- After successful login
2. Token Storage
Tokens are stored in:- Secure HTTP-only cookies
- Session storage
- Request state (during validation)
3. Token Validation
Tokens are validated on:- POST requests (form submissions)
- PUT requests (updates)
- PATCH requests (partial updates)
- DELETE requests (optional)
4. Token Expiration
Tokens expire when:- User logs out
- Session expires
- Cookie is deleted
Handling CSRF Errors
Custom Error Messages
Error Template
Configuration
Cookie Settings
Configure CSRF cookie behavior:Excluding Routes
Exclude certain routes from CSRF protection:Advanced Usage
Manual Token Validation
For custom controllers, validate tokens manually:Token Refresh
Refresh tokens after sensitive operations:Security Best Practices
1. Always Use HTTPS
CSRF tokens should only be transmitted over HTTPS:2. Use SameSite Cookies
SetSameSite attribute to prevent cross-site token leakage:
3. Rotate Tokens Regularly
Rotate tokens after:- Login/logout
- Password changes
- Permission changes
4. Never Expose Tokens in URLs
5. Validate on All State-Changing Requests
Protect all operations that modify data:- POST (create)
- PUT (update)
- PATCH (partial update)
- DELETE (remove)
Testing CSRF Protection
Unit Tests
Integration Tests
Troubleshooting
Token Mismatch Errors
If you’re getting CSRF errors:- Check cookie settings: Ensure cookies are being set correctly
- Verify HTTPS: Secure cookies require HTTPS
- Check SameSite: Some browsers block cookies with strict SameSite
- Clear old sessions: Old tokens may be cached
AJAX Requests Failing
- Include token in headers: Add
X-CSRF-Tokenheader - Check cookie access: Ensure JavaScript can read cookies
- Verify CORS settings: Cross-origin requests need proper CORS headers
Next Steps
Authentication
Learn about user authentication flows
Security Overview
Explore all security features