1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[rubygems/rubygems] Warn dangling symlinks

https://github.com/rubygems/rubygems/commit/425b78637f
This commit is contained in:
David Rodríguez 2022-08-02 10:27:42 +02:00 committed by git
parent 0591780a74
commit 542040fb83
4 changed files with 39 additions and 12 deletions

View file

@ -490,15 +490,7 @@ class Gem::Installer
spec.executables.each do |filename| spec.executables.each do |filename|
filename.tap(&Gem::UNTAINT) filename.tap(&Gem::UNTAINT)
bin_path = File.join gem_dir, spec.bindir, filename bin_path = File.join gem_dir, spec.bindir, filename
next unless File.exist? bin_path
unless File.exist? bin_path
if File.symlink? bin_path
alert_warning "`#{bin_path}` is dangling symlink pointing to `#{File.readlink bin_path}`"
else
alert_warning "`#{bin_path}` does not exist, maybe `gem pristine #{spec.name}` will fix it?"
end
next
end
mode = File.stat(bin_path).mode mode = File.stat(bin_path).mode
dir_mode = options[:prog_mode] || (mode | 0111) dir_mode = options[:prog_mode] || (mode | 0111)

View file

@ -409,6 +409,8 @@ EOM
def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc: def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc:
directories = [] directories = []
symlinks = []
open_tar_gz io do |tar| open_tar_gz io do |tar|
tar.each do |entry| tar.each do |entry|
full_name = entry.full_name full_name = entry.full_name
@ -422,6 +424,8 @@ EOM
raise Gem::Package::SymlinkError.new(full_name, real_destination, destination_dir) unless raise Gem::Package::SymlinkError.new(full_name, real_destination, destination_dir) unless
normalize_path(real_destination).start_with? normalize_path(destination_dir + "/") normalize_path(real_destination).start_with? normalize_path(destination_dir + "/")
symlinks << [full_name, link_target, destination, real_destination]
end end
FileUtils.rm_rf destination FileUtils.rm_rf destination
@ -445,12 +449,18 @@ EOM
FileUtils.chmod file_mode(entry.header.mode), destination FileUtils.chmod file_mode(entry.header.mode), destination
end if entry.file? end if entry.file?
File.symlink(entry.header.linkname, destination) if entry.symlink?
verbose destination verbose destination
end end
end end
symlinks.each do |name, target, destination, real_destination|
if File.exist?(real_destination)
File.symlink(target, destination)
else
alert_warning "#{@spec.full_name} ships with a dangling symlink named #{name} pointing to missing #{target} file. Ignoring"
end
end
if dir_mode if dir_mode
File.chmod(dir_mode, *directories) File.chmod(dir_mode, *directories)
end end

View file

@ -756,7 +756,10 @@ gem 'other', version
end end
end end
assert_match %r{bin/ascii_binder` is dangling symlink pointing to `bin/asciibinder`}, @ui.error errors = @ui.error.split("\n")
assert_equal "WARNING: ascii_binder-0.1.10.1 ships with a dangling symlink named bin/ascii_binder pointing to missing bin/asciibinder file. Ignoring", errors.shift
assert_empty errors
assert_empty @ui.output assert_empty @ui.output
end end

View file

@ -529,6 +529,7 @@ class TestGemPackage < Gem::Package::TarTestCase
def test_extract_tar_gz_symlink_relative_path def test_extract_tar_gz_symlink_relative_path
package = Gem::Package.new @gem package = Gem::Package.new @gem
package.verify
tgz_io = util_tar_gz do |tar| tgz_io = util_tar_gz do |tar|
tar.add_file "relative.rb", 0644 do |io| tar.add_file "relative.rb", 0644 do |io|
@ -557,6 +558,27 @@ class TestGemPackage < Gem::Package::TarTestCase
File.read(extracted) File.read(extracted)
end end
def test_extract_tar_gz_symlink_broken_relative_path
package = Gem::Package.new @gem
package.verify
tgz_io = util_tar_gz do |tar|
tar.mkdir "lib", 0755
tar.add_symlink "lib/foo.rb", "../broken.rb", 0644
end
ui = Gem::MockGemUi.new
use_ui ui do
package.extract_tar_gz tgz_io, @destination
end
assert_equal "WARNING: a-2 ships with a dangling symlink named lib/foo.rb pointing to missing ../broken.rb file. Ignoring\n", ui.error
extracted = File.join @destination, "lib/foo.rb"
assert_path_not_exist extracted
end
def test_extract_symlink_parent def test_extract_symlink_parent
package = Gem::Package.new @gem package = Gem::Package.new @gem