Skip to content

Commit ea105d6

Browse files
Saidbekhsbt
authored andcommitted
pr reviews
1 parent b76ecd5 commit ea105d6

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

lib/rubygems/package.rb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,13 @@ def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc:
445445
full_name = entry.full_name
446446
next unless File.fnmatch pattern, full_name, File::FNM_DOTMATCH
447447

448-
destination = install_location full_name, destination_dir
449-
450-
if invalid_windows_filename?(full_name)
448+
if Gem.win_platform? && invalid_windows_filename?(full_name)
451449
gem_name = @spec ? @spec.full_name : "unknown"
452450
raise Gem::Package::InvalidWindowsFileNameError.new(full_name, gem_name)
453451
end
454452

453+
destination = install_location full_name, destination_dir
454+
455455
if entry.symlink?
456456
link_target = entry.header.linkname
457457
real_destination = link_target.start_with?("/") ? link_target : File.expand_path(link_target, File.dirname(destination))
@@ -480,18 +480,13 @@ def extract_tar_gz(io, destination_dir, pattern = "*") # :nodoc:
480480
end
481481

482482
if entry.file?
483-
begin
484-
File.open(destination, "wb") do |out|
485-
copy_stream(tar.io, out, entry.size)
486-
# Flush needs to happen before chmod because there could be data
487-
# in the IO buffer that needs to be written, and that could be
488-
# written after the chmod (on close) which would mess up the perms
489-
out.flush
490-
out.chmod file_mode(entry.header.mode) & ~File.umask
491-
end
492-
rescue Errno::EINVAL
493-
gem_name = @spec ? @spec.full_name : "unknown"
494-
raise Gem::Package::InvalidWindowsFileNameError.new(full_name, gem_name)
483+
File.open(destination, "wb") do |out|
484+
copy_stream(tar.io, out, entry.size)
485+
# Flush needs to happen before chmod because there could be data
486+
# in the IO buffer that needs to be written, and that could be
487+
# written after the chmod (on close) which would mess up the perms
488+
out.flush
489+
out.chmod file_mode(entry.header.mode) & ~File.umask
495490
end
496491
end
497492

@@ -571,10 +566,7 @@ def normalize_path(pathname) # :nodoc:
571566
# Note: Colons are only valid as drive letter separators (e.g., C:), not in filenames.
572567

573568
def invalid_windows_filename?(filename) # :nodoc:
574-
return false unless Gem.win_platform?
575-
576-
basename = File.basename(filename)
577-
basename.match?(/[:<>"|?*\\\x00-\x1f]/)
569+
filename.to_s.split("/").any? { |part| part.match?(/[:<>"|?*\\\x00-\x1f]/) }
578570
end
579571

580572
##

test/rubygems/test_gem_package.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,4 +1384,35 @@ def test_extract_tar_gz_invalid_filename
13841384
assert_match(/characters in its name that are not allowed on Windows/, e.message)
13851385
assert_match(/This is a problem with the 'a-2' gem, not Rubygems/, e.message)
13861386
end
1387+
1388+
def test_build_warns_on_invalid_windows_filename
1389+
pend "Windows filename validation only applies on non-Windows" if Gem.win_platform?
1390+
1391+
spec = Gem::Specification.new "test_gem", "1.0"
1392+
spec.summary = "test"
1393+
spec.authors = "test"
1394+
spec.files = ["lib/code.rb", "lib/file:name.rb"]
1395+
1396+
FileUtils.mkdir "lib"
1397+
1398+
File.open "lib/code.rb", "w" do |io|
1399+
io.write "# lib/code.rb"
1400+
end
1401+
1402+
File.open "lib/file:name.rb", "w" do |io|
1403+
io.write "# lib/file:name.rb"
1404+
end
1405+
1406+
package = Gem::Package.new spec.file_name
1407+
package.spec = spec
1408+
1409+
ui = Gem::MockGemUi.new
1410+
use_ui ui do
1411+
package.build
1412+
end
1413+
1414+
assert_match(%r{filename 'lib/file:name\.rb' contains characters that are invalid on Windows}, ui.error)
1415+
assert_match(/This gem may fail to install on Windows/, ui.error)
1416+
assert_path_exist spec.file_name
1417+
end
13871418
end

0 commit comments

Comments
 (0)