【问题标题】:Uploadify and rails 3 authenticity tokensUploadify 和 rails 3 真实性令牌
【发布时间】:2011-02-11 00:55:03
【问题描述】:

我正在尝试使用 uploadify (http://www.uploadify.com) 让文件上传进度条在 rails 3 应用程序中工作,但我遇到了真实性令牌。我当前的上传配置看起来像

            <script type="text/javascript" charset="utf-8">
             $(document).ready(function() {
                   $("#zip_input").uploadify({
                    'uploader': '/flash/uploadify.swf',
                    'script': $("#upload").attr('action'),
                    'scriptData': { 'format': 'json', 'authenticity_token': encodeURIComponent('<%= form_authenticity_token if protect_against_forgery? %>') },
                    'fileDataName': "world[zip]",
                    //'scriptAccess': 'always', // Incomment this, if for some reason it doesn't work
                    'auto': true,
                    'fileDesc': 'Zip files only',
                    'fileExt': '*.zip',
                    'width': 120, 
                    'height': 24,
                    'cancelImg': '/images/cancel.png',
                    'onComplete': function(event, data) { $.getScript(location.href) }, // We assume that we can refresh the list by doing a js get on the current page
                   'displayData': 'speed'
                   });
                 });
            </script>

但是我从 rails 收到了这个回复:

Started POST "/worlds" for 127.0.0.1 at 2010-04-22 12:39:44

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):


Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (6.6ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (12.2ms)

这似乎是因为我没有随请求一起发送身份验证 cookie。有谁知道我如何获取我应该发送到那里的值,以及如何让 rails 从 HTTP POST 中读取它而不是尝试将其作为 cookie 来查找?

【问题讨论】:

    标签: ruby-on-rails cookies uploadify ruby-on-rails-3


    【解决方案1】:

    跳过真实性令牌检查并不理想,因为它会打开 XSS 攻击向量。 此处描述了完成这项工作的另一种方法:http://metautonomo.us/2010/07/09/uploadify-and-rails-3/

    请注意,您可能需要对 url 进行双重编码。在示例中,使用了 rails 'u' 以及 encodeURLComponent()。但是,如果您设置了更花哨的/rails3 类型并从页眉中的元标记获取会话数据/真实性令牌,则需要调用 encodeURLComponent() 两次。

    【讨论】:

    • 我对“+”字符的authentity_token 蜜蜂条有严重问题。你的建议解决了我的问题。做双 u() 是关键。 escapeURIComponent 和 u() 的工作方式有什么不同吗?
    【解决方案2】:

    这似乎是 rails 3 的错误。

    https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3913

    这意味着我必须更改跳过真实性令牌检查的方式:

    改变自

    protect_from_forgery :except => :upload
    

    skip_before_filter :verify_authenticity_token, :only => :upload
    

    这似乎仍然可以正常工作

    【讨论】:

      【解决方案3】:

      好吧,我想出了如何解决这个问题。视图上是否有要上传文件的表单。如果你只是使用 jquery 来获取隐藏的真实性令牌的值并将其传递给 scriptData var。

      var token = ($('input[name=authenticity_token]').val());
      scriptData : {'authenticity_token':token}
      

      希望这对你有用。

      【讨论】:

      • 感谢您的建议,我还没有机会重新审视这个项目,但您的建议看起来非常有用,一旦我有机会完成这项工作,我会来的还给你。
      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 2013-04-21
      • 2012-07-29
      • 2011-05-05
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多