【问题标题】:Paperclip gem triggering CSRF token verification problemsPaperclip gem 触发 CSRF 令牌验证问题
【发布时间】:2017-01-09 18:53:57
【问题描述】:

我有一个使用回形针 gem (v 3.4.0) 的 Rails 3.1 应用程序。简而言之。我有一个故事模型和一个帖子模型。一个故事可以有很多帖子。

#story.rb

class Story < ActiveRecord::Base

  attr_accessible :title, :user_id, :username, :posts_attributes

  belongs_to    :user
  has_many      :posts, :dependent  =>  :destroy,
                      :order => "created_at DESC"

  accepts_nested_attributes_for :posts, :reject_if => lambda { |t| t['contents'].nil? }

end

#post.rb

class Post < ActiveRecord::Base

  attr_accessible :contents, :photo, :dimensions

  belongs_to    :story, :touch => true
  belongs_to    :user, :touch => true

  has_attached_file :photo, 
                    :styles => { 
                      :medium => { :geometry => "400x400>" },
                      :thumb => { :geometry => "100x100>" },
                    },
                    :processors => [:thumbnail],
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
                    :path => "/:style/:id/:filename"


  before_save   :extract_dimensions

  serialize   :dimensions

  validates   :contents,  :presence   => true,
                      :length         => {  :maximum => 399,
                                        :minimum => 5 } 
  validates   :user_id,   :presence => true

  validates_attachment_content_type :photo, 
    :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'],
    :message => "Sorry, we don't support that type of image format"

end

如您所见,帖子可能带有照片附件。我使用回形针来管理这些附件。

我使用 javascript/jquery 在客户端上生成动态发布这些帖子的表单。我的问题是这个。 . .如果帖子不包含照片附件,则一切正常。但是,如果帖子有照片附件,我会收到以下错误消息并且帖子不会发布:

WARNING: Can't verify CSRF token authenticity
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 61 LIMIT 1
   (0.3ms)  BEGIN
   (0.2ms)  COMMIT
Completed 401 Unauthorized in 238ms

结果,我的会话数据被破坏了,我什至看不到 Firebug 的请求标头。 put 请求根本不会出现在 firebug 中。

现在,毫不奇怪,我可以通过 PostController 中的以下内容解决这个问题:

skip_before_filter :verify_authenticity_token, :only => [:create]

但我不想放弃这种安全性。我还尝试通过 js/jquery 将 CSRF 标头添加到我的表单中:

jQuery.ajaxSetup({ 
  beforeSend: function(xhr) {
    xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-          
            token"]').attr('content'));
  }
});

但这并不能解决问题,而且正如我上面所说的,我什至无法看到请求头数据来查看请求头。

谁能想出回形针触发问题的原因?

【问题讨论】:

    标签: ruby-on-rails-3 ruby-on-rails-3.1 paperclip csrf


    【解决方案1】:

    我知道自从我第一次发布上述问题以来已经有一段时间了,但人们仍然在他们的搜索中找到它,所以我想我会更新一些答案。

    我上面讨论的问题与回形针无关。提交表单时没有 csrf 令牌,因为我正在使用 remotipart.js 来处理具有文件附件的表单的提交。 Remotipart 通过将表单数据复制到 i-frame 来启用类似 ajax 的表单提交,然后在您的站点保持活动状态时进行正常(即非 ajax)提交。有关通过 i-frame 上传 ajax 文件的更详细说明,请参阅this article

    在以前版本的 remotipart 中,csrf 令牌没有被复制到 i-frame 提交的表单中。支持remotipart的好心人现在已经解决了这个缺点。你可以找到修复here

    【讨论】:

      【解决方案2】:
      $.ajaxSetup({
          beforeSend: function(xhr) {
              xhr.setRequestHeader('X-CSRF-Token',
                                   $('meta[name="csrf-token"]').attr('content'));
          }
      });
      

      在js中

      在布局中

      <%= csrf_meta_tags %>
      

      文件应该足以让它工作。

      否则你可以使用处理 CSRF 令牌的jquery-rails gem

      【讨论】:

      • 大多数情况下,您只需要在布局中使用 - 在 部分。
      【解决方案3】:

      这是你需要解决的问题:

      https://github.com/JangoSteve/remotipart

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-08
        • 2014-07-12
        • 2017-12-14
        • 2018-05-10
        • 2019-03-22
        • 2022-10-06
        • 2011-09-28
        • 2018-02-01
        相关资源
        最近更新 更多