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