8ca061
From 97c6e3934c68e90592f6913f68861d0dbc49c6a4 Mon Sep 17 00:00:00 2001
8ca061
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8ca061
Date: Sun, 10 Sep 2017 01:10:24 +0000
8ca061
Subject: [PATCH] * lib/rubygems: fix several vulnerabilities in RubyGems; bump
8ca061
 to version   2.4.5.3. [Backport #13842]
8ca061
8ca061
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@59805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8ca061
---
8ca061
 ChangeLog                                        |    4 +
8ca061
 lib/rubygems/commands/query_command.rb           |    5 -
8ca061
 lib/rubygems/installer.rb                        |    7 ++
8ca061
 lib/rubygems/remote_fetcher.rb                   |    2 +-
8ca061
 lib/rubygems/specification.rb                    |   12 ++-
8ca061
 lib/rubygems/text.rb                             |   15 ++++
8ca061
 test/rubygems/test_gem_commands_query_command.rb |   80 +++++++++++++++++++++++
8ca061
 test/rubygems/test_gem_installer.rb              |   32 +++++++++
8ca061
 test/rubygems/test_gem_remote_fetcher.rb         |   15 ++++
8ca061
 test/rubygems/test_gem_specification.rb          |   32 ++++++++-
8ca061
 test/rubygems/test_gem_text.rb                   |   11 +++
8ca061
 11 files changed, 208 insertions(+), 7 deletions(-)
8ca061
8ca061
diff --git a/ChangeLog b/ChangeLog
8ca061
index 08bc53d050..ef36ffbd15 100644
8ca061
--- a/ChangeLog
8ca061
+++ b/ChangeLog
8ca061
@@ -4,6 +4,10 @@
8ca061
 	  protocol list.
8ca061
 	  The protocol list from OpenSSL is not null-terminated.
8ca061
 	  patched by Kazuki Yamaguchi [Bug #11810] [ruby-core:72082]
8ca061
+
8ca061
+Sun Sep 10 10:10:05 2017  SHIBATA Hiroshi  <hsbt@ruby-lang.org>
8ca061
+
8ca061
+	* lib/rubygems: fix several vulnerabilities in RubyGems [Backport #13842]
8ca061
 
8ca061
 Thu Feb 25 19:49:31 2016  Nobuyoshi Nakada  <nobu@ruby-lang.org>
8ca061
 
8ca061
diff --git a/lib/rubygems/commands/query_command.rb b/lib/rubygems/commands/query_command.rb
8ca061
index 432250e033..44364cfab2 100644
8ca061
--- a/lib/rubygems/commands/query_command.rb
8ca061
+++ b/lib/rubygems/commands/query_command.rb
8ca061
@@ -193,7 +193,7 @@ def output_versions output, versions
8ca061
         end
8ca061
       end
8ca061
 
8ca061
-      output << make_entry(matching_tuples, platforms)
8ca061
+      output << clean_text(make_entry(matching_tuples, platforms))
8ca061
     end
8ca061
   end
8ca061
 
8ca061
@@ -311,7 +311,8 @@ def spec_platforms entry, platforms
8ca061
   end
8ca061
 
8ca061
   def spec_summary entry, spec
8ca061
-    entry << "\n\n" << format_text(spec.summary, 68, 4)
8ca061
+    summary = truncate_text(spec.summary, "the summary for #{spec.full_name}")
8ca061
+    entry << "\n\n" << format_text(summary, 68, 4)
8ca061
   end
8ca061
 
8ca061
 end
8ca061
diff --git a/lib/rubygems/installer.rb b/lib/rubygems/installer.rb
8ca061
index 10fc1a34a5..a27569fe2e 100644
8ca061
--- a/lib/rubygems/installer.rb
8ca061
+++ b/lib/rubygems/installer.rb
8ca061
@@ -596,6 +596,11 @@ def verify_gem_home(unpack = false) # :nodoc:
8ca061
       unpack or File.writable?(gem_home)
8ca061
   end
8ca061
 
8ca061
+  def verify_spec_name
8ca061
+    return if spec.name =~ Gem::Specification::VALID_NAME_PATTERN
8ca061
+    raise Gem::InstallError, "#{spec} has an invalid name"
8ca061
+  end
8ca061
+
8ca061
   ##
8ca061
   # Return the text for an application file.
8ca061
 
8ca061
@@ -767,6 +772,8 @@ def pre_install_checks
8ca061
 
8ca061
     ensure_loadable_spec
8ca061
 
8ca061
+    verify_spec_name
8ca061
+
8ca061
     Gem.ensure_gem_subdirectories gem_home
8ca061
 
8ca061
     return true if @force
8ca061
diff --git a/lib/rubygems/remote_fetcher.rb b/lib/rubygems/remote_fetcher.rb
8ca061
index b1f6dd17fc..2b9d61c0a1 100644
8ca061
--- a/lib/rubygems/remote_fetcher.rb
8ca061
+++ b/lib/rubygems/remote_fetcher.rb
8ca061
@@ -105,7 +105,7 @@ def api_endpoint(uri)
8ca061
     else
8ca061
       target = res.target.to_s.strip
8ca061
 
8ca061
-      if /\.#{Regexp.quote(host)}\z/ =~ target
8ca061
+      if URI("http://" + target).host.end_with?(".#{host}")
8ca061
         return URI.parse "#{uri.scheme}://#{target}#{uri.path}"
8ca061
       end
8ca061
 
8ca061
diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb
8ca061
index ab1cd92270..faca837128 100644
8ca061
--- a/lib/rubygems/specification.rb
8ca061
+++ b/lib/rubygems/specification.rb
8ca061
@@ -107,6 +107,8 @@ class Gem::Specification < Gem::BasicSpecification
8ca061
   today = Time.now.utc
8ca061
   TODAY = Time.utc(today.year, today.month, today.day)
8ca061
 
8ca061
+  VALID_NAME_PATTERN = /\A[a-zA-Z0-9\.\-\_]+\z/ # :nodoc:
8ca061
+
8ca061
   # :startdoc:
8ca061
 
8ca061
   ##
8ca061
@@ -2377,9 +2379,15 @@ def validate packaging = true
8ca061
       end
8ca061
     end
8ca061
 
8ca061
-    unless String === name then
8ca061
+    if !name.is_a?(String) then
8ca061
+      raise Gem::InvalidSpecificationException,
8ca061
+            "invalid value for attribute name: \"#{name.inspect}\" must be a string"
8ca061
+    elsif name !~ /[a-zA-Z]/ then
8ca061
+      raise Gem::InvalidSpecificationException,
8ca061
+            "invalid value for attribute name: #{name.dump} must include at least one letter"
8ca061
+    elsif name !~ VALID_NAME_PATTERN then
8ca061
       raise Gem::InvalidSpecificationException,
8ca061
-            "invalid value for attribute name: \"#{name.inspect}\""
8ca061
+            "invalid value for attribute name: #{name.dump} can only include letters, numbers, dashes, and underscores"
8ca061
     end
8ca061
 
8ca061
     if require_paths.empty? then
8ca061
diff --git a/lib/rubygems/text.rb b/lib/rubygems/text.rb
8ca061
index 5c9287ad2e..86a722ffc0 100644
8ca061
--- a/lib/rubygems/text.rb
8ca061
+++ b/lib/rubygems/text.rb
8ca061
@@ -5,13 +5,26 @@
8ca061
 
8ca061
 module Gem::Text
8ca061
 
8ca061
+  ##
8ca061
+  # Remove any non-printable characters and make the text suitable for
8ca061
+  # printing.
8ca061
+  def clean_text(text)
8ca061
+    text.gsub(/[\000-\b\v-\f\016-\037\177]/, ".".freeze)
8ca061
+  end
8ca061
+
8ca061
+  def truncate_text(text, description, max_length = 100_000)
8ca061
+    raise ArgumentError, "max_length must be positive" unless max_length > 0
8ca061
+    return text if text.size <= max_length
8ca061
+    "Truncating #{description} to #{max_length.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse} characters:\n" + text[0, max_length]
8ca061
+  end
8ca061
+
8ca061
   ##
8ca061
   # Wraps +text+ to +wrap+ characters and optionally indents by +indent+
8ca061
   # characters
8ca061
 
8ca061
   def format_text(text, wrap, indent=0)
8ca061
     result = []
8ca061
-    work = text.dup
8ca061
+    work = clean_text(text)
8ca061
 
8ca061
     while work.length > wrap do
8ca061
       if work =~ /^(.{0,#{wrap}})[ \n]/ then
8ca061
diff --git a/test/rubygems/test_gem_commands_query_command.rb b/test/rubygems/test_gem_commands_query_command.rb
8ca061
index 43fa82571d..ccd2621874 100644
8ca061
--- a/test/rubygems/test_gem_commands_query_command.rb
8ca061
+++ b/test/rubygems/test_gem_commands_query_command.rb
8ca061
@@ -127,6 +127,86 @@ def test_execute_details
8ca061
     This is a lot of text. This is a lot of text. This is a lot of text.
8ca061
     This is a lot of text.
8ca061
 
8ca061
+pl (1)
8ca061
+    Platform: i386-linux
8ca061
+    Author: A User
8ca061
+    Homepage: http://example.com
8ca061
+
8ca061
+    this is a summary
8ca061
+    EOF
8ca061
+
8ca061
+    assert_equal expected, @ui.output
8ca061
+    assert_equal '', @ui.error
8ca061
+  end
8ca061
+
8ca061
+  def test_execute_details_cleans_text
fe27b6
+    @a2.summary = 'This is a lot of text. ' * 4
fe27b6
+    @a2.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
fe27b6
+    @a2.homepage = "http://a.example.com/\x03"
fe27b6
+
fe27b6
+    @pl1.platform = Gem::Platform.new 'i386-linux'
fe27b6
+    @pl1.instance_variable_set :@original_platform, 'i386-linux'
fe27b6
+
fe27b6
+    util_clear_gems
fe27b6
+    util_setup_spec_fetcher @a2, @pl1
8ca061
+
8ca061
+    @cmd.handle_options %w[-r -d]
8ca061
+
8ca061
+    use_ui @ui do
8ca061
+      @cmd.execute
8ca061
+    end
8ca061
+
8ca061
+    expected = <<-EOF
8ca061
+
8ca061
+*** REMOTE GEMS ***
8ca061
+
8ca061
+a (2)
8ca061
+    Authors: Abraham Lincoln ., . Hirohito
8ca061
+    Homepage: http://a.example.com/.
8ca061
+
8ca061
+    This is a lot of text. This is a lot of text. This is a lot of text.
8ca061
+    This is a lot of text.
8ca061
+
8ca061
+pl (1)
8ca061
+    Platform: i386-linux
8ca061
+    Author: A User
8ca061
+    Homepage: http://example.com
8ca061
+
8ca061
+    this is a summary
8ca061
+    EOF
8ca061
+
8ca061
+    assert_equal expected, @ui.output
8ca061
+    assert_equal '', @ui.error
8ca061
+  end
8ca061
+
8ca061
+  def test_execute_details_truncates_summary
fe27b6
+    @a2.summary = 'This is a lot of text. ' * 10_000
fe27b6
+    @a2.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
fe27b6
+    @a2.homepage = "http://a.example.com/\x03"
fe27b6
+
fe27b6
+    @pl1.platform = Gem::Platform.new 'i386-linux'
fe27b6
+    @pl1.instance_variable_set :@original_platform, 'i386-linux'
fe27b6
+
fe27b6
+    util_clear_gems
fe27b6
+    util_setup_spec_fetcher @a2, @pl1
8ca061
+
8ca061
+    @cmd.handle_options %w[-r -d]
8ca061
+
8ca061
+    use_ui @ui do
8ca061
+      @cmd.execute
8ca061
+    end
8ca061
+
8ca061
+    expected = <<-EOF
8ca061
+
8ca061
+*** REMOTE GEMS ***
8ca061
+
8ca061
+a (2)
8ca061
+    Authors: Abraham Lincoln ., . Hirohito
8ca061
+    Homepage: http://a.example.com/.
8ca061
+
8ca061
+    Truncating the summary for a-2 to 100,000 characters:
8ca061
+#{"    This is a lot of text. This is a lot of text. This is a lot of text.\n" * 1449}    This is a lot of te
8ca061
+
8ca061
 pl (1)
8ca061
     Platform: i386-linux
8ca061
     Author: A User
8ca061
diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb
8ca061
index 6f8012feb8..0a439cdf3d 100644
8ca061
--- a/test/rubygems/test_gem_installer.rb
8ca061
+++ b/test/rubygems/test_gem_installer.rb
8ca061
@@ -1190,6 +1190,38 @@ def test_pre_install_checks_wrong_rubygems_version
8ca061
     end
8ca061
   end
8ca061
 
8ca061
+  def test_pre_install_checks_malicious_name
8ca061
+    spec = Gem::Specification.new do |s|
8ca061
+      s.platform    = Gem::Platform::RUBY
8ca061
+      s.name        = '../malicious'
8ca061
+      s.version     = '1'
8ca061
+      s.author      = 'A User'
8ca061
+      s.email       = 'example@example.com'
8ca061
+      s.homepage    = 'http://example.com'
8ca061
+      s.summary     = "this is a summary"
8ca061
+      s.description = "This is a test description"
8ca061
+    end
8ca061
+
8ca061
+    Gem::Specification.reset
8ca061
+
8ca061
+    def spec.full_name # so the spec is buildable
8ca061
+      "malicious-1"
8ca061
+    end
8ca061
+    def spec.validate; end
8ca061
+
8ca061
+    util_build_gem spec
8ca061
+
8ca061
+    gem = File.join(@gemhome, 'cache', spec.file_name)
8ca061
+
8ca061
+    use_ui @ui do
8ca061
+      @installer = Gem::Installer.new gem
8ca061
+      e = assert_raises Gem::InstallError do
8ca061
+        @installer.pre_install_checks
8ca061
+      end
8ca061
+      assert_equal '#<Gem::Specification name=../malicious version=1> has an invalid name', e.message
8ca061
+    end
8ca061
+  end
8ca061
+
8ca061
   def test_shebang
8ca061
     util_make_exec @spec, "#!/usr/bin/ruby"
8ca061
 
8ca061
diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb
8ca061
index 63dd8feb38..ca4627810b 100644
8ca061
--- a/test/rubygems/test_gem_remote_fetcher.rb
8ca061
+++ b/test/rubygems/test_gem_remote_fetcher.rb
8ca061
@@ -191,6 +191,21 @@ def test_api_endpoint
8ca061
     dns.verify
8ca061
   end
8ca061
 
8ca061
+  def test_api_endpoint_ignores_trans_domain_values_that_end_with_original_in_path
8ca061
+    uri = URI.parse "http://example.com/foo"
8ca061
+    target = MiniTest::Mock.new
8ca061
+    target.expect :target, "evil.com/a.example.com"
8ca061
+
8ca061
+    dns = MiniTest::Mock.new
8ca061
+    dns.expect :getresource, target, [String, Object]
8ca061
+
8ca061
+    fetch = Gem::RemoteFetcher.new nil, dns
8ca061
+    assert_equal URI.parse("http://example.com/foo"), fetch.api_endpoint(uri)
8ca061
+
8ca061
+    target.verify
8ca061
+    dns.verify
8ca061
+  end
8ca061
+
8ca061
   def test_api_endpoint_ignores_trans_domain_values
8ca061
     uri = URI.parse "http://gems.example.com/foo"
8ca061
     target = MiniTest::Mock.new
8ca061
diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb
8ca061
index 3cadc55d5d..4f7076a03a 100644
8ca061
--- a/test/rubygems/test_gem_specification.rb
8ca061
+++ b/test/rubygems/test_gem_specification.rb
8ca061
@@ -1598,7 +1598,37 @@ def test_validate_name
8ca061
       @a1.validate
8ca061
     end
8ca061
 
8ca061
-    assert_equal 'invalid value for attribute name: ":json"', e.message
8ca061
+    assert_equal 'invalid value for attribute name: ":json" must be a string', e.message
8ca061
+
8ca061
+    @a1.name = []
8ca061
+    e = assert_raises Gem::InvalidSpecificationException do
8ca061
+      @a1.validate
8ca061
+    end
8ca061
+    assert_equal "invalid value for attribute name: \"[]\" must be a string", e.message
8ca061
+
8ca061
+    @a1.name = ""
8ca061
+    e = assert_raises Gem::InvalidSpecificationException do
8ca061
+      @a1.validate
8ca061
+    end
8ca061
+    assert_equal "invalid value for attribute name: \"\" must include at least one letter", e.message
8ca061
+
8ca061
+    @a1.name = "12345"
8ca061
+    e = assert_raises Gem::InvalidSpecificationException do
8ca061
+      @a1.validate
8ca061
+    end
8ca061
+    assert_equal "invalid value for attribute name: \"12345\" must include at least one letter", e.message
8ca061
+
8ca061
+    @a1.name = "../malicious"
8ca061
+    e = assert_raises Gem::InvalidSpecificationException do
8ca061
+      @a1.validate
8ca061
+    end
8ca061
+    assert_equal "invalid value for attribute name: \"../malicious\" can only include letters, numbers, dashes, and underscores", e.message
8ca061
+
8ca061
+    @a1.name = "\ba\t"
8ca061
+    e = assert_raises Gem::InvalidSpecificationException do
8ca061
+      @a1.validate
8ca061
+    end
8ca061
+    assert_equal "invalid value for attribute name: \"\\ba\\t\" can only include letters, numbers, dashes, and underscores", e.message
8ca061
   end
8ca061
 
8ca061
   def test_validate_non_nil
8ca061
diff --git a/test/rubygems/test_gem_text.rb b/test/rubygems/test_gem_text.rb
8ca061
index e5cfc41e61..9b270b481b 100644
8ca061
--- a/test/rubygems/test_gem_text.rb
8ca061
+++ b/test/rubygems/test_gem_text.rb
8ca061
@@ -35,6 +35,10 @@ def test_format_text_trailing # for two spaces after .
8ca061
     assert_equal expected, format_text(text, 78)
8ca061
   end
8ca061
 
8ca061
+  def test_format_removes_nonprintable_characters
8ca061
+    assert_equal "text with weird .. stuff .", format_text("text with weird \x1b\x02 stuff \x7f", 40)
8ca061
+  end
8ca061
+
8ca061
   def test_levenshtein_distance_add
8ca061
     assert_equal 2, levenshtein_distance("zentest", "zntst")
8ca061
     assert_equal 2, levenshtein_distance("zntst", "zentest")
8ca061
@@ -55,4 +59,11 @@ def test_levenshtein_distance_replace
8ca061
     assert_equal 7, levenshtein_distance("xxxxxxx", "ZenTest")
8ca061
     assert_equal 7, levenshtein_distance("zentest", "xxxxxxx")
8ca061
   end
8ca061
+
8ca061
+  def test_truncate_text
8ca061
+    assert_equal "abc", truncate_text("abc", "desc")
8ca061
+    assert_equal "Truncating desc to 2 characters:\nab", truncate_text("abc", "desc", 2)
8ca061
+    s = "ab" * 500_001
8ca061
+    assert_equal "Truncating desc to 1,000,000 characters:\n#{s[0, 1_000_000]}", truncate_text(s, "desc", 1_000_000)
8ca061
+  end
8ca061
 end
8ca061
-- 
8ca061
2.15.1
8ca061