rails: config.hosts no longer supports underscores in host name and error message is incorrect

Steps to reproduce

Create an application with an underscore in the hostname (e.g. rails_example). Add that hostname to config.hosts in your config/environment.rb. Update your local hosts file to point your hostname to 127.0.0.1 so that your local dns will resolve your hostname to your locally running server. When navigating to https://rails_example:3000, you will see the following error:

Blocked host: rails_example
To allow requests to rails_example, add the following to your environment configuration:
config.hosts << "rails_example"
# config/environment.rb
Rails.application.configure do
...
# returns error saying to add "rails_example" to config.hosts
config.hosts << "rails_example"
...
end

This issue is due to an updated regex for hostname validation introduced in Rails 6.0.3.5 which no longer allows underscores in the hostname.

Expected behavior

User should receive a warning that the hostname they specified in config.hosts is not a valid hostname.

Actual behavior

User is told to add the same host name that they have already added to config.hosts.

System configuration

Rails version: 6.0.3.5

Ruby version: 2.6.6

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Hello team, i have followed every documention on the internet to solve this issues while the error still persists.

Blocked hosts: site.com To allow requests to these hosts, make sure they are valid hostnames (containing only numbers, letters, dashes and dots), then add the following to your environment configuration: config.hosts << “site.com”

For more details view: the Host Authorization guide.

how can i solve this am using rails 7

Realistically, while there are security concerns when not following the same spec as everyone else, it’s not a significant risk in Rails. It’s just not a “valid” domain by convention. Django does not allow underscores, but some other frameworks like Flask do (https://github.com/pallets/werkzeug/blob/main/src/werkzeug/urls.py#L25).

Keep in mind that by allowing underscores, users are likely to run into even more confusing issues. Such as:

  • being unable to get an SSL certificate for that domain
  • Cookies not being saved in IE7-IE11 (for the poor souls that still have to work with IE)
  • Going from development to a production server such as apache, underscores won’t work.
  • Issues with routing on the local network (_ is not at all valid in hostnames) depending on how strict the components of your particular network are.
  • You usually cannot register a public domain with an underscore in it.
  • Intranet sites will have issues depending on what’s handling the local DNS. For example in Windows Server you have to disable validation entirely for Microsoft’s “DNS Server” to reply to underscores.

If you want to re-enable this behavior by default, I believe it should still emit a warning on domains with an underscore at a minimum. Otherwise, it should stick to following convention and prevent domains with underscores.

I’d be happy to write a PR which shows a meaningful error message pointing out that the given URL is invalid. Waiting for the go from the core team before working on it.

Nice one! Ran into this issue this morning…