Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rubygems/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ def verify_spec
raise Gem::InstallError, "#{spec} has an invalid name"
end

unless /\A#{Gem::Version::VERSION_PATTERN}\z/.match?(spec.version.to_s)
raise Gem::InstallError, "#{spec} has an invalid version"
end

if spec.raw_require_paths.any? {|path| path =~ /\R/ }
raise Gem::InstallError, "#{spec} has an invalid require_paths"
end
Expand Down
40 changes: 40 additions & 0 deletions test/rubygems/test_gem_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,46 @@ def spec.validate(*args); end
refute defined?(::Object::FROM_EVAL)
end

# Psych restores a version from gem metadata through
# Gem::Version#yaml_initialize, which skips the checks in #initialize.
def test_pre_install_checks_malicious_version_before_eval
spec = util_spec "malicious", "1"
def spec.validate(*args); end
version = Gem::Version.allocate
version.yaml_initialize nil, "version" => "1\n::Object.const_set(:FROM_EVAL, true)#"
spec.version = version

installer = Gem::Installer.for_spec spec
installer.gem_home = @gemhome

use_ui @ui do
e = assert_raise Gem::InstallError do
installer.pre_install_checks
end
assert_equal "#<Gem::Specification name=malicious version=1\n::Object.const_set(:FROM_EVAL, true)#> has an invalid version", e.message
end
refute defined?(::Object::FROM_EVAL)
end

def test_pre_install_checks_accepts_real_versions
%w[1 1.0.0 1.0.0.a 1.0.0-rc.1 0.1.0.pre.20260918].each do |version|
spec = util_spec "a", version

util_build_gem spec

installer = Gem::Installer.at spec.cache_file,
install_dir: @gemhome,
user_install: false,
force: true

use_ui @ui do
assert_equal spec, installer.install, version
end

assert_path_exist File.join(@gemhome, "gems", spec.full_name), version
end
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
Expand Down
Loading