How to disable "Cannot Render Console from..." on Rails

Ruby on-RailsVagrant

Ruby on-Rails Problem Overview


I'm using Ubuntu/vagrant as my development environment. I'm getting these messages on rails console:

Started GET "/assets/home-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for 10.0.2.2 at 2015-04-02 15:48:31 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

Is it possible to disable those "cannot render..." messages or allow them in any way?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You need to specifically allow the 10.0.2.2 network space in the Web Console config.

So you'll want something like this:

class Application < Rails::Application
  config.web_console.permissions = '10.0.2.2'
end

Read here for more information.

As pointed out by pguardiario, this wants to go into config/environments/development.rb rather than config/application.rb so it is only applied in your development environment.

Solution 2 - Ruby on-Rails

You can whitelist single IP's or whole networks.

Say you want to share your console with 192.168.0.100. You can do this:

class Application < Rails::Application
  config.web_console.whitelisted_ips = '192.168.0.100'
end

If you want to whitelist the whole private network, you can do:

class Application < Rails::Application
  config.web_console.whitelisted_ips = '192.168.0.0/16'
end
If you don't wanna see this message anymore, set this option to false:
class Application < Rails::Application
  config.web_console.whiny_requests = false
end
Be careful what you wish for, 'cause you might just get it all

This is probably only for development purposes so you might prefer to place it under config/environments/development.rb instead of config/application.rb.

Solution 3 - Ruby on-Rails

Hardcoding an IP into a configuration file isn't good. What about other devs? What if the ip changes?

Docker-related config should not leak into the rails app whenever possible. That's why you should use env vars in the config/environments/development.rb file:

class Application < Rails::Application
  # Check if we use Docker to allow docker ip through web-console
  if ENV['DOCKERIZED'] == 'true'
    config.web_console.whitelisted_ips = ENV['DOCKER_HOST_IP']
  end
end

You should set correct env vars in a .env file, not tracked into version control.

In docker-compose.yml you can inject env vars from this file with env_file:

app:
  build: .
  ports:
   - "3000:3000"
  volumes:
    - .:/app
  links:
    - db
  environment:
    - DOCKERIZED=true
  env_file:
    - ".env"

Based on the feebdack received in comments, we can also build a solution without environment variables:

class Application < Rails::Application
  # Check if we use Docker to allow docker ip through web-console
  if File.file?('/.dockerenv') == true
    host_ip = `/sbin/ip route|awk '/default/ { print $3 }'`.strip
    config.web_console.whitelisted_ips << host_ip
  end
end

I'll leave the solutions with env var for learning purposes.

Solution 4 - Ruby on-Rails

Auto discovery within your config/development.rb

config.web_console.whitelisted_ips = Socket.ip_address_list.reduce([]) do |res, addrinfo|
    addrinfo.ipv4? ? res << IPAddr.new(addrinfo.ip_address).mask(24) : res
end

Of course might need to add

require 'socket'
require 'ipaddr'

Within your file.

Solution 5 - Ruby on-Rails

Anyone on any of my private networks is welcome.

I run in a docker container and I don't care which network it wants to use this week.

config/environments/development.rb add line

config.web_console.whitelisted_ips = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']

Solution 6 - Ruby on-Rails

For development environment: Detect if it's docker, then determine the IP address and whitelist it

# config/environments/development.rb
require 'socket'
require 'ipaddr'

Rails.application.configure do
  ...

  # When inside a docker container
  if File.file?('/.dockerenv')
    # Whitelist docker ip for web console
    # Cannot render console from 172.27.0.1! Allowed networks: 127.0.0.1
    Socket.ip_address_list.each do |addrinfo|
      next unless addrinfo.ipv4?
      next if addrinfo.ip_address == "127.0.0.1" # Already whitelisted

      ip = IPAddr.new(addrinfo.ip_address).mask(24)

      Logger.new(STDOUT).info "Adding #{ip.inspect} to config.web_console.whitelisted_ips"

      config.web_console.whitelisted_ips << ip
    end
  end
end

For me this prints the following and the warning goes away 

Adding 172.27.0.0 to config.web_console.whitelisted_ips
Adding 172.18.0.0 to config.web_console.whitelisted_ips

My solution was to combine

Solution 7 - Ruby on-Rails

If you run your site locally (on the host) it generally works out, since 127.0.0.1 is always permitted. But if you're going to put your site into a container (not in production, locally), you might want to add this into config/environments/development.rb:

require 'socket'
require 'ipaddr'
Rails.application.configure do
  ...
  config.web_console.permissions = Socket.getifaddrs
    .select { |ifa| ifa.addr.ipv4_private? }
    .map { |ifa| IPAddr.new(ifa.addr.ip_address + '/' + ifa.netmask.ip_address) }
  ...
end

P.S. Most of the time you want it to whine (don't want to do config.web_console.whiny_requests = false). Because it might mean you're running web-console in production (which you shouldn't do).

Solution 8 - Ruby on-Rails

For me, whitelisted_ips didn't seem to work in a new project. The Readme states the corresponding configuration entry is supposed to be permissions now:

Rails.application.configure do
  config.web_console.permissions = '192.168.0.0/16'
end

https://github.com/rails/web-console/blob/master/README.markdown

Solution 9 - Ruby on-Rails

If you want to stop seeing this error message you can add this line in development.rb

config.web_console.whiny_requests = false

Solution 10 - Ruby on-Rails

If you are using Docker most likely you don't want neither to introduce new ENV variables nor to hardcode your specific IP address.

Instead you may want to check that you are in Docker using /proc/1/cgroup, and to allow your host IP (both for web_console and better_errors). Add to your config/environments/development.rb

  # https://stackoverflow.com/a/20012536/4862360
  if File.read('/proc/1/cgroup').include?('docker')
    # https://stackoverflow.com/a/24716645/4862360
    host_ip = `/sbin/ip route|awk '/default/ { print $3 }'`.strip

    BetterErrors::Middleware.allow_ip!(host_ip) if defined?(BetterErrors::Middleware)
    config.web_console.whitelisted_ips << host_ip
  end

Solution 11 - Ruby on-Rails

class Application < Rails::Application
  config.web_console.whitelisted_ips = %w( 0.0.0.0/0 ::/0 )
end

Solution 12 - Ruby on-Rails

Note that only the last 'config.web_console.whitelisted_ips' will be used. So

  config.web_console.whitelisted_ips = '10.0.2.2'
  config.web_console.whitelisted_ips = '192.168.0.0/16'

will only whitelist 192.168.0.0/16, not 10.0.2.2.

Instead, use:

  config.web_console.whitelisted_ips = ['10.0.2.2', '192.168.0.0/16']

Solution 13 - Ruby on-Rails

I just want to add this because my own mistake caused me to get the same error.

I was missing this from the controller

load_and_authorize_resource except: [:payment_webhook] 

Basically I was using cancancan to place authorization on that route, which was causing a 404 to be returned. I saw the message and assumed the two were related, when in fact they were not

Cannot render console from xxxxxx Allowed networks: xxxxx

So if you are getting an error message, it's possible that it has nothing to do with the Cannot render console from xxxxxx Allowed networks: xxxxx you see - look for other problems!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeandro Fran&#231;aView Question on Stackoverflow
Solution 1 - Ruby on-RailsydaetskcoRView Answer on Stackoverflow
Solution 2 - Ruby on-RailsFlavio WuenscheView Answer on Stackoverflow
Solution 3 - Ruby on-RailsPakView Answer on Stackoverflow
Solution 4 - Ruby on-RailsMeta LambdaView Answer on Stackoverflow
Solution 5 - Ruby on-RailskwerleView Answer on Stackoverflow
Solution 6 - Ruby on-RailsScymexView Answer on Stackoverflow
Solution 7 - Ruby on-Railsx-yuriView Answer on Stackoverflow
Solution 8 - Ruby on-RailsFabian KüblerView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSai Ram ReddyView Answer on Stackoverflow
Solution 10 - Ruby on-RailsAleksander RyhlitskiView Answer on Stackoverflow
Solution 11 - Ruby on-RailsDayvson LimaView Answer on Stackoverflow
Solution 12 - Ruby on-RailsdjmView Answer on Stackoverflow
Solution 13 - Ruby on-RailsstevecView Answer on Stackoverflow