From 8c50571808a8e6788eae493c6ffa739749b0ead0 Mon Sep 17 00:00:00 2001 From: Debbie Milburn Date: Fri, 26 Feb 2021 19:13:16 +0000 Subject: [PATCH] Add RAILS_DEVELOPMENT_HOSTS to allow adding authorized hosts for development Co-authored-by: Josh Abernathy Update railties/CHANGELOG.md Co-authored-by: Gannon McGibbon Update railties/lib/rails/application/configuration.rb Co-authored-by: Gannon McGibbon Use yield block to safely alter env variables Remove trailing whitespace Update railties/CHANGELOG.md wording Co-authored-by: Marivaldo Cavalheiro Have comma separated list ignore spaces Add missing comma --- guides/source/configuring.md | 7 +-- railties/CHANGELOG.md | 4 ++ .../lib/rails/application/configuration.rb | 1 + .../test/application/configuration_test.rb | 46 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/guides/source/configuring.md b/guides/source/configuring.md index bce5bf9601..ed369fb16e 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -237,9 +237,10 @@ Every Rails application comes with a standard set of middleware which it uses in ```ruby Rails.application.config.hosts = [ - IPAddr.new("0.0.0.0/0"), # All IPv4 addresses. - IPAddr.new("::/0"), # All IPv6 addresses. - "localhost" # The localhost reserved domain. + IPAddr.new("0.0.0.0/0"), # All IPv4 addresses. + IPAddr.new("::/0"), # All IPv6 addresses. + "localhost", # The localhost reserved domain. + ENV["RAILS_DEVELOPMENT_HOSTS"] # Additional comma-separated hosts for development. ] ``` diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index c388528a04..43fedc50ee 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow adding additional authorized hosts in development via `ENV['RAILS_DEVELOPMENT_HOSTS']` + + *Josh Abernathy*, *Debbie Milburn* + * Stop generating a license for in-app plugins. *Gannon McGibbon* diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 32e1776bb4..453a284715 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -34,6 +34,7 @@ module Rails @filter_redirect = [] @helpers_paths = [] @hosts = Array(([".localhost", IPAddr.new("0.0.0.0/0"), IPAddr.new("::/0")] if Rails.env.development?)) + @hosts.concat(ENV["RAILS_DEVELOPMENT_HOSTS"].to_s.split(",").map(&:strip)) if Rails.env.development? @host_authorization = {} @public_file_server = ActiveSupport::OrderedOptions.new @public_file_server.enabled = true diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index c6f4100e9c..f3ee033a30 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -49,6 +49,14 @@ module ApplicationTests end end + def switch_development_hosts_to(*hosts) + old_development_hosts = ENV["RAILS_DEVELOPMENT_HOSTS"] + ENV["RAILS_DEVELOPMENT_HOSTS"] = hosts.join(",") + yield + ensure + ENV["RAILS_DEVELOPMENT_HOSTS"] = old_development_hosts + end + def setup build_app suppress_default_config @@ -2933,6 +2941,44 @@ module ApplicationTests assert_includes Rails.application.config.hosts, ".localhost" end + test "hosts reads multiple values from RAILS_DEVELOPMENT_HOSTS" do + host = "agoodhost.com" + another_host = "bananapants.com" + switch_development_hosts_to(host, another_host) do + app "development" + assert_includes Rails.application.config.hosts, host + assert_includes Rails.application.config.hosts, another_host + end + end + + test "hosts reads multiple values from RAILS_DEVELOPMENT_HOSTS and trims white space" do + host = "agoodhost.com" + host_with_white_space = " #{host} " + another_host = "bananapants.com" + another_host_with_white_space = " #{another_host}" + switch_development_hosts_to(host_with_white_space, another_host_with_white_space) do + app "development" + assert_includes Rails.application.config.hosts, host + assert_includes Rails.application.config.hosts, another_host + end + end + + test "hosts reads from RAILS_DEVELOPMENT_HOSTS" do + host = "agoodhost.com" + switch_development_hosts_to(host) do + app "development" + assert_includes Rails.application.config.hosts, host + end + end + + test "hosts does not read from RAILS_DEVELOPMENT_HOSTS in production" do + host = "agoodhost.com" + switch_development_hosts_to(host) do + app "production" + assert_not_includes Rails.application.config.hosts, host + end + end + test "disable_sandbox is false by default" do app "development"