symfony/security-csrf
Symfony Security CSRF component provides CsrfTokenManager to generate, store, and validate CSRF tokens, protecting forms and requests against cross-site request forgery. Integrates cleanly with Symfony apps and can be used standalone in PHP projects.
CSRF Protection Alignment:
The symfony/security-csrf package is a direct fit for Laravel applications requiring CSRF protection, particularly for:
Symfony Ecosystem Synergy:
If the Laravel stack already integrates with Symfony components (e.g., symfony/http-foundation, symfony/routing, or symfony/psr-http-message-bridge), this package reduces duplication and leverages shared security abstractions. For example:
CsrfTokenManager can replace or extend Laravel’s csrf_token() helper.Validator or Laravel’s Validator facade.symfony/http-foundation if used for request/response objects.Flexibility Over Laravel’s Defaults: Laravel’s built-in CSRF middleware relies on session storage, which may not suit:
Laravel Compatibility:
VerifyCsrfToken middleware uses session storage by default, while Symfony’s CsrfTokenManager is storage-agnostic.Dependency Conflicts:
symfony/http-foundation if used for request handling).symfony/flex or symfony/require to enforce version consistency.composer.json overrides to align Symfony dependencies."extra": {
"symfony": {
"allow-contrib": false,
"require": "7.4.*"
}
}
Token Storage Adaptation:
CsrfTokenManagerInterface expectations (e.g., key-value store assumptions).TokenStorage class extending Illuminate/Session to wrap Symfony’s interface.
class LaravelSessionTokenStorage implements TokenStorageInterface {
public function getToken($tokenId): ?Token {
return Session::get('csrf_token_' . $tokenId);
}
// ... other methods
}
Cache or Database facades to store tokens.X-CSRF-Token headers (requires custom middleware).Middleware Integration:
VerifyCsrfToken is stateful.CsrfTokenManager.Illuminate\Auth\Middleware\VerifyCsrfToken.namespace App\Http\Middleware;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Closure;
class SymfonyCsrfMiddleware {
public function __construct(private CsrfTokenManagerInterface $tokenManager) {}
public function handle($request, Closure $next) {
$token = $request->header('X-CSRF-TOKEN');
if (!$this->tokenManager->isTokenValid('api_token', $token)) {
abort(403, 'Invalid CSRF token.');
}
return $next($request);
}
}
Middleware Injection Risk:
api, web) to scope CSRF validation.app/Http/Kernel.php:
'api' => [
\App\Http\Middleware\SymfonyCsrfMiddleware::class,
// ... other middleware
],
Token Format Inconsistencies:
SYMFONY_CSRF_TOKEN) may conflict with Laravel’s _token convention.CsrfTokenManager to use Laravel’s expected token name:
$tokenManager = new CsrfTokenManager([
'token_name' => '_token', // Laravel's default
'token_generator' => new SecureRandomTokenGenerator(32),
]);
X-CSRF-Token).Performance Overhead:
symfony/security-core for HMAC-signed tokens to reduce storage I/O.Authorization headers instead of cookies/sessions.Session Dependency:
Upgrade Risk:
composer.json platform checks to enforce PHP versions.Use Case Clarity:
X-CSRF-Token) or cookie-based?Token Storage:
Existing CSRF Implementation:
@csrf) be generated?Symfony Ecosystem Adoption:
security-bundle, http-foundation)?Token Rotation Policy:
CsrfTokenManager supports TTL but requires custom configuration.)How can I help you explore Laravel packages today?