有没有办法阻止Rails的内置服务器默认监听0.0.0.0?

Kev*_*gle 9 ruby security networking ruby-on-rails

我在不受信任的网络(coffeeshops,邻居的开放式wifi,DEF CON)上进行了大量的网络开发,当随机的,肯定有缺陷的软件(我正在开发的Rails应用程序)绑定0.0.0.0上的端口时,我会感到抽搐.开始接受所有人的请求.我知道我可以使用-b选项指定绑定到服务器的地址,但是我想全局更改默认值,因此它总是以这种方式运行,除非我告诉它.当然我也可以运行某种阻止连接的防火墙,但最好不要先听.是否存在'.railsrc'文件或类似文件 - 至少是每个项目的设置文件,但最好是一些全局设置文件 - 我可以使用它来强制服务器默认只绑定到127.0.0.1?

Sie*_*bbe 3

您可以更新 Rails 应用程序中的 /script/rails 文件以反映以下内容:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)

# START NEW CODE
require "rails/commands/server"
module Rails
  class Server
    def default_options
      super.merge({
        :Host        => 'my-host.com',
        :Port        => 3000,
        :environment => (ENV['RAILS_ENV'] || "development").dup,
        :daemonize   => false,
        :debugger    => false,
        :pid         => File.expand_path("tmp/pids/server.pid"),
        :config      => File.expand_path("config.ru")            
      })
    end
  end
end
# END NEW CODE

require 'rails/commands'
Run Code Online (Sandbox Code Playgroud)

这将在启动时将 Rails 应用程序绑定到 my-host.com。您仍然可以从命令行覆盖这些选项。

我不确定为什么 Rails::Server API 文档中没有反映这一点。您可以查看https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb查看服务器实现。

请注意,在 Rails 4 中,/script/rails 文件已移至/bin/rails。