diff --git lib/rubygems.rb lib/rubygems.rb index 118268dce2..616887838c 100644 --- lib/rubygems.rb +++ lib/rubygems.rb @@ -9,7 +9,7 @@ require 'rbconfig' module Gem - VERSION = "3.0.1".freeze + VERSION = "3.0.3".freeze end # Must be first since it unloads the prelude from 1.9.2 diff --git lib/rubygems/command_manager.rb lib/rubygems/command_manager.rb index 8e4e26fdea..8ad723be55 100644 --- lib/rubygems/command_manager.rb +++ lib/rubygems/command_manager.rb @@ -7,6 +7,7 @@ require 'rubygems/command' require 'rubygems/user_interaction' +require 'rubygems/text' ## # The command manager registers and installs all the individual sub-commands @@ -32,6 +33,7 @@ class Gem::CommandManager + include Gem::Text include Gem::UserInteraction BUILTIN_COMMANDS = [ # :nodoc: @@ -145,12 +147,12 @@ def command_names def run(args, build_args=nil) process_args(args, build_args) rescue StandardError, Timeout::Error => ex - alert_error "While executing gem ... (#{ex.class})\n #{ex}" + alert_error clean_text("While executing gem ... (#{ex.class})\n #{ex}") ui.backtrace ex terminate_interaction(1) rescue Interrupt - alert_error "Interrupted" + alert_error clean_text("Interrupted") terminate_interaction(1) end @@ -168,7 +170,7 @@ def process_args(args, build_args=nil) say Gem::VERSION terminate_interaction 0 when /^-/ then - alert_error "Invalid option: #{args.first}. See 'gem --help'." + alert_error clean_text("Invalid option: #{args.first}. See 'gem --help'.") terminate_interaction 1 else cmd_name = args.shift.downcase @@ -224,7 +226,7 @@ def load_and_instantiate(command_name) rescue Exception => e e = load_error if load_error - alert_error "Loading command: #{command_name} (#{e.class})\n\t#{e}" + alert_error clean_text("Loading command: #{command_name} (#{e.class})\n\t#{e}") ui.backtrace e end end diff --git lib/rubygems/commands/owner_command.rb lib/rubygems/commands/owner_command.rb index 799f31704a..22b7327683 100644 --- lib/rubygems/commands/owner_command.rb +++ lib/rubygems/commands/owner_command.rb @@ -2,8 +2,11 @@ require 'rubygems/command' require 'rubygems/local_remote_options' require 'rubygems/gemcutter_utilities' +require 'rubygems/text' class Gem::Commands::OwnerCommand < Gem::Command + + include Gem::Text include Gem::LocalRemoteOptions include Gem::GemcutterUtilities @@ -65,7 +68,7 @@ def show_owners(name) end with_response response do |resp| - owners = Gem::SafeYAML.load resp.body + owners = Gem::SafeYAML.load clean_text(resp.body) say "Owners for gem: #{name}" owners.each do |owner| diff --git lib/rubygems/gemcutter_utilities.rb lib/rubygems/gemcutter_utilities.rb index 3607b61529..e49bff36f9 100644 --- lib/rubygems/gemcutter_utilities.rb +++ lib/rubygems/gemcutter_utilities.rb @@ -1,11 +1,14 @@ # frozen_string_literal: true require 'rubygems/remote_fetcher' +require 'rubygems/text' ## # Utility methods for using the RubyGems API. module Gem::GemcutterUtilities + include Gem::Text + # TODO: move to Gem::Command OptionParser.accept Symbol do |value| value.to_sym @@ -162,13 +165,13 @@ def with_response(response, error_prefix = nil) if block_given? yield response else - say response.body + say clean_text(response.body) end else message = response.body message = "#{error_prefix}: #{message}" if error_prefix - say message + say clean_text(message) terminate_interaction 1 # TODO: question this end end diff --git lib/rubygems/install_update_options.rb lib/rubygems/install_update_options.rb index f05491a31c..38a0682958 100644 --- lib/rubygems/install_update_options.rb +++ lib/rubygems/install_update_options.rb @@ -30,7 +30,7 @@ def add_install_update_options options[:bin_dir] = File.expand_path(value) end - add_option(:"Install/Update", '--[no-]document [TYPES]', Array, + add_option(:"Install/Update", '--document [TYPES]', Array, 'Generate documentation for installed gems', 'List the documentation types you wish to', 'generate. For example: rdoc,ri') do |value, options| diff --git lib/rubygems/installer.rb lib/rubygems/installer.rb index 6cd073e11d..3308049ccf 100644 --- lib/rubygems/installer.rb +++ lib/rubygems/installer.rb @@ -725,9 +725,26 @@ def verify_gem_home(unpack = false) # :nodoc: unpack or File.writable?(gem_home) end - def verify_spec_name - return if spec.name =~ Gem::Specification::VALID_NAME_PATTERN - raise Gem::InstallError, "#{spec} has an invalid name" + def verify_spec + unless spec.name =~ Gem::Specification::VALID_NAME_PATTERN + raise Gem::InstallError, "#{spec} has an invalid name" + end + + if spec.raw_require_paths.any?{|path| path =~ /\R/ } + raise Gem::InstallError, "#{spec} has an invalid require_paths" + end + + if spec.extensions.any?{|ext| ext =~ /\R/ } + raise Gem::InstallError, "#{spec} has an invalid extensions" + end + + unless spec.specification_version.to_s =~ /\A\d+\z/ + raise Gem::InstallError, "#{spec} has an invalid specification_version" + end + + if spec.dependencies.any? {|dep| dep.type =~ /\R/ || dep.name =~ /\R/ } + raise Gem::InstallError, "#{spec} has an invalid dependencies" + end end ## @@ -876,10 +893,12 @@ def dir def pre_install_checks verify_gem_home options[:unpack] + # The name and require_paths must be verified first, since it could contain + # ruby code that would be eval'ed in #ensure_loadable_spec + verify_spec + ensure_loadable_spec - verify_spec_name - if options[:install_as_default] Gem.ensure_default_gem_subdirectories gem_home else diff --git lib/rubygems/package.rb lib/rubygems/package.rb index 8695b85fec..3bf4f8fd62 100644 --- lib/rubygems/package.rb +++ lib/rubygems/package.rb @@ -456,6 +456,16 @@ def install_location(filename, destination_dir) # :nodoc: raise Gem::Package::PathError.new(destination, destination_dir) unless destination.start_with? destination_dir + '/' + begin + real_destination = File.expand_path(File.realpath(destination)) + rescue + # it's fine if the destination doesn't exist, because rm -rf'ing it can't cause any damage + nil + else + raise Gem::Package::PathError.new(real_destination, destination_dir) unless + real_destination.start_with? destination_dir + '/' + end + destination.untaint destination end diff --git lib/rubygems/requirement.rb lib/rubygems/requirement.rb index 1a73274c01..48f4b00d63 100644 --- lib/rubygems/requirement.rb +++ lib/rubygems/requirement.rb @@ -267,7 +267,22 @@ def to_s # :nodoc: def ==(other) # :nodoc: return unless Gem::Requirement === other - requirements == other.requirements + + # An == check is always necessary + return false unless requirements == other.requirements + + # An == check is sufficient unless any requirements use ~> + return true unless _tilde_requirements.any? + + # If any requirements use ~> we use the stricter `#eql?` that also checks + # that version precision is the same + _tilde_requirements.eql?(other._tilde_requirements) + end + + protected + + def _tilde_requirements + requirements.select { |r| r.first == "~>" } end private diff --git lib/rubygems/test_case.rb lib/rubygems/test_case.rb index 7648ffdc84..a93f749240 100644 --- lib/rubygems/test_case.rb +++ lib/rubygems/test_case.rb @@ -254,6 +254,7 @@ def setup @orig_gem_env_requirements = ENV.to_hash ENV['GEM_VENDOR'] = nil + ENV['SOURCE_DATE_EPOCH'] = nil @current_dir = Dir.pwd @fetcher = nil diff --git lib/rubygems/user_interaction.rb lib/rubygems/user_interaction.rb index 03eac76ea8..0696d54951 100644 --- lib/rubygems/user_interaction.rb +++ lib/rubygems/user_interaction.rb @@ -7,6 +7,7 @@ require 'rubygems/util' require 'rubygems/deprecate' +require 'rubygems/text' ## # Module that defines the default UserInteraction. Any class including this @@ -14,6 +15,8 @@ module Gem::DefaultUserInteraction + include Gem::Text + ## # The default UI is a class variable of the singleton class for this # module. @@ -162,7 +165,7 @@ def terminate_interaction(exit_code = 0) # is true. def verbose(msg = nil) - say(msg || yield) if Gem.configuration.really_verbose + say(clean_text(msg || yield)) if Gem.configuration.really_verbose end end diff --git test/rubygems/test_gem.rb test/rubygems/test_gem.rb index b65787470c..e740a5ab94 100644 --- test/rubygems/test_gem.rb +++ test/rubygems/test_gem.rb @@ -150,6 +150,11 @@ def test_self_install_permissions_umask_077 File.umask(umask) end + def test_self_install_permissions_with_format_executable + @format_executable = true + assert_self_install_permissions + end + def assert_self_install_permissions mask = /mingw|mswin/ =~ RUBY_PLATFORM ? 0700 : 0777 options = { @@ -157,6 +162,7 @@ def assert_self_install_permissions :prog_mode => 0510, :data_mode => 0640, :wrappers => true, + :format_executable => !!(@format_executable if defined?(@format_executable)) } Dir.chdir @tempdir do Dir.mkdir 'bin' @@ -182,8 +188,10 @@ def assert_self_install_permissions prog_mode = (options[:prog_mode] & mask).to_s(8) dir_mode = (options[:dir_mode] & mask).to_s(8) data_mode = (options[:data_mode] & mask).to_s(8) + prog_name = 'foo.cmd' + prog_name = RUBY_INSTALL_NAME.sub('ruby', 'foo.cmd') if options[:format_executable] expected = { - "bin/#{RUBY_INSTALL_NAME.sub('ruby', 'foo.cmd')}" => prog_mode, + "bin/#{prog_name}" => prog_mode, 'gems/foo-1' => dir_mode, 'gems/foo-1/bin' => dir_mode, 'gems/foo-1/data' => dir_mode, diff --git test/rubygems/test_gem_installer.rb test/rubygems/test_gem_installer.rb index 23c153d69b..81888038d0 100644 --- test/rubygems/test_gem_installer.rb +++ test/rubygems/test_gem_installer.rb @@ -1446,6 +1446,112 @@ def spec.validate(packaging, strict); end end end + def test_pre_install_checks_malicious_name_before_eval + spec = util_spec "malicious\n::Object.const_set(:FROM_EVAL, true)#", '1' + def spec.full_name # so the spec is buildable + "malicious-1" + end + def spec.validate(*args); end + + util_build_gem spec + + gem = File.join(@gemhome, 'cache', spec.file_name) + + use_ui @ui do + @installer = Gem::Installer.at gem + e = assert_raises Gem::InstallError do + @installer.pre_install_checks + end + assert_equal "# has an invalid name", e.message + end + refute defined?(::Object::FROM_EVAL) + end + + def test_pre_install_checks_malicious_require_paths_before_eval + spec = util_spec "malicious", '1' + def spec.full_name # so the spec is buildable + "malicious-1" + end + def spec.validate(*args); end + spec.require_paths = ["malicious\n``"] + + util_build_gem spec + + gem = File.join(@gemhome, 'cache', spec.file_name) + + use_ui @ui do + @installer = Gem::Installer.at gem + e = assert_raises Gem::InstallError do + @installer.pre_install_checks + end + assert_equal "# has an invalid require_paths", e.message + end + end + + def test_pre_install_checks_malicious_extensions_before_eval + spec = util_spec "malicious", '1' + def spec.full_name # so the spec is buildable + "malicious-1" + end + def spec.validate(*args); end + spec.extensions = ["malicious\n``"] + + util_build_gem spec + + gem = File.join(@gemhome, 'cache', spec.file_name) + + use_ui @ui do + @installer = Gem::Installer.at gem + e = assert_raises Gem::InstallError do + @installer.pre_install_checks + end + assert_equal "# has an invalid extensions", e.message + end + end + + def test_pre_install_checks_malicious_specification_version_before_eval + spec = util_spec "malicious", '1' + def spec.full_name # so the spec is buildable + "malicious-1" + end + def spec.validate(*args); end + spec.specification_version = "malicious\n``" + + util_build_gem spec + + gem = File.join(@gemhome, 'cache', spec.file_name) + + use_ui @ui do + @installer = Gem::Installer.at gem + e = assert_raises Gem::InstallError do + @installer.pre_install_checks + end + assert_equal "# has an invalid specification_version", e.message + end + end + + def test_pre_install_checks_malicious_dependencies_before_eval + spec = util_spec "malicious", '1' + def spec.full_name # so the spec is buildable + "malicious-1" + end + def spec.validate(*args); end + spec.add_dependency "b\nfoo", '> 5' + + util_build_gem spec + + gem = File.join(@gemhome, 'cache', spec.file_name) + + use_ui @ui do + @installer = Gem::Installer.at gem + @installer.ignore_dependencies = true + e = assert_raises Gem::InstallError do + @installer.pre_install_checks + end + assert_equal "# has an invalid dependencies", e.message + end + end + def test_shebang util_make_exec @spec, "#!/usr/bin/ruby" diff --git test/rubygems/test_gem_package.rb test/rubygems/test_gem_package.rb index 77c5b65c90..495c7fd644 100644 --- test/rubygems/test_gem_package.rb +++ test/rubygems/test_gem_package.rb @@ -105,6 +105,7 @@ def test_add_checksums end def test_build_time_source_date_epoch + epoch = ENV["SOURCE_DATE_EPOCH"] ENV["SOURCE_DATE_EPOCH"] = "123456789" spec = Gem::Specification.new 'build', '1' @@ -118,6 +119,8 @@ def test_build_time_source_date_epoch package = Gem::Package.new spec.file_name assert_equal Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc, package.build_time + ensure + ENV["SOURCE_DATE_EPOCH"] = epoch end def test_add_files @@ -526,6 +529,40 @@ def test_extract_symlink_parent end end + def test_extract_symlink_parent_doesnt_delete_user_dir + package = Gem::Package.new @gem + + # Extract into a subdirectory of @destination; if this test fails it writes + # a file outside destination_subdir, but we want the file to remain inside + # @destination so it will be cleaned up. + destination_subdir = File.join @destination, 'subdir' + FileUtils.mkdir_p destination_subdir + + destination_user_dir = File.join @destination, 'user' + destination_user_subdir = File.join destination_user_dir, 'dir' + FileUtils.mkdir_p destination_user_subdir + + tgz_io = util_tar_gz do |tar| + tar.add_symlink 'link', destination_user_dir, 16877 + tar.add_symlink 'link/dir', '.', 16877 + end + + e = assert_raises(Gem::Package::PathError, Errno::EACCES) do + package.extract_tar_gz tgz_io, destination_subdir + end + + assert_path_exists destination_user_subdir + + if Gem::Package::PathError === e + assert_equal("installing into parent path #{destination_user_subdir} of " + + "#{destination_subdir} is not allowed", e.message) + elsif win_platform? + skip "symlink - must be admin with no UAC on Windows" + else + raise e + end + end + def test_extract_tar_gz_directory package = Gem::Package.new @gem diff --git test/rubygems/test_gem_package_tar_writer.rb test/rubygems/test_gem_package_tar_writer.rb index 408db07de7..517320c011 100644 --- test/rubygems/test_gem_package_tar_writer.rb +++ test/rubygems/test_gem_package_tar_writer.rb @@ -11,9 +11,12 @@ def setup @data = 'abcde12345' @io = TempIO.new @tar_writer = Gem::Package::TarWriter.new @io + @epoch = ENV["SOURCE_DATE_EPOCH"] + ENV["SOURCE_DATE_EPOCH"] = nil end def teardown + ENV["SOURCE_DATE_EPOCH"] = @epoch @tar_writer.close unless @tar_writer.closed? @io.close! diff --git test/rubygems/test_gem_requirement.rb test/rubygems/test_gem_requirement.rb index 1564ffb0ed..7a59243b6a 100644 --- test/rubygems/test_gem_requirement.rb +++ test/rubygems/test_gem_requirement.rb @@ -20,6 +20,12 @@ def test_equals2 refute_requirement_equal "= 1.2", "= 1.3" refute_requirement_equal "= 1.3", "= 1.2" + refute_requirement_equal "~> 1.3", "~> 1.3.0" + refute_requirement_equal "~> 1.3.0", "~> 1.3" + + assert_requirement_equal ["> 2", "~> 1.3"], ["> 2.0", "~> 1.3"] + assert_requirement_equal ["> 2.0", "~> 1.3"], ["> 2", "~> 1.3"] + refute_equal Object.new, req("= 1.2") refute_equal req("= 1.2"), Object.new end diff --git test/rubygems/test_gem_specification.rb test/rubygems/test_gem_specification.rb index 8deb211798..d2ecbf4434 100644 --- test/rubygems/test_gem_specification.rb +++ test/rubygems/test_gem_specification.rb @@ -1719,8 +1719,11 @@ def test_date_tolerates_hour_sec_and_timezone end def test_date_use_env_source_date_epoch + epoch = ENV["SOURCE_DATE_EPOCH"] ENV["SOURCE_DATE_EPOCH"] = "123456789" assert_equal Time.utc(1973,11,29,0,0,0), @a1.date + ensure + ENV["SOURCE_DATE_EPOCH"] = epoch end def test_dependencies diff --git test/rubygems/test_gem_text.rb test/rubygems/test_gem_text.rb index 0249c47f83..c50d0bdb7a 100644 --- test/rubygems/test_gem_text.rb +++ test/rubygems/test_gem_text.rb @@ -89,4 +89,9 @@ def test_truncate_text s = "ab" * 500_001 assert_equal "Truncating desc to 1,000,000 characters:\n#{s[0, 1_000_000]}", truncate_text(s, "desc", 1_000_000) end + + def test_clean_text + assert_equal ".]2;nyan.", clean_text("\e]2;nyan\a") + end + end