symfony/security-http
Symfony Security HTTP integrates the Security Core with HTTP: firewalls, authenticators, and request/response handling to protect parts of your app and authenticate users. Install via composer require symfony/security-http.
## Getting Started
### Minimal Setup in Laravel (Updated for v8.1.0-BETA3)
Install the updated package via Composer:
```bash
composer require symfony/security-http:^8.1.0-BETA3
Leverage the updated attribute system (now hardened against HEAD request bypasses):
// routes/web.php
use Symfony\Component\Security\Http\Attribute\IsGranted;
Route::get('/admin', [AdminController::class, 'dashboard'])
->middleware(['auth'])
->withAttributes([
new IsGranted('ROLE_ADMIN')
]);
app/Http/Kernel.php: Register middleware with security hardening:
protected $middlewareGroups = [
'web' => [
\Symfony\Component\Security\Http\Middleware\FirewallMiddleware::class,
// Other middleware...
],
];
config/security.php: Updated OIDC configuration with new claim handling:
'providers' => [
'oidc' => [
'type' => 'openid_connect',
'claims' => ['email', 'name'], // Explicitly define claims (CVE-2026-45069 fix)
],
],
Route::middleware(['auth', 'csrf'])->group(function () {
// Protected routes with CSRF protection
});
// config/security.php
'firewalls' => [
'cas' => [
'pattern' => '^/cas',
'cas' => [
'service' => '/cas/service',
'trusted_hosts' => ['yourdomain.com', 'localhost'], // Required for CAS (CVE-2026-45074)
],
],
],
// config/security.php
'providers' => [
'oidc' => [
'type' => 'openid_connect',
'client_id' => 'your-client-id',
'claims' => ['email', 'name', 'preferred_username'], // Explicit claims
'discovery_endpoint' => 'https://your-identity-provider/.well-known/openid-configuration',
],
],
use Symfony\Component\Security\Core\User\UserInterface;
#[CurrentUser]
public function show(UserInterface $user)
{
// Access claims via $user->getUserIdentifier() or custom getters
return view('profile', ['user' => $user]);
}
use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid;
class ProfileController
{
#[IsCsrfTokenValid(
token: 'profile_update',
message: 'Invalid CSRF token.',
methods: ['POST', 'PUT', 'PATCH'] // Explicitly define allowed methods
)]
public function update(Request $request)
{
// ...
}
}
// app/Http/Middleware/ImpersonateUser.php
public function handle(Request $request, Closure $next)
{
if ($request->has('impersonate')) {
$this->auth()->impersonate($request->user(), $request->input('impersonate'));
}
return $next($request);
}
// config/security.php
'firewalls' => [
'x509' => [
'pattern' => '^/x509',
'x509' => [
'subject' => ['CN=%u'], // Updated to use RDN boundary
],
],
],
IsGranted, IsCsrfTokenValid, and IsSignatureValid attributes.
#[IsGranted('ROLE_ADMIN', methods: ['GET', 'POST'])] // Only allow GET/POST
public function adminAction()
{
// ...
}
// app/Http/Middleware/BlockHeadRequests.php
public function handle(Request $request, Closure $next)
{
if ($request->isMethod('HEAD')) {
abort(405, 'HEAD method not allowed');
}
return $next($request);
}
trusted_hosts configuration (CVE-2026-45074).
'firewalls' => [
'cas' => [
'cas' => [
'trusted_hosts' => ['yourdomain.com', 'localhost'], // Mandatory
],
],
],
OidcTokenHandler could lead to authentication issues (CVE-2026-45069).
'providers' => [
'oidc' => [
'claims' => ['email', 'name', 'preferred_username'], // Required
],
],
#[CurrentUser]
public function debug(UserInterface $user)
{
dd($user->getUserIdentifier(), $user->getRoles());
}
public function stopImpersonating()
{
$this->auth()->stopImpersonating();
return back();
}
<form method="POST" action="/update">
@csrf
<!-- CSRF token is automatically included -->
</form>
fetch('/update', {
method: 'POST', // Must match attribute's allowed methods
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
},
});
use Symfony\Component\Security\Http\Middleware\FirewallMiddleware;
// Register in Kernel.php
protected $middleware = [
FirewallMiddleware::class,
];
use Symfony\Component\Security\Http\Event\AuthenticatorFailureEvent;
$eventDispatcher->addListener(
AuthenticatorFailureEvent::class,
function (AuthenticatorFailureEvent $event) {
// Custom failure logic
}
);
AuthenticationUtils for testing:
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
public function testAuthenticatedRoute()
{
$authUtils = $this->app->make(AuthenticationUtils::class);
$authUtils->startAuthentication('main', new Request());
$response = $this->get('/admin');
$response->assertStatus(200);
}
$response = $this->actingAs(User::find(1))
->withHeaders(['X-CSRF-TOKEN' => csrf_token()])
->post('/update');
'providers' => [
'oidc' => [
'claims_cache_ttl' => 60, // Cache claims for 60 seconds
],
How can I help you explore Laravel packages today?