Add executables and bindir validation to the gem installer#9595
Merged
Conversation
Reject executables that are not plain basenames during pre-install checks. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Reject a bindir that resolves outside the gem directory during pre-install checks. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Escape the executable name interpolated into the generated wrapper so a name containing quotes cannot change the generated Ruby. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR strengthens RubyGems installation safety by validating spec.executables and spec.bindir during Gem::Installer#verify_spec, and by escaping executable names when generating wrapper scripts to prevent Ruby code injection via crafted executable names.
Changes:
- Add pre-install validation rejecting invalid
spec.executables(must be plain basenames) andspec.bindir(must remain within the gem directory). - Escape
'and\in wrapper script executable names inapp_script_text. - Add regression tests covering malicious executables/bindir values and wrapper escaping behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/rubygems/test_gem_installer.rb | Adds tests for executable-name escaping in wrappers and for rejecting malicious executables/bindir during pre-install checks. |
| lib/rubygems/installer.rb | Implements new verify_spec validations for executables and bindir, and escapes executable names in generated wrapper scripts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+720
to
+724
| expanded_gem_dir = File.expand_path(gem_dir) | ||
| expanded_bindir = File.expand_path(File.join(gem_dir, spec.bindir)) | ||
| unless expanded_bindir == expanded_gem_dir || expanded_bindir.start_with?("#{expanded_gem_dir}/") | ||
| raise Gem::InstallError, "#{spec} has an invalid bindir" | ||
| end |
Comment on lines
+716
to
+717
| if spec.executables.any? {|name| name != File.basename(name) || /\A\.\.?\z|\R/.match?(name) } | ||
| raise Gem::InstallError, "#{spec} has an invalid executable" |
A non-String executable name or bindir previously raised TypeError from File.basename or File.join. Guard the type so verify_spec raises Gem::InstallError instead of aborting with an unexpected exception. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is your fix for the problem, implemented in this PR?
Gem::Installer#verify_specnow validatesspec.executablesandspec.bindirduring pre-install checks, requiring executables to be plain basenames and bindir to stay within the gem directory.The generated wrapper script also escapes the executable name so quotes in the name cannot change the generated Ruby.
Make sure the following tasks are checked