Skip to content

Commit 13bb559

Browse files
hsbtmatzbot
authored andcommitted
[ruby/rubygems] Validate executable names for invalid characters
ruby/rubygems@95dabef672
1 parent 459222a commit 13bb559

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/rubygems/specification_policy.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def validate_values
436436
warning "deprecated autorequire specified" if @specification.autorequire
437437

438438
@specification.executables.each do |executable|
439+
validate_executable(executable)
439440
validate_shebang_line_in(executable)
440441
end
441442

@@ -449,6 +450,13 @@ def validate_attribute_present(attribute)
449450
warning("no #{attribute} specified") if value.nil? || value.empty?
450451
end
451452

453+
def validate_executable(executable)
454+
separators = [File::SEPARATOR, File::ALT_SEPARATOR, File::PATH_SEPARATOR].compact.map {|sep| Regexp.escape(sep) }.join
455+
return unless executable.match?(/[\s#{separators}]/)
456+
457+
error "executable \"#{executable}\" contains invalid characters"
458+
end
459+
452460
def validate_shebang_line_in(executable)
453461
executable_path = File.join(@specification.bindir, executable)
454462
return if File.read(executable_path, 2) == "#!"

test/rubygems/test_gem_specification.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,65 @@ def test_validate_executables
30133013
assert_match "#{w}: bin/exec is missing #! line\n", @ui.error, "error"
30143014
end
30153015

3016+
def test_validate_executables_with_space
3017+
util_setup_validate
3018+
3019+
FileUtils.mkdir_p File.join(@tempdir, "bin")
3020+
File.write File.join(@tempdir, "bin", "echo hax"), "#!/usr/bin/env ruby\n"
3021+
3022+
@a1.executables = ["echo hax"]
3023+
3024+
e = assert_raise Gem::InvalidSpecificationException do
3025+
use_ui @ui do
3026+
Dir.chdir @tempdir do
3027+
@a1.validate
3028+
end
3029+
end
3030+
end
3031+
3032+
assert_match "executable \"echo hax\" contains invalid characters", e.message
3033+
end
3034+
3035+
def test_validate_executables_with_path_separator
3036+
util_setup_validate
3037+
3038+
FileUtils.mkdir_p File.join(@tempdir, "bin")
3039+
File.write File.join(@tempdir, "exe"), "#!/usr/bin/env ruby\n"
3040+
3041+
@a1.executables = Gem.win_platform? ? ["..\\exe"] : ["../exe"]
3042+
3043+
e = assert_raise Gem::InvalidSpecificationException do
3044+
use_ui @ui do
3045+
Dir.chdir @tempdir do
3046+
@a1.validate
3047+
end
3048+
end
3049+
end
3050+
3051+
assert_match "executable \"#{Gem.win_platform? ? "..\\exe" : "../exe"}\" contains invalid characters", e.message
3052+
end
3053+
3054+
def test_validate_executables_with_path_list_separator
3055+
sep = Gem.win_platform? ? ";" : ":"
3056+
3057+
util_setup_validate
3058+
3059+
FileUtils.mkdir_p File.join(@tempdir, "bin")
3060+
File.write File.join(@tempdir, "bin", "foo#{sep}bar"), "#!/usr/bin/env ruby\n"
3061+
3062+
@a1.executables = ["foo#{sep}bar"]
3063+
3064+
e = assert_raise Gem::InvalidSpecificationException do
3065+
use_ui @ui do
3066+
Dir.chdir @tempdir do
3067+
@a1.validate
3068+
end
3069+
end
3070+
end
3071+
3072+
assert_match "executable \"foo#{sep}bar\" contains invalid characters", e.message
3073+
end
3074+
30163075
def test_validate_empty_require_paths
30173076
util_setup_validate
30183077

0 commit comments

Comments
 (0)