Skip to content

Commit

Permalink
Merge pull request #7568 from rubygems/segiddins/add-a-limit-to-the-s…
Browse files Browse the repository at this point in the history
…ize-of-the-metadata-and-checksums-files-in-a-gem-package

Add a limit to the size of the metadata and checksums files in a gem package.

(cherry picked from commit 4842ad9)
  • Loading branch information
deivid-rodriguez committed Apr 30, 2024
1 parent a2f67f8 commit 95f635c
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/rubygems/package.rb
Expand Up @@ -527,12 +527,13 @@ def normalize_path(pathname)
# Loads a Gem::Specification from the TarEntry +entry+

def load_spec(entry) # :nodoc:
limit = 10 * 1024 * 1024
case entry.full_name
when "metadata" then
@spec = Gem::Specification.from_yaml entry.read
@spec = Gem::Specification.from_yaml limit_read(entry, "metadata", limit)
when "metadata.gz" then
Zlib::GzipReader.wrap(entry, external_encoding: Encoding::UTF_8) do |gzio|
@spec = Gem::Specification.from_yaml gzio.read
@spec = Gem::Specification.from_yaml limit_read(gzio, "metadata.gz", limit)
end
end
end
Expand All @@ -556,7 +557,7 @@ def read_checksums(gem)

@checksums = gem.seek "checksums.yaml.gz" do |entry|
Zlib::GzipReader.wrap entry do |gz_io|
Gem::SafeYAML.safe_load gz_io.read
Gem::SafeYAML.safe_load limit_read(gz_io, "checksums.yaml.gz", 10 * 1024 * 1024)
end
end
end
Expand Down Expand Up @@ -663,7 +664,7 @@ def verify_entry(entry)

case file_name
when /\.sig$/ then
@signatures[$`] = entry.read if @security_policy
@signatures[$`] = limit_read(entry, file_name, 1024 * 1024) if @security_policy
return
else
digest entry
Expand Down Expand Up @@ -723,6 +724,12 @@ def copy_stream(src, dst) # :nodoc:
IO.copy_stream(src, dst)
end
end

def limit_read(io, name, limit)
bytes = io.read(limit + 1)
raise Gem::Package::FormatError, "#{name} is too big (over #{limit} bytes)" if bytes.size > limit
bytes
end
end

require_relative "package/digest_io"
Expand Down

0 comments on commit 95f635c

Please sign in to comment.