将 Rails 升级到 6,出现 Blocked host 错误

问题描述 投票:0回答:14

我需要 ActiveStorage 中的新函数来 resize_to_fill,因此我升级到 Ruby 2.5.1 和 Rails 6。

ruby '2.5.1'

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

当我停止并重新启动我的服务器(Cloud 9)时,我收到以下 Rails 错误:

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"

我尝试过重新启动、新窗口,但没有任何效果。我以前从未见过这个错误。我猜测新版本的 Rails 正在做一些事情?

ruby-on-rails cloud9 ruby-on-rails-6
14个回答
134
投票

被阻止的主机是 Rails 6 的一项新功能。您可以将此模式添加到您的

config/environments/development.rb
中,这样在动态 url 的情况下就不用担心了

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

对于 ngrok 用户,只需将上面的

c9users
替换为
ngrok

更新: ngrok 目前使用

-
.
作为其 URL 中的子域,因此这应该是准确的
config.hosts << /[a-z0-9-.]+\.ngrok\.io/

来源:https://github.com/MikeRogers0/puma-ngrok-tunnel


83
投票

如果您想在您的开发环境中禁用此功能,您可以将

config.hosts.clear
添加到
config/environments/development.rb


43
投票

这些行添加到

config/environments/development.rb

  config.hosts << /.*\.ngrok\.io/
  config.hosts << /.*\.ngrok-free\.app/

重新启动你的rails服务器,它就会工作了

注意:自 2023 年起,ngrok 现在需要身份验证令牌。如果您尚未设置,请转到此处,登录,然后将其提供的行复制到您的终端:

ngrok config add-authtoken <your token>

您应该在几分钟内起床并开始工作。如果您遇到困难,这里有一个很好的解释器这里


16
投票

这篇文章对我有用:

  1. 第一个选项是将

    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. 第二个选项是清除整个白名单,这样可以通过所有主机名的请求:

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

归功于Manfred Stienstra


16
投票

要允许来自

ngrok.io
(或其他服务)的任何子域的请求,最简单的解决方案是在其前面加上
.
,如下所示:

# config/environments/development.rb

Rails.application.configure do

  ...

  config.hosts << '.ngrok.io'
end

不需要像其他答案中提到的那样对子域使用正则表达式。

PS:不要像其他答案中提到的那样通过执行

config.hosts.clear
来禁用此功能,因为这违背了 Rails 的 DNS 重新绑定保护的目的,并且在适当的情况下,外部攻击者可以获得对本地 Rails 应用程序的完全访问权限信息(来源)。


5
投票

在 Rails 6 Action Pack 中引入了 ActionDispatch::HostAuthorization,默认情况下仅允许 [IPAddr.new(“0.0.0.0/0”)、IPAddr.new(“::/0”)、“localhost”]

您可以像这样在文件 config/application.rb 中添加 RegExp、Proc、IPAddr 和 String 的数组或单个 String

class Application < Rails::Application
  config.hosts << "xxxxxxx-xxxxxxx.c9users.io"
  ...
end

来自“https://drivy.engineering/rails-6-unnoticed-features”:

Rails 6 添加了一个新的中间件,称为 ActionDispatch::HostAuthorization 允许您将某些主机列入白名单 为您的应用程序并防止主机标头攻击。你可以 使用 String、IPAddr、Proc 和 RegExp 轻松配置它(有用 处理通配符域时)。


5
投票

我将

Rails.application.config.hosts << "xxxxxxx-xxxxxxx.c9users.io"
添加到
config/application.rb
,它很好地修复了我的测试应用程序。然后我对我真正的应用程序做了它,它也有效。问题是,Devise 也抛出了一个错误,显然至少要到 Rails 6 beta 才能修复。我想我会回到
Carrierwave
来满足我的图像尺寸需求,直到
ActiveStorage
更加成熟。


4
投票

Rails 6
中,当您想要允许来自
ngrok v2.3.40
的主机时,请将此配置添加到
config/environments/development.rb

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

重启服务器即可享受


4
投票

将此行添加到 config/environments/development.rb

config.hosts << /.+\.ngrok\.io:\d+/

我看到的大多数回复都缺少 URL 的 port 部分。如果您在特定端口(通常为

:3000
)访问此 URL,则正则表达式的
:\d+
部分是必需的。

重启服务器后即可工作。


4
投票

config.hosts = nil

development.rb
中使用它并重新启动你的 Rails 服务器,它对我有用,它会起作用。


2
投票

为了支持 ngrok 子域名和区域中的连字符,您需要将

config/environments/development.rb
更改
config.hosts
/[a-z0-9.-]+.ngrok.io/

示例:

  config.hosts = (config.hosts rescue []) << /[a-z0-9.-]+.ngrok.io/

1
投票

注意:您可以使用配置将您的主机列入白名单

application.config.hosts << 'your_unvalid_host_name'
,但仍然有错误。 在这种情况下,错误消息目前不准确。请参阅此问题。 您不应使用带下划线的主机名。 注意:在这种情况下,
application.config.hosts.clear
起作用。


0
投票

rails 7 和 8 的方法是通过:

config.host_authorization = { exclude: ->(request) { request.path =~ /healthcheck/ } }

或者如果您想使用主机而不是路径

config.host_authorization = { exclude: ->(request) { request.host =~ /ngrok/ } }

请参阅文档


-1
投票

第一次在其中一个终端运行 ngrok 3000,然后打开新终端并运行 Rails s...然后您可以看到现在 ngrok 和 Rails s 都可以同时运行...

© www.soinside.com 2019 - 2024. All rights reserved.