PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/rubygems/package.rb

https://github.com/wanabe/ruby
Ruby | 713 lines | 415 code | 149 blank | 149 comment | 29 complexity | c552f14781cd921059086a69a708b17c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, 0BSD, Unlicense, GPL-2.0, BSD-3-Clause
  1. # frozen_string_literal: true
  2. #--
  3. # Copyright (C) 2004 Mauricio Julio Fernández Pradier
  4. # See LICENSE.txt for additional licensing information.
  5. #++
  6. #
  7. # Example using a Gem::Package
  8. #
  9. # Builds a .gem file given a Gem::Specification. A .gem file is a tarball
  10. # which contains a data.tar.gz, metadata.gz, checksums.yaml.gz and possibly
  11. # signatures.
  12. #
  13. # require 'rubygems'
  14. # require 'rubygems/package'
  15. #
  16. # spec = Gem::Specification.new do |s|
  17. # s.summary = "Ruby based make-like utility."
  18. # s.name = 'rake'
  19. # s.version = PKG_VERSION
  20. # s.requirements << 'none'
  21. # s.files = PKG_FILES
  22. # s.description = <<-EOF
  23. # Rake is a Make-like program implemented in Ruby. Tasks
  24. # and dependencies are specified in standard Ruby syntax.
  25. # EOF
  26. # end
  27. #
  28. # Gem::Package.build spec
  29. #
  30. # Reads a .gem file.
  31. #
  32. # require 'rubygems'
  33. # require 'rubygems/package'
  34. #
  35. # the_gem = Gem::Package.new(path_to_dot_gem)
  36. # the_gem.contents # get the files in the gem
  37. # the_gem.extract_files destination_directory # extract the gem into a directory
  38. # the_gem.spec # get the spec out of the gem
  39. # the_gem.verify # check the gem is OK (contains valid gem specification, contains a not corrupt contents archive)
  40. #
  41. # #files are the files in the .gem tar file, not the Ruby files in the gem
  42. # #extract_files and #contents automatically call #verify
  43. require_relative "../rubygems"
  44. require_relative 'security'
  45. require_relative 'user_interaction'
  46. class Gem::Package
  47. include Gem::UserInteraction
  48. class Error < Gem::Exception; end
  49. class FormatError < Error
  50. attr_reader :path
  51. def initialize(message, source = nil)
  52. if source
  53. @path = source.path
  54. message = message + " in #{path}" if path
  55. end
  56. super message
  57. end
  58. end
  59. class PathError < Error
  60. def initialize(destination, destination_dir)
  61. super "installing into parent path %s of %s is not allowed" %
  62. [destination, destination_dir]
  63. end
  64. end
  65. class NonSeekableIO < Error; end
  66. class TooLongFileName < Error; end
  67. ##
  68. # Raised when a tar file is corrupt
  69. class TarInvalidError < Error; end
  70. attr_accessor :build_time # :nodoc:
  71. ##
  72. # Checksums for the contents of the package
  73. attr_reader :checksums
  74. ##
  75. # The files in this package. This is not the contents of the gem, just the
  76. # files in the top-level container.
  77. attr_reader :files
  78. ##
  79. # Reference to the gem being packaged.
  80. attr_reader :gem
  81. ##
  82. # The security policy used for verifying the contents of this package.
  83. attr_accessor :security_policy
  84. ##
  85. # Sets the Gem::Specification to use to build this package.
  86. attr_writer :spec
  87. ##
  88. # Permission for directories
  89. attr_accessor :dir_mode
  90. ##
  91. # Permission for program files
  92. attr_accessor :prog_mode
  93. ##
  94. # Permission for other files
  95. attr_accessor :data_mode
  96. def self.build(spec, skip_validation = false, strict_validation = false, file_name = nil)
  97. gem_file = file_name || spec.file_name
  98. package = new gem_file
  99. package.spec = spec
  100. package.build skip_validation, strict_validation
  101. gem_file
  102. end
  103. ##
  104. # Creates a new Gem::Package for the file at +gem+. +gem+ can also be
  105. # provided as an IO object.
  106. #
  107. # If +gem+ is an existing file in the old format a Gem::Package::Old will be
  108. # returned.
  109. def self.new(gem, security_policy = nil)
  110. gem = if gem.is_a?(Gem::Package::Source)
  111. gem
  112. elsif gem.respond_to? :read
  113. Gem::Package::IOSource.new gem
  114. else
  115. Gem::Package::FileSource.new gem
  116. end
  117. return super unless Gem::Package == self
  118. return super unless gem.present?
  119. return super unless gem.start
  120. return super unless gem.start.include? 'MD5SUM ='
  121. Gem::Package::Old.new gem
  122. end
  123. ##
  124. # Extracts the Gem::Specification and raw metadata from the .gem file at
  125. # +path+.
  126. #--
  127. def self.raw_spec(path, security_policy = nil)
  128. format = new(path, security_policy)
  129. spec = format.spec
  130. metadata = nil
  131. File.open path, Gem.binary_mode do |io|
  132. tar = Gem::Package::TarReader.new io
  133. tar.each_entry do |entry|
  134. case entry.full_name
  135. when 'metadata' then
  136. metadata = entry.read
  137. when 'metadata.gz' then
  138. metadata = Gem::Util.gunzip entry.read
  139. end
  140. end
  141. end
  142. return spec, metadata
  143. end
  144. ##
  145. # Creates a new package that will read or write to the file +gem+.
  146. def initialize(gem, security_policy) # :notnew:
  147. require 'zlib'
  148. @gem = gem
  149. @build_time = Gem.source_date_epoch
  150. @checksums = {}
  151. @contents = nil
  152. @digests = Hash.new {|h, algorithm| h[algorithm] = {} }
  153. @files = nil
  154. @security_policy = security_policy
  155. @signatures = {}
  156. @signer = nil
  157. @spec = nil
  158. end
  159. ##
  160. # Copies this package to +path+ (if possible)
  161. def copy_to(path)
  162. FileUtils.cp @gem.path, path unless File.exist? path
  163. end
  164. ##
  165. # Adds a checksum for each entry in the gem to checksums.yaml.gz.
  166. def add_checksums(tar)
  167. Gem.load_yaml
  168. checksums_by_algorithm = Hash.new {|h, algorithm| h[algorithm] = {} }
  169. @checksums.each do |name, digests|
  170. digests.each do |algorithm, digest|
  171. checksums_by_algorithm[algorithm][name] = digest.hexdigest
  172. end
  173. end
  174. tar.add_file_signed 'checksums.yaml.gz', 0444, @signer do |io|
  175. gzip_to io do |gz_io|
  176. YAML.dump checksums_by_algorithm, gz_io
  177. end
  178. end
  179. end
  180. ##
  181. # Adds the files listed in the packages's Gem::Specification to data.tar.gz
  182. # and adds this file to the +tar+.
  183. def add_contents(tar) # :nodoc:
  184. digests = tar.add_file_signed 'data.tar.gz', 0444, @signer do |io|
  185. gzip_to io do |gz_io|
  186. Gem::Package::TarWriter.new gz_io do |data_tar|
  187. add_files data_tar
  188. end
  189. end
  190. end
  191. @checksums['data.tar.gz'] = digests
  192. end
  193. ##
  194. # Adds files included the package's Gem::Specification to the +tar+ file
  195. def add_files(tar) # :nodoc:
  196. @spec.files.each do |file|
  197. stat = File.lstat file
  198. if stat.symlink?
  199. tar.add_symlink file, File.readlink(file), stat.mode
  200. end
  201. next unless stat.file?
  202. tar.add_file_simple file, stat.mode, stat.size do |dst_io|
  203. File.open file, 'rb' do |src_io|
  204. dst_io.write src_io.read 16384 until src_io.eof?
  205. end
  206. end
  207. end
  208. end
  209. ##
  210. # Adds the package's Gem::Specification to the +tar+ file
  211. def add_metadata(tar) # :nodoc:
  212. digests = tar.add_file_signed 'metadata.gz', 0444, @signer do |io|
  213. gzip_to io do |gz_io|
  214. gz_io.write @spec.to_yaml
  215. end
  216. end
  217. @checksums['metadata.gz'] = digests
  218. end
  219. ##
  220. # Builds this package based on the specification set by #spec=
  221. def build(skip_validation = false, strict_validation = false)
  222. raise ArgumentError, "skip_validation = true and strict_validation = true are incompatible" if skip_validation && strict_validation
  223. Gem.load_yaml
  224. @spec.mark_version
  225. @spec.validate true, strict_validation unless skip_validation
  226. setup_signer(
  227. signer_options: {
  228. expiration_length_days: Gem.configuration.cert_expiration_length_days,
  229. }
  230. )
  231. @gem.with_write_io do |gem_io|
  232. Gem::Package::TarWriter.new gem_io do |gem|
  233. add_metadata gem
  234. add_contents gem
  235. add_checksums gem
  236. end
  237. end
  238. say <<-EOM
  239. Successfully built RubyGem
  240. Name: #{@spec.name}
  241. Version: #{@spec.version}
  242. File: #{File.basename @gem.path}
  243. EOM
  244. ensure
  245. @signer = nil
  246. end
  247. ##
  248. # A list of file names contained in this gem
  249. def contents
  250. return @contents if @contents
  251. verify unless @spec
  252. @contents = []
  253. @gem.with_read_io do |io|
  254. gem_tar = Gem::Package::TarReader.new io
  255. gem_tar.each do |entry|
  256. next unless entry.full_name == 'data.tar.gz'
  257. open_tar_gz entry do |pkg_tar|
  258. pkg_tar.each do |contents_entry|
  259. @contents << contents_entry.full_name
  260. end
  261. end
  262. return @contents
  263. end
  264. end
  265. end
  266. ##
  267. # Creates a digest of the TarEntry +entry+ from the digest algorithm set by
  268. # the security policy.
  269. def digest(entry) # :nodoc:
  270. algorithms = if @checksums
  271. @checksums.keys
  272. else
  273. [Gem::Security::DIGEST_NAME].compact
  274. end
  275. algorithms.each do |algorithm|
  276. digester = Gem::Security.create_digest(algorithm)
  277. digester << entry.read(16384) until entry.eof?
  278. entry.rewind
  279. @digests[algorithm][entry.full_name] = digester
  280. end
  281. @digests
  282. end
  283. ##
  284. # Extracts the files in this package into +destination_dir+
  285. #
  286. # If +pattern+ is specified, only entries matching that glob will be
  287. # extracted.
  288. def extract_files(destination_dir, pattern = "*")
  289. verify unless @spec
  290. FileUtils.mkdir_p destination_dir, :mode => dir_mode && 0755
  291. @gem.with_read_io do |io|
  292. reader = Gem::Package::TarReader.new io
  293. reader.each do |entry|
  294. next unless entry.full_name == 'data.tar.gz'
  295. extract_tar_gz entry, destination_dir, pattern
  296. return # ignore further entries
  297. end
  298. end
  299. end
  300. ##
  301. # Extracts all the files in the gzipped tar archive +io+ into
  302. # +destination_dir+.
  303. #
  304. # If an entry in the archive contains a relative path above
  305. # +destination_dir+ or an absolute path is encountered an exception is
  306. # raised.
  307. #
  308. # If +pattern+ is specified, only entries matching that glob will be
  309. # extracted.
  310. def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc:
  311. directories = [] if dir_mode
  312. open_tar_gz io do |tar|
  313. tar.each do |entry|
  314. next unless File.fnmatch pattern, entry.full_name, File::FNM_DOTMATCH
  315. destination = install_location entry.full_name, destination_dir
  316. FileUtils.rm_rf destination
  317. mkdir_options = {}
  318. mkdir_options[:mode] = dir_mode ? 0755 : (entry.header.mode if entry.directory?)
  319. mkdir =
  320. if entry.directory?
  321. destination
  322. else
  323. File.dirname destination
  324. end
  325. directories << mkdir if directories
  326. mkdir_p_safe mkdir, mkdir_options, destination_dir, entry.full_name
  327. File.open destination, 'wb' do |out|
  328. out.write entry.read
  329. FileUtils.chmod file_mode(entry.header.mode), destination
  330. end if entry.file?
  331. File.symlink(entry.header.linkname, destination) if entry.symlink?
  332. verbose destination
  333. end
  334. end
  335. if directories
  336. directories.uniq!
  337. File.chmod(dir_mode, *directories)
  338. end
  339. end
  340. def file_mode(mode) # :nodoc:
  341. ((mode & 0111).zero? ? data_mode : prog_mode) || mode
  342. end
  343. ##
  344. # Gzips content written to +gz_io+ to +io+.
  345. #--
  346. # Also sets the gzip modification time to the package build time to ease
  347. # testing.
  348. def gzip_to(io) # :yields: gz_io
  349. gz_io = Zlib::GzipWriter.new io, Zlib::BEST_COMPRESSION
  350. gz_io.mtime = @build_time
  351. yield gz_io
  352. ensure
  353. gz_io.close
  354. end
  355. ##
  356. # Returns the full path for installing +filename+.
  357. #
  358. # If +filename+ is not inside +destination_dir+ an exception is raised.
  359. def install_location(filename, destination_dir) # :nodoc:
  360. raise Gem::Package::PathError.new(filename, destination_dir) if
  361. filename.start_with? '/'
  362. destination_dir = File.expand_path(File.realpath(destination_dir))
  363. destination = File.expand_path(File.join(destination_dir, filename))
  364. raise Gem::Package::PathError.new(destination, destination_dir) unless
  365. destination.start_with? destination_dir + '/'
  366. begin
  367. real_destination = File.expand_path(File.realpath(destination))
  368. rescue
  369. # it's fine if the destination doesn't exist, because rm -rf'ing it can't cause any damage
  370. nil
  371. else
  372. raise Gem::Package::PathError.new(real_destination, destination_dir) unless
  373. real_destination.start_with? destination_dir + '/'
  374. end
  375. destination.tap(&Gem::UNTAINT)
  376. destination
  377. end
  378. def normalize_path(pathname)
  379. if Gem.win_platform?
  380. pathname.downcase
  381. else
  382. pathname
  383. end
  384. end
  385. def mkdir_p_safe(mkdir, mkdir_options, destination_dir, file_name)
  386. destination_dir = File.realpath(File.expand_path(destination_dir))
  387. parts = mkdir.split(File::SEPARATOR)
  388. parts.reduce do |path, basename|
  389. path = File.realpath(path) unless path == ""
  390. path = File.expand_path(path + File::SEPARATOR + basename)
  391. lstat = File.lstat path rescue nil
  392. if !lstat || !lstat.directory?
  393. unless normalize_path(path).start_with? normalize_path(destination_dir) and (FileUtils.mkdir path, **mkdir_options rescue false)
  394. raise Gem::Package::PathError.new(file_name, destination_dir)
  395. end
  396. end
  397. path
  398. end
  399. end
  400. ##
  401. # Loads a Gem::Specification from the TarEntry +entry+
  402. def load_spec(entry) # :nodoc:
  403. case entry.full_name
  404. when 'metadata' then
  405. @spec = Gem::Specification.from_yaml entry.read
  406. when 'metadata.gz' then
  407. Zlib::GzipReader.wrap(entry, external_encoding: Encoding::UTF_8) do |gzio|
  408. @spec = Gem::Specification.from_yaml gzio.read
  409. end
  410. end
  411. end
  412. ##
  413. # Opens +io+ as a gzipped tar archive
  414. def open_tar_gz(io) # :nodoc:
  415. Zlib::GzipReader.wrap io do |gzio|
  416. tar = Gem::Package::TarReader.new gzio
  417. yield tar
  418. end
  419. end
  420. ##
  421. # Reads and loads checksums.yaml.gz from the tar file +gem+
  422. def read_checksums(gem)
  423. Gem.load_yaml
  424. @checksums = gem.seek 'checksums.yaml.gz' do |entry|
  425. Zlib::GzipReader.wrap entry do |gz_io|
  426. Gem::SafeYAML.safe_load gz_io.read
  427. end
  428. end
  429. end
  430. ##
  431. # Prepares the gem for signing and checksum generation. If a signing
  432. # certificate and key are not present only checksum generation is set up.
  433. def setup_signer(signer_options: {})
  434. passphrase = ENV['GEM_PRIVATE_KEY_PASSPHRASE']
  435. if @spec.signing_key
  436. @signer =
  437. Gem::Security::Signer.new(
  438. @spec.signing_key,
  439. @spec.cert_chain,
  440. passphrase,
  441. signer_options
  442. )
  443. @spec.signing_key = nil
  444. @spec.cert_chain = @signer.cert_chain.map {|cert| cert.to_s }
  445. else
  446. @signer = Gem::Security::Signer.new nil, nil, passphrase
  447. @spec.cert_chain = @signer.cert_chain.map {|cert| cert.to_pem } if
  448. @signer.cert_chain
  449. end
  450. end
  451. ##
  452. # The spec for this gem.
  453. #
  454. # If this is a package for a built gem the spec is loaded from the
  455. # gem and returned. If this is a package for a gem being built the provided
  456. # spec is returned.
  457. def spec
  458. verify unless @spec
  459. @spec
  460. end
  461. ##
  462. # Verifies that this gem:
  463. #
  464. # * Contains a valid gem specification
  465. # * Contains a contents archive
  466. # * The contents archive is not corrupt
  467. #
  468. # After verification the gem specification from the gem is available from
  469. # #spec
  470. def verify
  471. @files = []
  472. @spec = nil
  473. @gem.with_read_io do |io|
  474. Gem::Package::TarReader.new io do |reader|
  475. read_checksums reader
  476. verify_files reader
  477. end
  478. end
  479. verify_checksums @digests, @checksums
  480. @security_policy.verify_signatures @spec, @digests, @signatures if
  481. @security_policy
  482. true
  483. rescue Gem::Security::Exception
  484. @spec = nil
  485. @files = []
  486. raise
  487. rescue Errno::ENOENT => e
  488. raise Gem::Package::FormatError.new e.message
  489. rescue Gem::Package::TarInvalidError => e
  490. raise Gem::Package::FormatError.new e.message, @gem
  491. end
  492. ##
  493. # Verifies the +checksums+ against the +digests+. This check is not
  494. # cryptographically secure. Missing checksums are ignored.
  495. def verify_checksums(digests, checksums) # :nodoc:
  496. return unless checksums
  497. checksums.sort.each do |algorithm, gem_digests|
  498. gem_digests.sort.each do |file_name, gem_hexdigest|
  499. computed_digest = digests[algorithm][file_name]
  500. unless computed_digest.hexdigest == gem_hexdigest
  501. raise Gem::Package::FormatError.new \
  502. "#{algorithm} checksum mismatch for #{file_name}", @gem
  503. end
  504. end
  505. end
  506. end
  507. ##
  508. # Verifies +entry+ in a .gem file.
  509. def verify_entry(entry)
  510. file_name = entry.full_name
  511. @files << file_name
  512. case file_name
  513. when /\.sig$/ then
  514. @signatures[$`] = entry.read if @security_policy
  515. return
  516. else
  517. digest entry
  518. end
  519. case file_name
  520. when "metadata", "metadata.gz" then
  521. load_spec entry
  522. when 'data.tar.gz' then
  523. verify_gz entry
  524. end
  525. rescue
  526. warn "Exception while verifying #{@gem.path}"
  527. raise
  528. end
  529. ##
  530. # Verifies the files of the +gem+
  531. def verify_files(gem)
  532. gem.each do |entry|
  533. verify_entry entry
  534. end
  535. unless @spec
  536. raise Gem::Package::FormatError.new 'package metadata is missing', @gem
  537. end
  538. unless @files.include? 'data.tar.gz'
  539. raise Gem::Package::FormatError.new \
  540. 'package content (data.tar.gz) is missing', @gem
  541. end
  542. if duplicates = @files.group_by {|f| f }.select {|k,v| v.size > 1 }.map(&:first) and duplicates.any?
  543. raise Gem::Security::Exception, "duplicate files in the package: (#{duplicates.map(&:inspect).join(', ')})"
  544. end
  545. end
  546. ##
  547. # Verifies that +entry+ is a valid gzipped file.
  548. def verify_gz(entry) # :nodoc:
  549. Zlib::GzipReader.wrap entry do |gzio|
  550. gzio.read 16384 until gzio.eof? # gzip checksum verification
  551. end
  552. rescue Zlib::GzipFile::Error => e
  553. raise Gem::Package::FormatError.new(e.message, entry.full_name)
  554. end
  555. end
  556. require_relative 'package/digest_io'
  557. require_relative 'package/source'
  558. require_relative 'package/file_source'
  559. require_relative 'package/io_source'
  560. require_relative 'package/old'
  561. require_relative 'package/tar_header'
  562. require_relative 'package/tar_reader'
  563. require_relative 'package/tar_reader/entry'
  564. require_relative 'package/tar_writer'