【问题标题】:Drawbacks and support for adopting a custom `redirect_to` method采用自定义 `redirect_to` 方法的缺点和支持
【发布时间】:2015-03-02 02:27:00
【问题描述】:

为了redirect users to a custom web page each time params contain a redirect_uri value,我正在评估以这种方式使用custom redirect_to method(@kiddorails):

class ApplicationController < ActionController::Base
  def custom_redirect_to(url)
    redirect_to (params[:redirect_uri].present? ? params[:redirect_uri] : url)
  end
end

def create
  @post = Post.new(params)
  if @post.save
    custom_redirect_to @post
  else
    render 'new'
  end
end

但是,我想了解可能存在的缺陷,并希望获得采用上述解决方案的支持。

【问题讨论】:

    标签: ruby-on-rails ruby redirect ruby-on-rails-4 routing


    【解决方案1】:

    允许通过 URL 参数设置重定向目标而不进行任何验证对您的用户来说可能是危险的,因为它使钓鱼尝试更容易。

    攻击者可以向您的用户发送链接,例如

    http://my-trusted-site.com/some/action/path?redirect_uri=malicious-site-that-looks-like-trusted-site.com

    ,许多用户在点击该链接后只会看到域部分,而无法意识到他们最终会到达哪里。

    Open Web Application Security Project (OWASP) 为此considers this a vulnerability:

    在 Web 应用程序中可能会出现未经验证的重定向和转发 接受可能导致 Web 应用程序 将请求重定向到包含在不受信任的输入中的 URL。经过 修改到恶意站点的不受信任的 URL 输入,攻击者可能 成功发起网络钓鱼诈骗并窃取用户凭据。 因为修改后的链接中的服务器名称与 原创网站,钓鱼尝试可能更值得信赖 外貌。也可以使用未经验证的重定向和转发攻击 恶意制作一个可以通过应用程序访问的 URL 控制检查,然后将攻击者转发给特权函数 他们通常无法访问。

    在执行重定向之前仔细检查redirect_uri参数很重要。

    但是由于正确的验证很棘手并且容易出错,所以更好的办法是首先不要接受 URI 参数,而是允许使用某些关键字来指定用户将被重定向到哪里。

    class ApplicationController < ActionController::Base
    
        protected
    
      def dynamic_redirect_to(default_route, options)
        options.stringify_keys!
    
        redirect_to options.fetch(params[:redirect_to], default_route)
      end
    end
    

    您现在可以预先定义任意数量的允许关键字,这些关键字可以用作?redirect_to= 参数:

    def create
      @post = Post.new(params)
      if @post.save
        dynamic_redirect_to post_path(@post), edit: edit_post_path(@post)
      else
        render 'new'
      end
    end
    

    如果设置了?redirect_to=edit,用户将被重定向回编辑页面。如果参数未设置或包含未指定的关键字,则会将她重定向到默认的post_path(@post)。

    【讨论】:

    • 我应该如何验证 URL?我应该对 URL 执行哪些类型的检查?
    • 这取决于你想让你的用户做什么。你能描述一下你的用户在做什么,以及你为什么要重定向他们吗?
    • 示例场景: 1) 更新帖子后,用户可以重定向到编辑操作(而不是帖子显示操作),具体取决于他是否选择了表单检查字段。 2) 用户在页面 A 上并单击链接以更改当前应用程序语言,因此可以访问语言表单。更新语言后,用户应该被重定向到页面 A(而不是语言显示操作)。
    • @Backo 在这种情况下,您不必接受任意 URI。您可以改为提供预定义的关键字,这样更安全。我已经用示例代码更新了我的答案。
    • 如果我验证redirect_uri 只包含path(也就是说,它不包含host),那么重定向就会发生在@987654331“内部” @域名?会更确定吗?
    【解决方案2】:

    我同意 janfoeh 上面所说的。 但是为了实现您的要求,我在Rails code of redirection 中进行了修改以使其更简单。

    制作一个文件config/initializers/redirecting.rb:

    require 'abstract_controller/base'
    module ActionController
      module Redirecting
        extend ActiveSupport::Concern
    
        include AbstractController::Logger
        include ActionController::RackDelegation
        include ActionController::UrlFor
    
        def _compute_redirect_to_location(options)
          if redirect_url = session.delete(:redirect_uri)
            options = redirect_url
          end
          case options
            # The scheme name consist of a letter followed by any combination of
            # letters, digits, and the plus ("+"), period ("."), or hyphen ("-")
            # characters; and is terminated by a colon (":").
            # See http://tools.ietf.org/html/rfc3986#section-3.1
            # The protocol relative scheme starts with a double slash "//".
          when /\A([a-z][a-z\d\-+\.]*:|\/\/).*/i
            options
          when String
            request.protocol + request.host_with_port + options
          when :back
            request.headers["Referer"] or raise RedirectBackError
          when Proc
            _compute_redirect_to_location request, options.call
          else
            url_for(options)
          end.delete("\0\r\n")
        end
      end
    end
    

    在您的 app/controllers/application_controller.rb 中:

    class ApplicationController < ActionController::Base
      # Prevent CSRF attacks by raising an exception.
      # For APIs, you may want to use :null_session instead.
    
      before_action :set_redirect_uri
      protect_from_forgery with: :exception
    
      def set_redirect_uri
        session[:redirect_uri] = params[:redirect_uri] if params[:redirect_uri].present?
      end
    end
    

    还有,瞧!现在您可以继续使用原来的redirect_to,并且只要在url 中提供redirect_uri,它就会在会话中设置该url 并自动覆盖。 :)

    注意:我只在调用redirect_to 时清除session[:redirect_uri]。您可以根据需要轻松修改该行为以重置此会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多