跳至內容 跳至搜尋

Action Dispatch HostAuthorization

此中間件透過明確允許可傳送要求的主機,防範 DNS 重新繫結攻擊,並傳遞 config.host_authorization 中設定的選項。

要求可以使用 exclude 選擇不使用主機授權。

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

當要求傳送至未授權的主機時,將會執行並呈現 response_app 應用程式。如果未提供 response_app,將會執行預設應用程式。預設回應應用程式會以「錯誤」等級記錄封鎖主機資訊,並回應 403 禁止。如果將 config.consider_all_requests_local 設定為 true,回應本文會包含偵錯資訊;否則,本文會為空。

方法
C
N

常數

ALLOWED_HOSTS_IN_DEVELOPMENT = [".localhost", 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 125
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 133
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