8ca061
From 1281e56682692859e726e24fff30e44aac6f948b Mon Sep 17 00:00:00 2001
8ca061
From: nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8ca061
Date: Wed, 11 Oct 2017 13:48:14 +0000
8ca061
Subject: [PATCH] merge revision(s) 60149: [Backport #14003]
8ca061
8ca061
	Merge rubygems-2.6.14 changes.
8ca061
8ca061
	  It fixed http://blog.rubygems.org/2017/10/09/unsafe-object-deserialization-vulnerability.html
8ca061
8ca061
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@60168 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8ca061
---
8ca061
 lib/rubygems.rb               |  5 +++--
8ca061
 lib/rubygems/config_file.rb   |  2 +-
8ca061
 lib/rubygems/package.rb       |  2 +-
8ca061
 lib/rubygems/package/old.rb   |  2 +-
8ca061
 lib/rubygems/safe_yaml.rb     | 48 +++++++++++++++++++++++++++++++++++++++++++
8ca061
 lib/rubygems/specification.rb |  2 +-
8ca061
 6 files changed, 55 insertions(+), 6 deletions(-)
8ca061
 create mode 100644 lib/rubygems/safe_yaml.rb
8ca061
8ca061
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
8ca061
index 55aa85b8b2bd..0685bcb3c629 100644
8ca061
--- a/lib/rubygems.rb
8ca061
+++ b/lib/rubygems.rb
8ca061
@@ -574,7 +574,7 @@ def self.load_yaml
8ca061
 
8ca061
     unless test_syck
8ca061
       begin
8ca061
-        gem 'psych', '~> 1.2', '>= 1.2.1'
8ca061
+        gem 'psych', '>= 2.0.0'
8ca061
       rescue Gem::LoadError
8ca061
         # It's OK if the user does not have the psych gem installed.  We will
8ca061
         # attempt to require the stdlib version
8ca061
@@ -598,6 +598,7 @@ def self.load_yaml
8ca061
     end
8ca061
 
8ca061
     require 'yaml'
8ca061
+    require 'rubygems/safe_yaml'
8ca061
 
8ca061
     # If we're supposed to be using syck, then we may have to force
8ca061
     # activate it via the YAML::ENGINE API.
8ca061
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb
8ca061
index c95d7dd1f14e..63583b361615 100644
8ca061
--- a/lib/rubygems/config_file.rb
8ca061
+++ b/lib/rubygems/config_file.rb
8ca061
@@ -316,7 +316,7 @@ def load_file(filename)
8ca061
     return {} unless filename and File.exist? filename
8ca061
 
8ca061
     begin
8ca061
-      content = YAML.load(File.read(filename))
8ca061
+      content = Gem::SafeYAML.load(File.read(filename))
8ca061
       unless content.kind_of? Hash
8ca061
         warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
8ca061
         return {}
8ca061
diff --git a/lib/rubygems/package.rb b/lib/rubygems/package.rb
8ca061
index c36e71d800a2..77811ed5ecaa 100644
8ca061
--- a/lib/rubygems/package.rb
8ca061
+++ b/lib/rubygems/package.rb
8ca061
@@ -418,7 +418,7 @@ def read_checksums gem
8ca061
 
8ca061
     @checksums = gem.seek 'checksums.yaml.gz' do |entry|
8ca061
       Zlib::GzipReader.wrap entry do |gz_io|
8ca061
-        YAML.load gz_io.read
8ca061
+        Gem::SafeYAML.safe_load gz_io.read
8ca061
       end
8ca061
     end
8ca061
   end
8ca061
diff --git a/lib/rubygems/package/old.rb b/lib/rubygems/package/old.rb
8ca061
index 5e722baa3540..071f7141ab78 100644
8ca061
--- a/lib/rubygems/package/old.rb
8ca061
+++ b/lib/rubygems/package/old.rb
8ca061
@@ -100,7 +100,7 @@ def file_list io # :nodoc:
8ca061
       header << line
8ca061
     end
8ca061
 
8ca061
-    YAML.load header
8ca061
+    Gem::SafeYAML.safe_load header
8ca061
   end
8ca061
 
8ca061
   ##
8ca061
diff --git a/lib/rubygems/safe_yaml.rb b/lib/rubygems/safe_yaml.rb
8ca061
new file mode 100644
8ca061
index 000000000000..b98cfaa5e60d
8ca061
--- /dev/null
8ca061
+++ b/lib/rubygems/safe_yaml.rb
8ca061
@@ -0,0 +1,48 @@
8ca061
+module Gem
8ca061
+
8ca061
+  ###
8ca061
+  # This module is used for safely loading YAML specs from a gem.  The
8ca061
+  # `safe_load` method defined on this module is specifically designed for
8ca061
+  # loading Gem specifications.  For loading other YAML safely, please see
8ca061
+  # Psych.safe_load
8ca061
+
8ca061
+  module SafeYAML
8ca061
+    WHITELISTED_CLASSES = %w(
8ca061
+      Symbol
8ca061
+      Time
8ca061
+      Date
8ca061
+      Gem::Dependency
8ca061
+      Gem::Platform
8ca061
+      Gem::Requirement
8ca061
+      Gem::Specification
8ca061
+      Gem::Version
8ca061
+      Gem::Version::Requirement
8ca061
+      YAML::Syck::DefaultKey
8ca061
+      Syck::DefaultKey
8ca061
+    )
8ca061
+
8ca061
+    WHITELISTED_SYMBOLS = %w(
8ca061
+      development
8ca061
+      runtime
8ca061
+    )
8ca061
+
8ca061
+    if ::YAML.respond_to? :safe_load
8ca061
+      def self.safe_load input
8ca061
+        ::YAML.safe_load(input, WHITELISTED_CLASSES, WHITELISTED_SYMBOLS, true)
8ca061
+      end
8ca061
+
8ca061
+      def self.load input
8ca061
+        ::YAML.safe_load(input, [::Symbol])
8ca061
+      end
8ca061
+    else
8ca061
+      warn "YAML safe loading is not available. Please upgrade psych to a version that supports safe loading (>= 2.0)."
8ca061
+      def self.safe_load input, *args
8ca061
+        ::YAML.load input
8ca061
+      end
8ca061
+
8ca061
+      def self.load input
8ca061
+        ::YAML.load input
8ca061
+      end
8ca061
+    end
8ca061
+  end
8ca061
+end
8ca061
diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb
8ca061
index 88e320c05ac9..40e3a70d476c 100644
8ca061
--- a/lib/rubygems/specification.rb
8ca061
+++ b/lib/rubygems/specification.rb
8ca061
@@ -910,7 +910,7 @@ def self.from_yaml(input)
8ca061
     Gem.load_yaml
8ca061
 
8ca061
     input = normalize_yaml_input input
8ca061
-    spec = YAML.load input
8ca061
+    spec = Gem::SafeYAML.safe_load input
8ca061
 
8ca061
     if spec && spec.class == FalseClass then
8ca061
       raise Gem::EndOfYAMLException