【问题标题】:How can Rails have different csrf tokens at different times?Rails 如何在不同时间拥有不同的 csrf 令牌?
【发布时间】:2013-05-22 01:05:46
【问题描述】:

tl;博士

在我的产品中,rails 在不同的页面访问时似乎有不同的 csrf 令牌,但我认为每个会话只有一个 csrf 令牌。我是否误解了 rails 的 csrf 令牌的工作原理?还是跟我的情况有关?

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

详情

对于我所处的情况,我在 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'] = 

【问题讨论】:

    标签: ruby-on-rails jruby csrf


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2019-11-19
      • 2016-06-21
      • 2014-07-12
      • 2015-10-27
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多