core icon indicating copy to clipboard operation
core copied to clipboard

fuel_csrf_token() wont generate token.

Open Ghostff opened this issue 9 years ago • 22 comments

This use to wok before but all of a sudden it stopped. I downloaded a new one and added this:

<?= Security::js_fetch_token(); ?>
<script>
console.log(fuel_csrf_token());
</script>

to the views > welcome > index.php the fuel_csrf_token it created the fuel_csrf_token function and that's it.

Ghostff avatar Sep 21 '16 05:09 Ghostff

You upgraded your PHP version, and short tags are now disabled? It still works fine here:

[wanwizard@catwoman] $ oil console
Fuel 1.9-dev - PHP 5.6.25 (cli) (Aug 24 2016 07:55:57) [Linux]
>>> Security::js_fetch_token();
<script type="text/javascript">\n\tfunction fuel_csrf_token()\n\t{\n\t\tif (document.cookie.length > 0)\n\t\t{\n\t\t\tvar c_name = "fuel_csrf_token";\n\t\t\tc_start = document.cookie.indexOf(c_name + "=");\n\t\t\tif (c_start != -1)\n\t\t\t{\n\t\t\t\tc_start = c_start + c_name.length + 1;\n\t\t\t\tc_end = document.cookie.indexOf(";" , c_start);\n\t\t\t\tif (c_end == -1)\n\t\t\t\t{\n\t\t\t\t\tc_end=document.cookie.length;\n\t\t\t\t}\n\t\t\t\treturn unescape(document.cookie.substring(c_start, c_end));\n\t\t\t}\n\t\t}\n\t\treturn "";\n\t}\n</script>\n
>>> exit

WanWizard avatar Sep 21 '16 10:09 WanWizard

It generates the js function . But it seems to fall out after if (c_start != -1) and wen i log c_start i get -1

Ghostff avatar Sep 21 '16 13:09 Ghostff

If you get -1 there, the csrf cookie does not exist.

Are you changing the name of the cookie in your app after the Security class is loaded?

WanWizard avatar Sep 21 '16 15:09 WanWizard

please take a look at this VIDEO

Ghostff avatar Sep 21 '16 17:09 Ghostff

For the audience: you downloaded the framework, added the call to the welcome controller, and then discovered it doesn't work. Rightly so, because as I wrote before, the cookie needs to exist before this will work.

And you have done nothing yet to create the cookie. The cookie is only created when you call Security::set_token() or Security::fetch_token(), something that happens automatically when you create a form that includes a CSRF token, for example by using Form::csrf().

WanWizard avatar Sep 22 '16 12:09 WanWizard

In other words: Try this:

<?php echo Form::csrf(); ?>

<?php echo Security::js_fetch_token(); ?>
<script>
console.log(fuel_csrf_token());
</script>

Art4 avatar Sep 22 '16 13:09 Art4

Is there any reason why it worked before (like it use to generate token without the <?php Security::fetch_token() ?>)

Ghostff avatar Sep 22 '16 14:09 Ghostff

I wouldn't know, it has always worked like this. DId you have security.csrf_autoload enabled in your config by any chance? That is the only thing that would trigger creation of the cookie on every request.

Obviously, in any app, the first page with a form (and a csrf token) would create the cookie, and the cookie would exist until you close the browser, also on pages that don't have a token. Also check the value of security.csrf_expiration in your config file, that would influence the lifespan of the cookie.

WanWizard avatar Sep 22 '16 15:09 WanWizard

This may have caused a change in behaviour, in case you have a config file that doesn't include the csrf_autoload key: https://github.com/fuel/core/commit/5b407215a58fef482b346a5f2f9dfc71e5f00a0a

WanWizard avatar Sep 22 '16 15:09 WanWizard

@Ghostff any more feedback on this topic?

WanWizard avatar Oct 05 '16 13:10 WanWizard

nah, it just works well adding Security::set_token() to my bootstrap. but still works well at my other pc without Security::set_token() and security.csrf_autoload not enabled

Ghostff avatar Oct 05 '16 15:10 Ghostff

With the same codebase? Or with a pre-1.8 version of Fuel that had autoload enabled by default?

WanWizard avatar Oct 05 '16 15:10 WanWizard

i switch to a new pc, and installed fuel 1.8.0, it generates a token by itself (didnt include Security::set_token(), Security::fetch_token() and security.csrf_autoload is false), logs me out after the second refresh on ajax login.

Ghostff avatar Nov 20 '16 01:11 Ghostff

Logins are not controlled by the CSRF token, so I can't see the connection between the two.

How do you install "1.8.0"? And do you run a composer update after that to fetch the latest hotfixes for 1.8.0?

WanWizard avatar Nov 20 '16 13:11 WanWizard

like i validate CSFR before login more like if (Security::check_token(Input::post('__token'))) { ..} and it happens to be true without Security::set_token(), Security::fetch_token() or security.csrf_autoload enabled and yes i updated code sample

Ghostff avatar Nov 20 '16 17:11 Ghostff

As soon as you validate a token, the token will expire. If your ajax page doesn't load a new token, any other form post will post using an invalid token.

WanWizard avatar Nov 20 '16 21:11 WanWizard

i just post the form once, and its logs me in. but after multiple(2) page refresh, i get logged out. question is why is it passing the Security::check_token when token is not enabled, and what can cause a logout on page refresh?

Ghostff avatar Nov 21 '16 00:11 Ghostff

I am clueless as to what you are talking about. You refresh the post page (i.e. you post the login again)? If you do so, and you have no mechanism in place to update the token in the form, you post the form again with an invalid token, see my previous remark.

If you use the Auth package, the login status is recorded in the session, and is not related to CRSF tokens at all. There can be all sorts of reasons for losing the session, but since it's an ajax app, I'd start with checking if the session cookie is updated on the client. If not, you lose the session when the session token rotates server side (by default every 300 seconds).

WanWizard avatar Nov 21 '16 12:11 WanWizard

like if i login it takes me to account.page but when i refresh the account.page it logs me out.

Ghostff avatar Nov 21 '16 16:11 Ghostff

So, did you do what I asked and checked if your session mechanism works?

WanWizard avatar Nov 21 '16 20:11 WanWizard

am not sure if doing it right i have a token of (length=570) after login (length=698) on account.page refresh (length=570)(when i get logged out)

Ghostff avatar Nov 21 '16 22:11 Ghostff

what kind of session storage do you use? the cookie itself? It looks like you're losing the session for some reason. Time settings on the server ok?

WanWizard avatar Nov 22 '16 10:11 WanWizard