Symfony Security CSRF
Generate and validate CSRF tokens to protect PHP applications against cross-site request forgery.
Repository Health
Technical Analysis
Symfony Security CSRF is a small, focused PHP library that provides the tooling to defend web applications against cross-site request forgery (CSRF) attacks. Its central class, CsrfTokenManager, generates unguessable tokens tied to arbitrary token IDs, persists them in session storage, and validates incoming tokens using constant-time comparison.
Distributed as a standalone Composer package, it can be used inside the full Symfony framework or dropped into any PHP project. The component ships pluggable interfaces for token generation and storage, plus a SameOriginCsrfTokenManager for stateless, same-origin double-submit protection.
What You Get
- A CsrfTokenManager implementing a clean CsrfTokenManagerInterface for getToken, refreshToken, removeToken, and isTokenValid
- A URI-safe token generator producing unguessable, URL-friendly token values
- Session-backed token storage (native and Symfony session variants) plus a pluggable TokenStorageInterface
- A SameOriginCsrfTokenManager for stateless double-submit CSRF protection
- Constant-time token comparison to guard against timing attacks
Common Use Cases
- Protecting HTML form submissions in Symfony or standalone PHP applications
- Guarding state-changing POST/PUT/DELETE endpoints against forged requests
- Issuing per-action, once-only tokens for high-security operations
- Adding stateless same-origin CSRF protection to APIs via double-submit tokens
Under The Hood
Architecture - The component centers on CsrfTokenManager, which composes a TokenGeneratorInterface (default UriSafeTokenGenerator) and a TokenStorageInterface (default NativeSessionTokenStorage) behind the CsrfTokenManagerInterface contract in CsrfTokenManager.php. Tokens are namespaced by an HTTPS-aware closure so secure and insecure contexts never share values, and validation reads the stored value and compares it in constant time. A separate SameOriginCsrfTokenManager implements a stateless double-submit strategy driven by request origin.
Tech Stack - Pure PHP 8.4+, distributed via Composer with PSR-4 autoloading under Symfony\Component\Security\Csrf. It depends only on symfony/deprecation-contracts and symfony/security-core, with optional http-foundation/http-kernel integration for request-aware namespacing.
Code Quality - The code is strictly typed, small, and interface-driven, with a dedicated Tests directory and a phpunit.xml.dist configuration covering the manager, generator, and storage variants. Naming follows Symfony conventions and each public method carries documented contracts.
API Design - The public surface is four intent-revealing methods (getToken, refreshToken, removeToken, isTokenValid) plus sensible defaults, so basic protection works with zero configuration while generator and storage remain swappable for advanced needs.
Used by 2 apps in this directory
Kimai
Invoicing Finance · Project Management
Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.
Mautic
Automation · Marketing · Ecommerce
The world's largest open source marketing automation platform — own your data, run multi-channel campaigns, and escape vendor lock-in forever.