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)
Links to this issue
Commits related to this issue
- Bring back support for underscores in config.hosts Fix: https://github.com/rails/rails/issues/41545 Ref: https://github.com/rails/rails/commit/83a6ac3fee8fd538ce7e0088913ff54f0f9bcb6f As far as I ca... — committed to byroot/rails by byroot 5 years ago
- Add more detail about what hosts are allowed Fixes https://github.com/rails/rails/issues/41545 — committed to ghiculescu/rails by ghiculescu 5 years ago
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:
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…