Upgraded Rails to 6, getting Blocked host Error

I needed the new function in ActiveStorage to resize_to_fill so I upgraded to Ruby 2.5.1 and Rails 6.

ruby '2.5.1'

gem "rails", github: "rails/rails"

When I stopped, then started my server (Cloud 9), I received the Rails error:

Blocked host: xxxxxxx-xxxxxxx.c9users.io To allow requests to xxxxxxx-xxxxxxx.c9users.io, add the following configuration:

Rails.application.config.hosts << "xxxxxxx-xxxxxxx.c9users.io"

I've tried restarting, new windows, but nothing gets rid of this. I've never seen this error before. I'm guessing the new version of Rails is doing something?


Solution 1:

The Blocked Host is a new feature of Rails 6. You can add this pattern to your config/environments/development.rb to have no worries of that in case of dynamic urls

config.hosts << /[a-z0-9]+\.c9users\.io/

Also for ngrok user, just replace above c9users by ngrok

Update: ngrok is currently using - in their URLs so this should be accurate config.hosts << /[a-z0-9-]+\.ngrok\.io/

Source: https://github.com/MikeRogers0/puma-ngrok-tunnel

Solution 2:

If you want to disable this functionality on your development environment, you can add config.hosts.clear to config/environments/development.rb.

Solution 3:

Simple solution:

Add this line to config/environments/development.rb

config.hosts << /[a-z0-9-]+\.ngrok\.io/

Restart your rails server and it will work


UPDATE

If you successfully used this regex in the past and it stopped working, that's because in the past few months, ngrok URLs started using - characters. The regex above has one additional character, and must be used in place of the old (very similar regex).

E.g. This works

config.hosts << /[a-z0-9-]+\.ngrok\.io/ # allows dashes

this will not work

config.hosts << /[a-z0-9]+\.ngrok\.io/ # subtly different and won't allow dashes

Make sure you're using the regex that does allow dashes!

Solution 4:

This article worked for me:

  1. The first option is to whitelist the hostnames in config/environments/development.rb:

    Rails.application.configure do
      config.hosts << "hostname" # Whitelist one hostname
      config.hosts << /application\.local\Z/ # Whitelist a test domain
    end
    
  2. The second option is to clear the entire whitelist, which lets through requests for all hostnames:

    Rails.application.configure do
      config.hosts.clear
    end
    

Credit goes to Manfred Stienstra.