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