Skip to content

Commit

Permalink
Merge pull request #5415 from rubygems/fix-load-old-marshaled-specs-i…
Browse files Browse the repository at this point in the history
…ncluding-yaml-private-type-constant

Fix loading old marshaled specs including `YAML::PrivateType` constant
  • Loading branch information
deivid-rodriguez committed Jun 1, 2022
2 parents 1a89914 + 51b330b commit 5d6b328
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,14 @@ def self.unresolved_deps
def self._load(str)
Gem.load_yaml

array = Marshal.load str
array = begin
Marshal.load str
rescue ArgumentError => e
raise unless e.message.include?("YAML")

Object.const_set "YAML", Psych
Marshal.load str
end

spec = Gem::Specification.new
spec.instance_variable_set :@specification_version, array[1]
Expand All @@ -1289,11 +1296,6 @@ def self._load(str)
raise TypeError, "invalid Gem::Specification format #{array.inspect}"
end

# Cleanup any Psych::PrivateType. They only show up for an old bug
# where nil => null, so just convert them to nil based on the type.

array.map! {|e| e.kind_of?(Psych::PrivateType) ? nil : e }

spec.instance_variable_set :@rubygems_version, array[0]
# spec version
spec.instance_variable_set :@name, array[2]
Expand Down
Binary file modified test/rubygems/data/null-type.gemspec.rz
Binary file not shown.
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ def test_handles_private_null_type

data = Marshal.load Gem::Util.inflate(Gem.read_binary(path))

assert_nil data.signing_key
assert_instance_of Gem::Specification, data
end

def test_initialize
Expand Down

0 comments on commit 5d6b328

Please sign in to comment.