Skip to content

Commit

Permalink
Allow redirecting on CSRF failures. Fixes #1012
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Jul 9, 2018
1 parent 106a37d commit 107753d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions application/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ class App extends BaseConfig
| CSRFCookieName = The cookie name
| CSRFExpire = The number in seconds the token should expire.
| CSRFRegenerate = Regenerate token on every submission
| CSRFRedirect = Redirect to previous page with error on failure
*/
public $CSRFTokenName = 'csrf_test_name';
public $CSRFCookieName = 'csrf_cookie_name';
public $CSRFExpire = 7200;
public $CSRFRegenerate = true;
public $CSRFRedirect = true;

/*
|--------------------------------------------------------------------------
Expand Down
15 changes: 14 additions & 1 deletion application/Filters/CSRF.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Security\Exceptions\SecurityException;
use Config\Services;

class CSRF implements FilterInterface
Expand Down Expand Up @@ -30,7 +31,19 @@ public function before(RequestInterface $request)

$security = Services::security();

$security->CSRFVerify($request);
try
{
$security->CSRFVerify($request);
}
catch (SecurityException $e)
{
if (config('App')->CSRFRedirect && ! $request->isAJAX())
{
return redirect()->back()->with('error', $e->getMessage());
}

throw $e;
}
}

//--------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions tests/_support/Config/MockAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MockAppConfig
public $CSRFExpire = 7200;
public $CSRFRegenerate = true;
public $CSRFExcludeURIs = ['http://example.com'];
public $CSRFRedirect = false;

public $CSPEnabled = false;

Expand Down
9 changes: 9 additions & 0 deletions user_guide_src/source/libraries/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ may alter this behavior by editing the following config parameter

public $CSRFRegenerate = true;

When a request fails the CSRF validation check, it will redirect to the previous page by default,
setting an ``error`` flash message that you can display to the end user. This provides a nicer experience
than simply crashing. This can be turned off by editing the ``$CSRFRedirect`` value in
**application/Config/App.php**::

public $CSRFRedirect = false;

Even when the redirect value is **true**, AJAX calls will not redirect, but will throw an error.

*********************
Other Helpful Methods
*********************
Expand Down

0 comments on commit 107753d

Please sign in to comment.