Skip to content

Commit 510b163

Browse files
tenderlovesegiddins
authored andcommitted
Whitelist classes and symbols that are in Gem spec YAML
This patch adds a method for loading YAML specs from a gem and whitelists classes and symbols that are allowed in the spec. Then it changes calls to YAML.load to call the whitelisted "safe" loader instead. [CVE-2017-0903]
1 parent 6e77ace commit 510b163

File tree

7 files changed

+55
-5
lines changed

7 files changed

+55
-5
lines changed

Manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ lib/rubygems/resolver/specification.rb
387387
lib/rubygems/resolver/stats.rb
388388
lib/rubygems/resolver/vendor_set.rb
389389
lib/rubygems/resolver/vendor_specification.rb
390+
lib/rubygems/safe_yaml.rb
390391
lib/rubygems/security.rb
391392
lib/rubygems/security/policies.rb
392393
lib/rubygems/security/policy.rb

lib/rubygems.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def self.load_yaml
675675

676676
unless test_syck
677677
begin
678-
gem 'psych', '>= 1.2.1'
678+
gem 'psych', '>= 2.0.0'
679679
rescue Gem::LoadError
680680
# It's OK if the user does not have the psych gem installed. We will
681681
# attempt to require the stdlib version
@@ -699,6 +699,7 @@ def self.load_yaml
699699
end
700700

701701
require 'yaml'
702+
require 'rubygems/safe_yaml'
702703

703704
# If we're supposed to be using syck, then we may have to force
704705
# activate it via the YAML::ENGINE API.

lib/rubygems/config_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def load_file(filename)
345345
return {} unless filename and File.exist? filename
346346

347347
begin
348-
content = YAML.load(File.read(filename))
348+
content = Gem::SafeYAML.load(File.read(filename))
349349
unless content.kind_of? Hash
350350
warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
351351
return {}

lib/rubygems/package.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def read_checksums gem
468468

469469
@checksums = gem.seek 'checksums.yaml.gz' do |entry|
470470
Zlib::GzipReader.wrap entry do |gz_io|
471-
YAML.load gz_io.read
471+
Gem::SafeYAML.safe_load gz_io.read
472472
end
473473
end
474474
end

lib/rubygems/package/old.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def file_list io # :nodoc:
101101
header << line
102102
end
103103

104-
YAML.load header
104+
Gem::SafeYAML.safe_load header
105105
end
106106

107107
##

lib/rubygems/safe_yaml.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module Gem
2+
3+
###
4+
# This module is used for safely loading YAML specs from a gem. The
5+
# `safe_load` method defined on this module is specifically designed for
6+
# loading Gem specifications. For loading other YAML safely, please see
7+
# Psych.safe_load
8+
9+
module SafeYAML
10+
WHITELISTED_CLASSES = %w(
11+
Symbol
12+
Time
13+
Date
14+
Gem::Dependency
15+
Gem::Platform
16+
Gem::Requirement
17+
Gem::Specification
18+
Gem::Version
19+
Gem::Version::Requirement
20+
YAML::Syck::DefaultKey
21+
Syck::DefaultKey
22+
)
23+
24+
WHITELISTED_SYMBOLS = %w(
25+
development
26+
runtime
27+
)
28+
29+
if ::YAML.respond_to? :safe_load
30+
def self.safe_load input
31+
::YAML.safe_load(input, WHITELISTED_CLASSES, WHITELISTED_SYMBOLS, true)
32+
end
33+
34+
def self.load input
35+
::YAML.safe_load(input, [::Symbol])
36+
end
37+
else
38+
warn "YAML safe loading is not available. Please upgrade psych to a version that supports safe loading (>= 2.0)."
39+
def self.safe_load input, *args
40+
::YAML.load input
41+
end
42+
43+
def self.load input
44+
::YAML.load input
45+
end
46+
end
47+
end
48+
end

lib/rubygems/specification.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ def self.from_yaml(input)
11011101
Gem.load_yaml
11021102

11031103
input = normalize_yaml_input input
1104-
spec = YAML.load input
1104+
spec = Gem::SafeYAML.safe_load input
11051105

11061106
if spec && spec.class == FalseClass then
11071107
raise Gem::EndOfYAMLException

0 commit comments

Comments
 (0)