CSRF and expired logout forms
Prevent users seeing a 419 Page Expired when logging out
Best practice for logging out is that the function should be driven from a form and use a csrf token to prevent someone logging you out in a CSRF attack. Forcibly logging users out may cause them to disclose login credentials whilst they are being monitored.
A problem with the CSRF token in the logout form is that it can become stale in at least the following three scenarios
  1. 1.
    User has more than one tab open and logs out in one of them. The logout action on other tabs will now produce 419 Page Expired error
  2. 2.
    User has a single tab open but leaves the site and does not return during the session duration. The CSRF token is then stale and pressing logout gives 419 error.
  3. 3.
    The user uses the option to close all sessions on other devices. Logout on a terminated session will generate the error.
A solution to the problem is relatively simple, and requires a small addition to the VerifyCsrfToken middleware;
app/Http/Middleware/VerifyCsrfToken.php
1
<?php
2
​
3
namespace App\Http\Middleware;
4
​
5
use Closure;
6
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
7
use Illuminate\Support\Facades\Auth;
8
​
9
class VerifyCsrfToken extends Middleware
10
{
11
/**
12
* The URIs that should be excluded from CSRF verification.
13
*
14
* @var array
15
*/
16
protected $except = [
17
//
18
];
19
20
public function handle($request, Closure $next)
21
{
22
if($request->route()->named('logout')) {
23
​
24
if (!Auth::check() || Auth::guard()->viaRemember()) {
25
​
26
$this->except[] = route('logout');
27
28
}
29
​
30
}
31
​
32
return parent::handle($request, $next);
33
}
34
}
35
​
Copied!
Normally this file contains just an $except array of routes that should be ignored from csrf.
In this code we override the handle method and perform three checks.
  1. 1.
    is the route logout
  2. 2.
    is the user a guest (ie, not using an authenticated session), or,
  3. 3.
    did the user just get logged back in via the 'Remember Me' cookie
If this is the case then we add 'logout' to the except array. We then pass control to the core VerifyCsrfMiddleware which recognises the presence of the logout route in the array, and bypasses the check. The form data is correctly posted and we are redirected using the LogoutResponse.
The user sees no error page.
By checking in this way, we ensure that valid logout requests by authenticated users are still protected by CSRF Token and that those with expired sessions do not see the 419 error.
Last modified 2mo ago
Copy link