ruby-on-rails – 如何防止force_ssl在重定向中破坏params?

我有以下路线:

resources :widgets do
  resources :orders
end

所以请求,例如to / widgets / 1 / orders / new转到OrderController,它可以访问params [:widget_id]以了解正在购买的小部件.

问题是:我在OrderController中使用force_ssl.这导致请求:

http://www.example.com/widgets/1/orders/new

重定向(302)到:

https://www.example.com/

换句话说,force_ssl正在执行其工作(重定向到URL的https协议版本),但正在销毁流程中路由的动态段指定的参数.我怎样才能防止这种情况发生(更可取)或以最不具攻击性的方式解决这个问题?

请注意,这是在Heroku上托管的,例如Apache重定向对我不起作用.

解决方法

我相信force_ssl的认行为是将参数从非安全连接传递到安全连接.如果这不是您想要的行为,您可以尝试通过添加如下的初始化程序来覆盖force_ssl函数

#
# Pass parameters in SSL redirects
#
module ActionController
  module ForceSSL
    module ClassMethods
      def force_ssl(options = {})
        host = options.delete(:host)
        before_filter(options) do
          if !request.ssl? && !Rails.env.development?

            secure_params = request.params.clone
            [:only,:except,:protocol,:status,:host].each {|s| secure_params.delete(s)}

            redirect_options = {:protocol => 'https://',:status => :moved_permanently}
            redirect_options.merge!(:host => host) if host
            redirect_to redirect_options.merge(secure_params)
          end
        end

      end
    end
  end
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...