1

tl;dr

私の製品では、レールは異なるページ訪問で異なる csrf トークンを持っているようですが、セッションごとに 1 つの csrf トークンしかないと思いました。Rails の csrf トークンの仕組みを誤解していますか? それとも私の状況に何かありますか?

いくつかのコンテキスト情報: この Web サイトは、実際には tomcat 内で実行されている戦争です。コードの一部は、jruby-rackを介してレール上で実行されます(理由は聞かないでください ;) それは私が持っているものです)。

詳細

私がいる状況では、レールの csrf コードに手動でデバッグ コードを追加しました。具体的には、verified_request? methodこれを次のように変更しました。

  def verified_request?
    logger.info "printing info from `verified_request?` ..."
    logger.info "\trequest_forgery_protection_token = #{request_forgery_protection_token}"
    logger.info "\tform_authenticity_token = #{form_authenticity_token}"
    logger.info "\tparams[request_forgery_protection_token] = #{params[request_forgery_protection_token]}"
    logger.info "\trequest.headers['X-CSRF-Token'] = #{request.headers['X-CSRF-Token']}"
    logger.info
    !protect_against_forgery? || request.get? ||
      form_authenticity_token == params[request_forgery_protection_token] ||
      form_authenticity_token == request.headers['X-CSRF-Token']
  end

以下はログ出力です。重要な部分は、'form_authenticity_token' が異なる時点で異なることです (ただし、時々繰り返されます)。form_authenticity_token関数はセッションごとに同じものを返すため、それは私には意味がありません。

printing info from `verified_request?` ...
    request_forgery_protection_token = authenticity_token
    form_authenticity_token = wMPfNOM8s1Z0tLfeDJRpwKoWYGnA/K21SkgROLP2DMY=
    params[request_forgery_protection_token] = 
    request.headers['X-CSRF-Token'] = 

printing info from `verified_request?` ...
    request_forgery_protection_token = authenticity_token
    form_authenticity_token = viGS5kkOGvte7Sq+FpRsowiwujJNG8Y2WpTqqEShCy0=
    params[request_forgery_protection_token] = 
    request.headers['X-CSRF-Token'] = 

printing info from `verified_request?` ...
    request_forgery_protection_token = authenticity_token
    form_authenticity_token = lBpCrPHpuyyiyfCs30Jonz+vqOsQG1VKbbPOJl07DNE=
    params[request_forgery_protection_token] = 
    request.headers['X-CSRF-Token'] = viGS5kkOGvte7Sq+FpRsowiwujJNG8Y2WpTqqEShCy0=

printing info from `verified_request?` ...
    request_forgery_protection_token = authenticity_token
    form_authenticity_token = wMPfNOM8s1Z0tLfeDJRpwKoWYGnA/K21SkgROLP2DMY=
    params[request_forgery_protection_token] = viGS5kkOGvte7Sq+FpRsowiwujJNG8Y2WpTqqEShCy0=
    request.headers['X-CSRF-Token'] = 
4

1 に答える 1