Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
CSRF problem
  • Hi, I have small problem with CSRF validation. 
    In my config file i have "csrf_autoload" set to auto and my form have hidden csrf token field. But on submitt i got error "CSRF validation failed, Possible hacking attempt detected!". What am i missing?
  • You are sure $_POST contains the hidden csrf token field? And you are not checking manually as well (because that will invalidate the token)? Having multiple windows with forms open?

    You can use http://docs.fuelphp.com/classes/security.html#/method_js_set_token on your forms to make sure the hidden token field contains the correct token, to handle token rotation with multiple forms or multiple windows.
  • I'm creating view with:

    $data['token'] = Security::generate_token();
    return Responce::forge(View::forge('login.twig', $data));

    In page source code i see:
    <input type="hidden" name="csrf_token" id="csrf_token" value="...">
    with long alfanumeric value of field.

    I'm not checking for token manually and i have only one window with form opened.

  • Did you configure that fieldname in the config? Because by default the field name is "fuel_csrf_token".
  • Yes, i changed that:

    'csrf_token_key' => 'csrf_token'
  • HarroHarro
    Accepted Answer
    You use Security::generate_token(), which just generates a random token. It doesn't have anything to do with csrf checking. You need to use Security::fetch_token() instead.


  • Yes, that solved my problem. Thank You very much.
  • I had a similar problem where I set "csrf_autoload" to true and while also manually checking with "Security::check_token()." Thanks to this post I was able to solve that small bug. But I don't see it clearly stated in the documentation.

  • Checking the token invalidates and rotates the current token, so you can check only once. If you do both, you end up with invalid token errors all the time.

    I'll see how this can be clarified in the docs.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion