Add RAILS_DEVELOPMENT_HOSTS to allow adding authorized hosts for development

Co-authored-by: Josh Abernathy <joshaber@gmail.com>

Update railties/CHANGELOG.md

Co-authored-by: Gannon McGibbon <gannon@hey.com>

Update railties/lib/rails/application/configuration.rb

Co-authored-by: Gannon McGibbon <gannon@hey.com>

Use yield block to safely alter env variables

Remove trailing whitespace

Update railties/CHANGELOG.md wording

Co-authored-by: Marivaldo Cavalheiro <marivaldo@gmail.com>

Have comma separated list ignore spaces

Add missing comma
This commit is contained in:
Debbie Milburn 2021-02-26 19:13:16 +00:00
parent 35e9812dfc
commit 8c50571808
4 changed files with 55 additions and 3 deletions

View File

@ -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.
]
```

View File

@ -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*

View File

@ -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

View File

@ -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"