Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

No such file or directory @ rb_sysopen for external URL / Rails 6.11 / Ruby 3

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)

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.

question from:https://stackoverflow.com/questions/65937714/no-such-file-or-directory-rb-sysopen-for-external-url-rails-6-11-ruby-3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...