外部 URL/Rails 6.11/Ruby 3 没有这样的文件或目录@rb_sysopen [英] No such file or directory @ rb_sysopen for external URL / Rails 6.11 / Ruby 3

查看:28
本文介绍了外部 URL/Rails 6.11/Ruby 3 没有这样的文件或目录@rb_sysopen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在 Rails 6 中运行良好,但现在将 Rails 和 Ruby 升级到 6.11 和 3,它触发了这个经典"错误.基本上,简而言之,例如这个外部 URL 直接指向 Firefox 徽标,使用 Rails 控制台:

This was working fine in Rails 6 but now upgrading Rails and Ruby to 6.11 and 3, it's triggering this "classic" error. Basically, to be short, for example this external URL direct to Firefox logo, using the Rails console:

require 'open-uri'
open("https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg")

Traceback (most recent call last):
        4: from (irb):1:in `<main>'
        3: from (irb):2:in `rescue in <main>'
        2: from (irb):2:in `open'
        1: from (irb):2:in `initialize'
Errno::ENOENT (No such file or directory @ rb_sysopen - https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg)

在应用中出现同样的错误.在 Rails 和 Ruby 升级(使用 Ruby 2.5.8 和 Rails 6.0.3.1)之前,这工作得很好.

In the app same error. Before the Rails and Ruby upgrade (with Ruby 2.5.8 and Rails 6.0.3.1) this was working perfectly fine.

推荐答案

open-uri 用于(在 Ruby 3.0 之前)用自己的版本覆盖 Kernel#open还支持从外部 URL 读取,而不是简单地打开本地文件或运行命令.

open-uri used to (before Ruby 3.0) overwrite Kernel#open with its own version which also supports reading from external URLs rather than simply opening local files or running commands.

如果不能确保传递的 URL 在任何地方都是安全的(包括使用 Kernel#open 的第三方代码),混合这两个用例是非常危险的,并且有可能产生严重的漏洞.

Mixing those two use-cases was quite dangerous and had the potential for serious vulnerabilities if the passed URL was not ensured to be safe everywhere (including third-party code using Kernel#open).

因此,这种覆盖 Kernel#open 的行为在 Ruby 2.7 中被弃用,最终在 Ruby 3.0 中被移除.要打开外部 URL,您可以改用以下代码:

As such, this behavior to overwrite Kernel#open was deprecated in Ruby 2.7 and finally removed in Ruby 3.0. To open an external URL, you can use the following code instead:

URI.open("https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg")

这篇关于外部 URL/Rails 6.11/Ruby 3 没有这样的文件或目录@rb_sysopen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆