I’m using CodeIgniter 2 along with the Ion Auth authorization system by Ben Edmunds.
After creating my project, I would sometimes get a CodeIgniter error upon certain login attempts but this error was intermittent.
The action you have requested is not allowed.
After some troubleshooting, it became apparent this error was caused by an invalid CSRF token. Why is the token invalid? Well, in CodeIgniter’s configuration file, it’s set to expire in 4 hours. So if you load your login page and allow it to sit there for 4 hours before attempting a login, the CSRF tokens will expire and this will generate the error message as above. Simply reloading the login page avoids any issues.
You can verify this error message for yourself by deleting the CSRF cookie after you load the login page.
A cleaner solution would be to redirect to a custom error page or to display a flash message. However, this solution is not as simple as it sounds because when you extend the CodeIgniter Security class, certain hook-points are not available and you cannot yet access CodeIgniter’s Super Object using `get_instance()`.
So when you extend the Security class, you’re limited to standard PHP. In this case, I’m using PHP `header()` to redirect the offending login page (or any form page) back to itself.
<?php class MY_Security extends CI_Security { public function __construct() { parent::__construct(); } public function csrf_show_error() { // show_error('The action you have requested is not allowed.'); // default code // force page "refresh" - redirect back to itself with sanitized URI for security // a page refresh restores the CSRF cookie to allow a subsequent login header('Location: ' . htmlspecialchars($_SERVER['REQUEST_URI']), TRUE, 200); } }
This works fine except that the user gets a screen refresh without any indication why they have to enter their login credentials a seconds time.
I decided to make this a bit more user-friendly by adding another function into a Controller, in my case, the Ion Auth controller…
function csrf_redirect() { $flash = 'Session cookie automatically reset due to expired browser session. Please try again.'; $this->session->set_flashdata('message', $flash); redirect('/login', 'location'); }
As you can see, this function sets a flash message telling the user what happened and then redirects them to a fresh instance of the login page.
Session cookie automatically reset due to expired browser session. Please try again.
Instead of using PHP `header()` to redirect a page refresh, redirect to this new Ion Auth controller function at `/auth/csrf_redirect`.
<?php class MY_Security extends CI_Security { public function __construct() { parent::__construct(); } public function csrf_show_error() { // show_error('The action you have requested is not allowed.'); // default code // force redirect to the csrf_redirect function // this gives the user a useful message instructing them to login again // while the CSRF cookie is also refreshed to allow a new login header('Location: /auth/csrf_redirect', TRUE, 302); } }
The minor downside to this method is that you are always redirected back to the login page rather than a refresh of whatever page/form you’re trying to submit. However, that should be a moot point, since the session cookie expires at nearly the same time as the CSRF cookie, you’d be redirected back to the same login page regardless. You may also not be requiring the user be logged in for your particular form, so please be aware and re-direct accordingly.
Your code has a flaw: you must exit or die after a redirect header is called, otherwise the rest of the code will still be executed.
There is no “rest of the code” in this case, so `exit` or `die` is not required.
Sorry, where should I write that code?
It’s unclear which bit of code you’re asking about. Although, if you’re asking about extending classes, I refer you to the CodeIgniter documentation on that topic.
First check in `application/config/config.php` for `$config[‘subclass_prefix’]`. See what it’s set to, it should be `MY_` by default.
Create a file called `MY_Security.php` in your `application/core` folder.
Put in the top of it `
Be aware that the session might not have expired. So user could still be logged into the system, on a page with a form. So when posting that form, they would need to be redirected to the current page with an error message. Either way not a very user-friendly approach. Reloading CSRF would be better as the user doesn’t notice
This is cool. I’ve been trying to figure this out on my own, but to no avail. By the way is this CSRF trick applicable for Codeigniter 3+? Thanks a ton!
Rasmus, as far as I know, this should work the same in version 3. Let me know if you see something different.
Can’t we use ajax to get the CSRF and put it in the form every 30 mins or whatever time of session expiry is? Won’t it be much user-friendly?
Maybe. I never considered that as it would depend on JavaScript, so you would have to examine what happens when JS is disabled. Also, without reloading the page, you’d need JavaScript to rewrite the form in order to dynamically insert the new token, which also cannot be done without JavaScript. For my purposes I wanted a solution that was bullet-proof – that would not depend on anything that could be disabled by the user.