8f07d4
commit fe3c49c9baeeab58304ede915b7edd18ecf360fc
8f07d4
Author: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8f07d4
Date:   Sat Jul 3 17:10:28 2021 +0000
8f07d4
8f07d4
    merge revision(s) b1c73f23,c9ab8fe2: [Backport #17877]
8f07d4
    
8f07d4
            [ruby/rdoc] Use File.open to fix the OS Command Injection vulnerability in CVE-2021-31799
8f07d4
    
8f07d4
            https://github.com/ruby/rdoc/commit/a7f5d6ab88
8f07d4
    
8f07d4
            The test for command injection on Unix platforms should be omitted on Windows
8f07d4
    
8f07d4
    
8f07d4
    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67947 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8f07d4
8f07d4
diff --git a/lib/rdoc/rdoc.rb b/lib/rdoc/rdoc.rb
8f07d4
index ca2c1abefd..46aace7839 100644
8f07d4
--- a/lib/rdoc/rdoc.rb
8f07d4
+++ b/lib/rdoc/rdoc.rb
8f07d4
@@ -436,7 +436,7 @@ def remove_unparseable files
8f07d4
     files.reject do |file|
8f07d4
       file =~ /\.(?:class|eps|erb|scpt\.txt|svg|ttf|yml)$/i or
8f07d4
         (file =~ /tags$/i and
8f07d4
-         open(file, 'rb') { |io|
8f07d4
+         File.open(file, 'rb') { |io|
8f07d4
            io.read(100) =~ /\A(\f\n[^,]+,\d+$|!_TAG_)/
8f07d4
          })
8f07d4
     end
8f07d4
--- a/lib/rdoc/encoding.rb	2022-02-16 16:51:28.080178281 +0100
8f07d4
+++ b/lib/rdoc/encoding.rb	2022-02-16 16:51:37.108160840 +0100
8f07d4
@@ -18,7 +18,7 @@
8f07d4
   # unknown character in the target encoding will be replaced with '?'
8f07d4
 
8f07d4
   def self.read_file filename, encoding, force_transcode = false
8f07d4
-    content = open filename, "rb" do |f| f.read end
8f07d4
+    content = File.open filename, "rb" do |f| f.read end
8f07d4
     content.gsub!("\r\n", "\n") if RUBY_PLATFORM =~ /mswin|mingw/
8f07d4
 
8f07d4
     utf8 = content.sub!(/\A\xef\xbb\xbf/, '')
8f07d4
--- a/lib/rdoc/parser.rb	2021-04-05 13:46:35.000000000 +0200
8f07d4
+++ b/lib/rdoc/parser.rb	2022-02-16 15:37:17.904822389 +0100
8f07d4
@@ -74,7 +74,12 @@
8f07d4
   def self.binary?(file)
8f07d4
     return false if file =~ /\.(rdoc|txt)$/
8f07d4
 
8f07d4
-    s = File.read(file, 1024) or return false
8f07d4
+    begin
8f07d4
+      open_file = File.open(file)
8f07d4
+      s = open_file.read(1024) or return false
8f07d4
+    ensure
8f07d4
+      open_file.close if open_file
8f07d4
+    end
8f07d4
 
8f07d4
     return true if s[0, 2] == Marshal.dump('')[0, 2] or s.index("\x00")
8f07d4
 
8f07d4
@@ -92,7 +97,8 @@
8f07d4
   # http://www.garykessler.net/library/file_sigs.html
8f07d4
 
8f07d4
   def self.zip? file
8f07d4
-    zip_signature = File.read file, 4
8f07d4
+    zip_signature = ''
8f07d4
+    File.open(file) { |f| zip_signature = f.read(4) }
8f07d4
 
8f07d4
     zip_signature == "PK\x03\x04" or
8f07d4
       zip_signature == "PK\x05\x06" or
8f07d4
diff --git a/test/rdoc/test_rdoc_rdoc.rb b/test/rdoc/test_rdoc_rdoc.rb
8f07d4
index 3bce54b243..123b1a4f87 100644
8f07d4
--- a/test/rdoc/test_rdoc_rdoc.rb
8f07d4
+++ b/test/rdoc/test_rdoc_rdoc.rb
8f07d4
@@ -366,6 +366,18 @@ def test_remove_unparseable_tags_vim
8f07d4
     end
8f07d4
   end
8f07d4
 
8f07d4
+  def test_remove_unparseable_CVE_2021_31799
8f07d4
+    skip 'for Un*x platforms' if Gem.win_platform?
8f07d4
+    temp_dir do
8f07d4
+      file_list = ['| touch evil.txt && echo tags']
8f07d4
+      file_list.each do |f|
8f07d4
+        FileUtils.touch f
8f07d4
+      end
8f07d4
+      assert_equal file_list, @rdoc.remove_unparseable(file_list)
8f07d4
+      assert_equal file_list, Dir.children('.')
8f07d4
+    end
8f07d4
+  end
8f07d4
+
8f07d4
   def test_setup_output_dir
8f07d4
     Dir.mktmpdir {|d|
8f07d4
       path = File.join d, 'testdir'