Understanding the Rails Authenticity Token

前端 未结 10 1270
攒了一身酷
攒了一身酷 2020-11-22 05:55

I am running into some issues regarding the Authenticity Token in Rails, as I have many times now.

But I really don\'t want to just solve this problem and go on. I w

10条回答
  •  爱一瞬间的悲伤
    2020-11-22 06:36

    Minimal attack example that would be prevented: CSRF

    On my website evil.com I convince you to submit the following form:

    If you are logged into your bank through session cookies, then the cookies would be sent and the transfer would be made without you even knowing it.

    That is were the CSRF token comes into play:

    • with the GET response that that returned the form, Rails sends a very long random hidden parameter
    • when the browser makes the POST request, it will send the parameter along, and the server will only accept it if it matches

    So the form on an authentic browser would look like:

    Thus, my attack would fail, since it was not sending the authenticity_token parameter, and there is no way I could have guessed it since it is a huge random number.

    This prevention technique is called Synchronizer Token Pattern.

    Same Origin Policy

    But what if the attacker made two requests with JavaScript, one to read the token, and the second one to make the transfer?

    The synchronizer token pattern alone is not enough to prevent that!

    This is where the Same Origin Policy comes to the rescue, as I have explained at: https://security.stackexchange.com/questions/8264/why-is-the-same-origin-policy-so-important/72569#72569

    How Rails sends the tokens

    Covered at: Rails: How Does csrf_meta_tag Work?

    Basically:

    • HTML helpers like form_tag add a hidden field to the form for you if it's not a GET form

    • AJAX is dealt with automatically by jquery-ujs, which reads the token from the meta elements added to your header by csrf_meta_tags (present in the default template), and adds it to any request made.

      uJS also tries to update the token in forms in outdated cached fragments.

    Other prevention approaches

    • check if certain headers is present e.g. X-Requested-With:
      • What's the point of the X-Requested-With header?
      • https://security.stackexchange.com/questions/23371/csrf-protection-with-custom-headers-and-without-validating-token
      • Is an X-Requested-With header server check sufficient to protect against a CSRF for an ajax-driven application?
    • check the value of the Origin header: https://security.stackexchange.com/questions/91165/why-is-the-synchronizer-token-pattern-preferred-over-the-origin-header-check-to
    • re-authentication: ask user for password again. This should be done for every critical operation (bank login and money transfers, password changes in most websites), in case your site ever gets XSSed. The downside is that the user has to type the password multiple times, which is tiresome, and increases the chances of keylogging / shoulder surfing.

提交回复
热议问题