50eebb
From f86e5daee790ee509cb17f4f51f95cc76ca89a4e Mon Sep 17 00:00:00 2001
50eebb
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
50eebb
Date: Mon, 18 Mar 2019 18:30:36 +0000
50eebb
Subject: [PATCH] Applied security patches for RubyGems
50eebb
50eebb
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@67303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
50eebb
---
50eebb
 lib/rubygems/command_manager.rb        |  10 ++-
50eebb
 lib/rubygems/commands/owner_command.rb |   5 +-
50eebb
 lib/rubygems/gemcutter_utilities.rb    |   8 +-
50eebb
 lib/rubygems/installer.rb              |  29 +++++--
50eebb
 lib/rubygems/user_interaction.rb       |   8 +-
50eebb
 test/rubygems/test_gem_installer.rb    | 108 +++++++++++++++++++++++++
50eebb
 test/rubygems/test_gem_text.rb         |   5 ++
50eebb
 7 files changed, 159 insertions(+), 14 deletions(-)
50eebb
50eebb
diff --git a/lib/rubygems/command_manager.rb b/lib/rubygems/command_manager.rb
50eebb
index 451b719c4683..d3ff6614dc47 100644
50eebb
--- a/lib/rubygems/command_manager.rb
50eebb
+++ b/lib/rubygems/command_manager.rb
50eebb
@@ -6,6 +6,7 @@
50eebb
 
50eebb
 require 'rubygems/command'
50eebb
 require 'rubygems/user_interaction'
50eebb
+require 'rubygems/text'
50eebb
 
50eebb
 ##
50eebb
 # The command manager registers and installs all the individual sub-commands
50eebb
@@ -31,6 +32,7 @@
50eebb
 
50eebb
 class Gem::CommandManager
50eebb
 
50eebb
+  include Gem::Text
50eebb
   include Gem::UserInteraction
50eebb
 
50eebb
   ##
50eebb
@@ -129,7 +131,7 @@ def command_names
50eebb
   def run(args, build_args=nil)
50eebb
     process_args(args, build_args)
50eebb
   rescue StandardError, Timeout::Error => ex
50eebb
-    alert_error "While executing gem ... (#{ex.class})\n    #{ex.to_s}"
50eebb
+    alert_error clean_text("While executing gem ... (#{ex.class})\n    #{ex}")
50eebb
     ui.backtrace ex
50eebb
 
50eebb
     if Gem.configuration.really_verbose and \
50eebb
@@ -142,7 +144,7 @@ def run(args, build_args=nil)
50eebb
 
50eebb
     terminate_interaction(1)
50eebb
   rescue Interrupt
50eebb
-    alert_error "Interrupted"
50eebb
+    alert_error clean_text("Interrupted")
50eebb
     terminate_interaction(1)
50eebb
   end
50eebb
 
50eebb
@@ -162,7 +164,7 @@ def process_args(args, build_args=nil)
50eebb
       say Gem::VERSION
50eebb
       terminate_interaction 0
50eebb
     when /^-/ then
50eebb
-      alert_error "Invalid option: #{args.first}.  See 'gem --help'."
50eebb
+      alert_error clean_text("Invalid option: #{args.first}. See 'gem --help'.")
50eebb
       terminate_interaction 1
50eebb
     else
50eebb
       cmd_name = args.shift.downcase
50eebb
@@ -211,7 +213,7 @@ def load_and_instantiate(command_name)
50eebb
     rescue Exception => e
50eebb
       e = load_error if load_error
50eebb
 
50eebb
-      alert_error "Loading command: #{command_name} (#{e.class})\n\t#{e}"
50eebb
+      alert_error clean_text("Loading command: #{command_name} (#{e.class})\n\t#{e}")
50eebb
       ui.backtrace e
50eebb
     end
50eebb
   end
50eebb
diff --git a/lib/rubygems/commands/owner_command.rb b/lib/rubygems/commands/owner_command.rb
50eebb
index 2ee7f84462c1..7842a322cfce 100644
50eebb
--- a/lib/rubygems/commands/owner_command.rb
50eebb
+++ b/lib/rubygems/commands/owner_command.rb
50eebb
@@ -1,8 +1,11 @@
50eebb
 require 'rubygems/command'
50eebb
 require 'rubygems/local_remote_options'
50eebb
 require 'rubygems/gemcutter_utilities'
50eebb
+require 'rubygems/text'
50eebb
 
50eebb
 class Gem::Commands::OwnerCommand < Gem::Command
50eebb
+
50eebb
+  include Gem::Text
50eebb
   include Gem::LocalRemoteOptions
50eebb
   include Gem::GemcutterUtilities
50eebb
 
50eebb
@@ -48,7 +51,7 @@ def show_owners name
50eebb
     end
50eebb
 
50eebb
     with_response response do |resp|
50eebb
-      owners = YAML.load resp.body
50eebb
+      owners = Gem::SafeYAML.load clean_text(resp.body)
50eebb
 
50eebb
       say "Owners for gem: #{name}"
50eebb
       owners.each do |owner|
50eebb
diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb
50eebb
index 7c6d6bb36404..623d9301b598 100644
50eebb
--- a/lib/rubygems/gemcutter_utilities.rb
50eebb
+++ b/lib/rubygems/gemcutter_utilities.rb
50eebb
@@ -1,6 +1,10 @@
50eebb
 require 'rubygems/remote_fetcher'
50eebb
+require 'rubygems/text'
50eebb
 
50eebb
 module Gem::GemcutterUtilities
50eebb
+
50eebb
+  include Gem::Text
50eebb
+
50eebb
   # TODO: move to Gem::Command
50eebb
   OptionParser.accept Symbol do |value|
50eebb
     value.to_sym
50eebb
@@ -93,13 +97,13 @@ def with_response response, error_prefix = nil
50eebb
       if block_given? then
50eebb
         yield resp
50eebb
       else
50eebb
-        say resp.body
50eebb
+        say clean_text(resp.body)
50eebb
       end
50eebb
     else
50eebb
       message = resp.body
50eebb
       message = "#{error_prefix}: #{message}" if error_prefix
50eebb
 
50eebb
-      say message
50eebb
+      say clean_text(message)
50eebb
       terminate_interaction 1 # TODO: question this
50eebb
     end
50eebb
   end
50eebb
diff --git a/lib/rubygems/installer.rb b/lib/rubygems/installer.rb
50eebb
index 6fd3399dd44c..5818b94fb5f8 100644
50eebb
--- a/lib/rubygems/installer.rb
50eebb
+++ b/lib/rubygems/installer.rb
50eebb
@@ -596,9 +596,26 @@ def verify_gem_home(unpack = false) # :nodoc:
50eebb
       unpack or File.writable?(gem_home)
50eebb
   end
50eebb
 
50eebb
-  def verify_spec_name
50eebb
-    return if spec.name =~ Gem::Specification::VALID_NAME_PATTERN
50eebb
-    raise Gem::InstallError, "#{spec} has an invalid name"
50eebb
+  def verify_spec
50eebb
+    unless spec.name =~ Gem::Specification::VALID_NAME_PATTERN
50eebb
+      raise Gem::InstallError, "#{spec} has an invalid name"
50eebb
+    end
50eebb
+
50eebb
+    if spec.require_paths.any?{|path| path =~ /\r\n|\r|\n/ }
50eebb
+      raise Gem::InstallError, "#{spec} has an invalid require_paths"
50eebb
+    end
50eebb
+
50eebb
+    if spec.extensions.any?{|ext| ext =~ /\r\n|\r|\n/ }
50eebb
+      raise Gem::InstallError, "#{spec} has an invalid extensions"
50eebb
+    end
50eebb
+
50eebb
+    unless spec.specification_version.to_s =~ /\A\d+\z/
50eebb
+      raise Gem::InstallError, "#{spec} has an invalid specification_version"
50eebb
+    end
50eebb
+
50eebb
+    if spec.dependencies.any? {|dep| dep.type =~ /\r\n|\r|\n/ || dep.name =~ /\r\n|\r|\n/ }
50eebb
+      raise Gem::InstallError, "#{spec} has an invalid dependencies"
50eebb
+    end
50eebb
   end
50eebb
 
50eebb
   ##
50eebb
@@ -770,9 +787,11 @@ def dir
50eebb
     @security_policy = nil if
50eebb
       @force and @security_policy and not @security_policy.only_signed
50eebb
 
50eebb
+    # The name and require_paths must be verified first, since it could contain
50eebb
+    # ruby code that would be eval'ed in #ensure_loadable_spec
50eebb
+    verify_spec
50eebb
+
50eebb
     ensure_loadable_spec
50eebb
-
50eebb
-    verify_spec_name
50eebb
 
50eebb
     Gem.ensure_gem_subdirectories gem_home
50eebb
 
50eebb
diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb
50eebb
index 390d0f2aea72..237ae2bc71c2 100644
50eebb
--- a/lib/rubygems/user_interaction.rb
50eebb
+++ b/lib/rubygems/user_interaction.rb
50eebb
@@ -4,11 +4,15 @@
50eebb
 # See LICENSE.txt for permissions.
50eebb
 #++
50eebb
 
50eebb
+require 'rubygems/text'
50eebb
+
50eebb
 ##
50eebb
 # Module that defines the default UserInteraction.  Any class including this
50eebb
 # module will have access to the +ui+ method that returns the default UI.
50eebb
 
50eebb
 module Gem::DefaultUserInteraction
50eebb
+
50eebb
+  include Gem::Text
50eebb
 
50eebb
   ##
50eebb
   # The default UI is a class variable of the singleton class for this
50eebb
@@ -124,8 +128,8 @@ def terminate_interaction exit_code = 0
50eebb
   # Calls +say+ with +msg+ or the results of the block if really_verbose
50eebb
   # is true.
50eebb
 
50eebb
-  def verbose msg = nil
50eebb
-    say(msg || yield) if Gem.configuration.really_verbose
50eebb
+  def verbose(msg = nil)
50eebb
+    say(clean_text(msg || yield)) if Gem.configuration.really_verbose
50eebb
   end
50eebb
 end
50eebb
 
50eebb
diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb
50eebb
index dd049214fbb8..af4573cde8d2 100644
50eebb
--- a/test/rubygems/test_gem_installer.rb
50eebb
+++ b/test/rubygems/test_gem_installer.rb
50eebb
@@ -1222,6 +1222,114 @@ def spec.validate; end
50eebb
     end
50eebb
   end
50eebb
 
50eebb
+  def test_pre_install_checks_malicious_name_before_eval
50eebb
+    spec = util_spec "malicious\n::Object.const_set(:FROM_EVAL, true)#", '1'
50eebb
+    def spec.full_name # so the spec is buildable
50eebb
+      "malicious-1"
50eebb
+    end
50eebb
+    def spec.validate(*args); end
50eebb
+
50eebb
+    util_build_gem spec
50eebb
+
50eebb
+    gem = File.join(@gemhome, 'cache', spec.file_name)
50eebb
+
50eebb
+    use_ui @ui do
50eebb
+      @installer = Gem::Installer.new gem
50eebb
+      e = assert_raises Gem::InstallError do
50eebb
+        @installer.pre_install_checks
50eebb
+      end
50eebb
+      assert_equal "#<Gem::Specification name=malicious\n::Object.const_set(:FROM_EVAL, true)# version=1> has an invalid name", e.message
50eebb
+    end
50eebb
+    refute defined?(::Object::FROM_EVAL)
50eebb
+  end
50eebb
+
50eebb
+  def test_pre_install_checks_malicious_require_paths_before_eval
50eebb
+    spec = util_spec "malicious", '1'
50eebb
+    def spec.full_name # so the spec is buildable
50eebb
+      "malicious-1"
50eebb
+    end
50eebb
+    def spec.validate(*args); end
50eebb
+    spec.require_paths = ["malicious\n``"]
50eebb
+
50eebb
+    util_build_gem spec
50eebb
+
50eebb
+    gem = File.join(@gemhome, 'cache', spec.file_name)
50eebb
+
50eebb
+    use_ui @ui do
50eebb
+      @installer = Gem::Installer.new gem
50eebb
+      e = assert_raises Gem::InstallError do
50eebb
+        @installer.pre_install_checks
50eebb
+      end
50eebb
+      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid require_paths", e.message
50eebb
+    end
50eebb
+  end
50eebb
+
50eebb
+  def test_pre_install_checks_malicious_extensions_before_eval
50eebb
+    skip "mswin environment disallow to create file contained the carriage return code." if Gem.win_platform?
50eebb
+
50eebb
+    spec = util_spec "malicious", '1'
50eebb
+    def spec.full_name # so the spec is buildable
50eebb
+      "malicious-1"
50eebb
+    end
50eebb
+    def spec.validate(*args); end
50eebb
+    spec.extensions = ["malicious\n``"]
50eebb
+
50eebb
+    util_build_gem spec
50eebb
+
50eebb
+    gem = File.join(@gemhome, 'cache', spec.file_name)
50eebb
+
50eebb
+    use_ui @ui do
50eebb
+      @installer = Gem::Installer.new gem
50eebb
+      e = assert_raises Gem::InstallError do
50eebb
+        @installer.pre_install_checks
50eebb
+      end
50eebb
+      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid extensions", e.message
50eebb
+    end
50eebb
+  end
50eebb
+
50eebb
+  def test_pre_install_checks_malicious_specification_version_before_eval
50eebb
+    spec = util_spec "malicious", '1'
50eebb
+    def spec.full_name # so the spec is buildable
50eebb
+      "malicious-1"
50eebb
+    end
50eebb
+    def spec.validate(*args); end
50eebb
+    spec.specification_version = "malicious\n``"
50eebb
+
50eebb
+    util_build_gem spec
50eebb
+
50eebb
+    gem = File.join(@gemhome, 'cache', spec.file_name)
50eebb
+
50eebb
+    use_ui @ui do
50eebb
+      @installer = Gem::Installer.new gem
50eebb
+      e = assert_raises Gem::InstallError do
50eebb
+        @installer.pre_install_checks
50eebb
+      end
50eebb
+      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid specification_version", e.message
50eebb
+    end
50eebb
+  end
50eebb
+
50eebb
+  def test_pre_install_checks_malicious_dependencies_before_eval
50eebb
+    spec = util_spec "malicious", '1'
50eebb
+    def spec.full_name # so the spec is buildable
50eebb
+      "malicious-1"
50eebb
+    end
50eebb
+    def spec.validate(*args); end
50eebb
+    spec.add_dependency "b\nfoo", '> 5'
50eebb
+
50eebb
+    util_build_gem spec
50eebb
+
50eebb
+    gem = File.join(@gemhome, 'cache', spec.file_name)
50eebb
+
50eebb
+    use_ui @ui do
50eebb
+      @installer = Gem::Installer.new gem
50eebb
+      @installer.ignore_dependencies = true
50eebb
+      e = assert_raises Gem::InstallError do
50eebb
+        @installer.pre_install_checks
50eebb
+      end
50eebb
+      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid dependencies", e.message
50eebb
+    end
50eebb
+  end
50eebb
+
50eebb
   def test_shebang
50eebb
     util_make_exec @spec, "#!/usr/bin/ruby"
50eebb
 
50eebb
diff --git a/test/rubygems/test_gem_text.rb b/test/rubygems/test_gem_text.rb
50eebb
index 04f3f605e8c0..8ce6df94bbc0 100644
50eebb
--- a/test/rubygems/test_gem_text.rb
50eebb
+++ b/test/rubygems/test_gem_text.rb
50eebb
@@ -66,4 +66,9 @@ def test_truncate_text
50eebb
     s = "ab" * 500_001
50eebb
     assert_equal "Truncating desc to 1,000,000 characters:\n#{s[0, 1_000_000]}", truncate_text(s, "desc", 1_000_000)
50eebb
   end
50eebb
+
50eebb
+  def test_clean_text
50eebb
+    assert_equal ".]2;nyan.", clean_text("\e]2;nyan\a")
50eebb
+  end
50eebb
+
50eebb
 end