180

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?

13 Answers 13

221

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.

6
  • 8
    I think you only want the second line inside config/environments/development.rb, @ydaetskcoR Nov 4, 2015 at 16:56
  • 2
    For Vagrant in particular, something like this might also be good as the right side of the assignment: ENV.fetch('SSH_CLIENT', '127.0.0.1').split(' ').first. In general, this will probably be 10.0.2.2, but it should reflect whatever network configuration is active (vagrant or not, really -- which of course may or may not be what you want).
    – lindes
    Dec 17, 2017 at 22:56
  • 4
    There's two different things happening here. the first is the web console being rendered on your local machine when rails is running in a vagrant box. This is controlled by config.web_console.whitelisted_ips. The second is the error messages that you are seeing in your logs. This is controlled by config.web_console.whiny_requests. Finally, and this was the issue that I faced, the whitelist Ip error was caused because rails was trying to render the console as a default mechanism to handle another error. So either fixing the other error, or changing the default should also help.
    – kapad
    Jul 5, 2018 at 15:17
  • 1
    You generally don't want to hardcode things. See my answer.
    – x-yuri
    Nov 4, 2019 at 23:15
  • 3
    Note that since version 4 (latest), the syntax is config.web_console.permissions, not config.web_console.whitelisted_ips. See github.com/rails/web-console/commit/…
    – stwr667
    Dec 9, 2019 at 7:01
98

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.

1
  • I use the OS X "computer name" feature under System Prefs > Sharing and bind the Webrick source IP to a alphabetical name (e.g., myname.local:3000), however, Webrick won't start up when I attempt to whitelist this. Any suggestions?
    – nipponese
    Jan 23, 2016 at 3:28
57

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.

8
  • 1
    My DOCKER_HOST_IP env var is not set. Any idea what could have changed since feb 22?
    – dennis-tra
    May 15, 2016 at 12:45
  • You should specify it yourself in your environment file.
    – Pak
    May 15, 2016 at 13:04
  • 1
    @BrianKung I believe it's okay : .env should not be checked into version control, anyone may override it in its own environment. The docker information leaks anyway into the app, we just minimize the damage here :)
    – Pak
    Jul 5, 2016 at 22:04
  • 1
    Perfect, I just learned about the env_file and environment options in docker-compose.yml from your answer, too. 👍
    – Brian Kung
    Jul 5, 2016 at 22:49
  • 7
    No need to create the DOCKERIZED-env variable. Docker creates a /.dockerenv-file, which you can check for: File.file?('/.dockerenv') => true and you're inside a container.
    – jottr
    Nov 18, 2017 at 14:07
28

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.

7
  • 2
    Best answer - just newer then the rest
    – Jono
    Sep 13, 2017 at 17:23
  • this seems to work excellently for me as I'm running Rails in a Docker container
    – FireDragon
    Jun 12, 2018 at 20:45
  • Personally, I would prefer readability of a select + map combination: config.web_console.whitelisted_ips = Socket.ip_address_list.select(&:ipv4?).map{ |addrinfo| IPAddr.new(addrinfo.ip_address).mask(24) }
    – Alexis
    Sep 17, 2019 at 8:11
  • 2
    also, why is this better than simple config.web_console.whitelisted_ips = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'] from @kwerle's answer?
    – Alexis
    Sep 17, 2019 at 8:15
  • I also want to know. Why is this better than the simple config?
    – Anwar
    Oct 29, 2019 at 4:28
22

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']
8

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

1
  • 1
    Thank you for your answer! For me this code returned: undefined method <<' for nil:NilClass (NoMethodError). So I created a variable called whitelisted_ips = [ ], used it inside the loop adding the ips, and after the loop: config.web_console.whitelisted_ips = whitelisted_ips and then it worked for me! So, thanks! Dec 11, 2019 at 2:55
8

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).

6

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

4

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

config.web_console.whiny_requests = false
4

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']
3
class Application < Rails::Application
  config.web_console.whitelisted_ips = %w( 0.0.0.0/0 ::/0 )
end
1
  • 1
    Does this whitelist all IPs? Where do you put this code? Nov 14, 2019 at 3:23
3

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
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.