Symfony Security CSRF

Generate and validate CSRF tokens to protect PHP applications against cross-site request forgery.

Library
Composer
vv8.1.0
1,761stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
69/100Good
Development Activity60
Maintenance64
Community52
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture85
Code Quality88
Innovation82
Learning Curve72

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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search