Allow setting config.hosts to nil

Prior to #46858, uses can set `config.hosts = nil` to allow all hosts.
But now it causes `NoMethodError` because the check uses only `empty?`.

Given that the HostAuthorization itself allows taking `nil` as hosts,
I think this breaking change is not intentional. Therefore this commit
fixes the issue by also checking if the value is `nil`.
This commit is contained in:
Stan Lo 2023-01-07 14:33:35 +00:00
parent 481c169aa5
commit 56ecea0d4d
No known key found for this signature in database
GPG Key ID: 76D63D5E36F02747
2 changed files with 8 additions and 1 deletions

View File

@ -13,7 +13,7 @@ module Rails
def build_stack
ActionDispatch::MiddlewareStack.new do |middleware|
unless config.hosts.empty?
unless config.hosts.nil? || config.hosts.empty?
middleware.use ::ActionDispatch::HostAuthorization, config.hosts, **config.host_authorization
end

View File

@ -154,6 +154,13 @@ module ApplicationTests
assert_not_includes middleware, "ActionDispatch::HostAuthorization"
end
test "ActionDispatch::HostAuthorization is not included if hosts is set to nil" do
add_to_config "config.hosts = nil"
boot!
assert_not_includes middleware, "ActionDispatch::HostAuthorization"
end
test "Rack::Cache is not included by default" do
boot!