首页>itarticle>csrf protection in rails ( actioncontroller::requestforgeryprotection )
csrf protection in rails ( actioncontroller::requestforgeryprotection )
admin1月 18, 20210
The Basics
There are two components to CSRF. First, a unique token is embedded in your site’s HTML. That same token is also stored in the session cookie. When a user makes a POST request, the CSRF token from the HTML gets sent with that request. Rails compares the token from the page with the token from the session cookie to ensure they match.
Generation encrypted token and embedded into form
Token generation(raw token) & Store into session
1 2 3 4 5 6
def(session)# :doc: session[:_csrf_token] ||= SecureRandom.base64(AUTHENTICITY_TOKEN_LENGTH) Base64.strict_decode64(session[:_csrf_token]) end
defcsrf_meta_tags if protect_against_forgery? [ tag("meta", name:"csrf-param", content: request_forgery_protection_token), tag("meta", name:"csrf-token", content: form_authenticity_token) ].join("n").html_safe end end
then you’ll see:
In the post params, get authenticity_token & decryption token and verify
近期评论