Skip to content

Commit

Permalink
Don't write outside of destdir when regenerating plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Dec 4, 2021
1 parent a62d00c commit 141ef4c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rubygems/commands/pristine_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def initialize
options[:env_shebang] = value
end

add_option('-i', '--install-dir DIR',
'Gem repository to get binstubs and plugins installed') do |value, options|
options[:install_dir] = File.expand_path(value)
end

add_option('-n', '--bindir DIR',
'Directory where executables are',
'located') do |value, options|
Expand Down Expand Up @@ -163,11 +168,12 @@ def execute
end

bin_dir = options[:bin_dir] if options[:bin_dir]
install_dir = options[:install_dir] if options[:install_dir]

installer_options = {
:wrappers => true,
:force => true,
:install_dir => spec.base_dir,
:install_dir => install_dir || spec.base_dir,
:env_shebang => env_shebang,
:build_args => spec.build_args,
:bin_dir => bin_dir,
Expand All @@ -177,7 +183,7 @@ def execute
installer = Gem::Installer.for_spec(spec, installer_options)
installer.generate_bin
elsif options[:only_plugins]
installer = Gem::Installer.for_spec(spec)
installer = Gem::Installer.for_spec(spec, installer_options)
installer.generate_plugins
else
installer = Gem::Installer.at(gem, installer_options)
Expand Down
1 change: 1 addition & 0 deletions lib/rubygems/commands/setup_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ def regenerate_plugins(bindir)

args = %w[--all --only-plugins --silent]
args << "--bindir=#{bindir}"
args << "--install-dir=#{default_dir}"

command = Gem::Commands::PristineCommand.new
command.invoke(*args)
Expand Down
15 changes: 15 additions & 0 deletions test/rubygems/test_gem_commands_setup_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ def test_env_shebang_flag
assert_match %r{\A#!\s*#{bin_env}#{ruby_exec}}, File.read(gem_bin_path)
end

def test_destdir_flag_does_not_try_to_write_to_the_default_gem_home
FileUtils.chmod "-w", File.join(@gemhome, "plugins")

destdir = File.join(@tempdir, 'foo')

@cmd.options[:destdir] = destdir
@cmd.execute

spec = Gem::Specification.load("bundler/bundler.gemspec")

spec.executables.each do |e|
assert_path_exist File.join destdir, @gemhome.gsub(/^[a-zA-Z]:/, ''), 'gems', spec.full_name, spec.bindir, e
end
end

def test_files_in
assert_equal %w[rubygems.rb rubygems/requirement.rb rubygems/ssl_certs/rubygems.org/foo.pem],
@cmd.files_in('lib').sort
Expand Down

0 comments on commit 141ef4c

Please sign in to comment.