Adds a fix to whitelist hostnames with case-insensitive matching

This commit is contained in:
Sandip Mane 2020-08-14 14:05:16 +05:30
parent 601006c56d
commit 9bc7ea5dab
2 changed files with 46 additions and 2 deletions

View File

@ -46,9 +46,9 @@ module ActionDispatch
def sanitize_string(host)
if host.start_with?(".")
/\A(.+\.)?#{Regexp.escape(host[1..-1])}\z/
/\A(.+\.)?#{Regexp.escape(host[1..-1])}\z/i
else
host
/\A#{host}\z/i
end
end
end

View File

@ -42,6 +42,50 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
assert_equal "Success", body
end
test "hosts are matched case insensitive" do
@app = ActionDispatch::HostAuthorization.new(App, "Example.local")
get "/", env: {
"HOST" => "example.local",
}
assert_response :ok
assert_equal "Success", body
end
test "hosts are matched case insensitive with titlecased host" do
@app = ActionDispatch::HostAuthorization.new(App, "example.local")
get "/", env: {
"HOST" => "Example.local",
}
assert_response :ok
assert_equal "Success", body
end
test "hosts are matched case insensitive with hosts array" do
@app = ActionDispatch::HostAuthorization.new(App, ["Example.local"])
get "/", env: {
"HOST" => "example.local",
}
assert_response :ok
assert_equal "Success", body
end
test "regex matches are not title cased" do
@app = ActionDispatch::HostAuthorization.new(App, [/www.Example.local/])
get "/", env: {
"HOST" => "www.example.local",
}
assert_response :forbidden
assert_match "Blocked host: www.example.local", response.body
end
test "passes requests to allowed hosts with domain name notation" do
@app = ActionDispatch::HostAuthorization.new(App, ".example.com")