跳至内容 跳至搜索

Action Dispatch HostAuthorization

此中间件通过明确允许请求可以发送到的主机来防御 DNS 重绑定攻击,它接收在 config.host_authorization 中设置的选项。

请求可以通过 exclude 选择退出主机授权。

config.host_authorization = { exclude: ->(request) { request.path =~ /healthcheck/ } }

当请求到达未经授权的主机时,将执行并渲染 response_app 应用程序。如果未提供 response_app,则会运行一个默认的应用程序。默认响应应用程序以“error”级别记录被阻止的主机信息,并响应 403 Forbidden。如果 config.consider_all_requests_local 设置为 true,响应体将包含调试信息,否则响应体为空。

方法
C
N

常量

ALLOWED_HOSTS_IN_DEVELOPMENT = [".localhost", ".test", IPAddr.new("0.0.0.0/0"), IPAddr.new("::/0")]
 

类公共方法

new(app, hosts, exclude: nil, response_app: nil)

# File actionpack/lib/action_dispatch/middleware/host_authorization.rb, line 127
def initialize(app, hosts, exclude: nil, response_app: nil)
  @app = app
  @permissions = Permissions.new(hosts)
  @exclude = exclude

  @response_app = response_app || DefaultResponseApp.new
end

实例公共方法

call(env)

# File actionpack/lib/action_dispatch/middleware/host_authorization.rb, line 135
def call(env)
  return @app.call(env) if @permissions.empty?

  request = Request.new(env)
  hosts = blocked_hosts(request)

  if hosts.empty? || excluded?(request)
    mark_as_authorized(request)
    @app.call(env)
  else
    env["action_dispatch.blocked_hosts"] = hosts
    @response_app.call(env)
  end
end