Rails如何在不同的时间拥有不同的csrf令牌?

时间:2013-05-21 15:20:53

标签: ruby-on-rails jruby csrf

TL;博士

在我的产品中,rails在不同的页面访问中似乎有不同的csrf令牌,但我认为每个会话只有一个csrf令牌。我误解了rails的csrf令牌是如何工作的吗?或者这与我的情况有关吗?

一些上下文信息:该网站实际上是在tomcat中运行的战争。部分代码通过jruby-rack在轨道上运行(请不要问为什么;)这就是我所拥有的。)

详情

对于我所处的情况,我在rail的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 function会为每个会话返回相同的内容。

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'] = 

1 个答案:

答案 0 :(得分:0)

事实证明,我们正在运行jruby 的多个进程正在使用内存进行缓存。这意味着每个进程都有不同的会话。我们回到了只有一个jruby的过程。

相关问题