summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Rodríguez <deivid.rodriguez@riseup.net>2023-10-03 14:37:44 +0200
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2023-11-08 09:04:28 +0900
commita131ea39b7b9c34304dfbf8112581c49ce9ff827 (patch)
tree9e733fa21cb957a2d0ed425f2a19466534c3936e /lib
parent05ea3bcf14f27e1b3c6d7dd97889d02f988b8920 (diff)
[rubygems/rubygems] Better error when having an insecure install folder
https://github.com/rubygems/rubygems/commit/e41156e272
Diffstat (limited to 'lib')
-rw-r--r--lib/bundler.rb8
-rw-r--r--lib/bundler/errors.rb15
-rw-r--r--lib/bundler/installer/gem_installer.rb2
-rw-r--r--lib/bundler/rubygems_gem_installer.rb19
4 files changed, 31 insertions, 13 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb
index cc6c2c075c..bc28b24e2e 100644
--- a/lib/bundler.rb
+++ b/lib/bundler.rb
@@ -331,14 +331,6 @@ module Bundler
def rm_rf(path)
FileUtils.remove_entry_secure(path) if path && File.exist?(path)
- rescue ArgumentError
- message = <<EOF
-It is a security vulnerability to allow your home directory to be world-writable, and bundler cannot continue.
-You should probably consider fixing this issue by running `chmod o-w ~` on *nix.
-Please refer to https://ruby-doc.org/stdlib-3.1.2/libdoc/fileutils/rdoc/FileUtils.html#method-c-remove_entry_secure for details.
-EOF
- File.world_writable?(path) ? Bundler.ui.warn(message) : raise
- raise PathError, "Please fix the world-writable issue with your #{path} directory"
end
def settings
diff --git a/lib/bundler/errors.rb b/lib/bundler/errors.rb
index c6b3cec4dc..eec72b1692 100644
--- a/lib/bundler/errors.rb
+++ b/lib/bundler/errors.rb
@@ -215,4 +215,19 @@ module Bundler
status_code(36)
end
+
+ class InsecureInstallPathError < BundlerError
+ def initialize(path)
+ @path = path
+ end
+
+ def message
+ "The installation path is insecure. Bundler cannot continue.\n" \
+ "#{@path} is world-writable (without sticky bit).\n" \
+ "Bundler cannot safely replace gems in world-writeable directories due to potential vulnerabilities.\n" \
+ "Please change the permissions of this directory or choose a different install path."
+ end
+
+ status_code(38)
+ end
end
diff --git a/lib/bundler/installer/gem_installer.rb b/lib/bundler/installer/gem_installer.rb
index 1c8a24975d..b6065c24d0 100644
--- a/lib/bundler/installer/gem_installer.rb
+++ b/lib/bundler/installer/gem_installer.rb
@@ -17,7 +17,7 @@ module Bundler
Bundler.ui.debug "#{worker}: #{spec.name} (#{spec.version}) from #{spec.loaded_from}"
generate_executable_stubs
[true, post_install_message]
- rescue Bundler::InstallHookError, Bundler::SecurityError, Bundler::APIResponseMismatchError
+ rescue Bundler::InstallHookError, Bundler::SecurityError, Bundler::APIResponseMismatchError, Bundler::InsecureInstallPathError
raise
rescue Errno::ENOSPC
[false, out_of_space_message]
diff --git a/lib/bundler/rubygems_gem_installer.rb b/lib/bundler/rubygems_gem_installer.rb
index 8463b24bfa..d04ef62e8e 100644
--- a/lib/bundler/rubygems_gem_installer.rb
+++ b/lib/bundler/rubygems_gem_installer.rb
@@ -124,11 +124,22 @@ module Bundler
end
def strict_rm_rf(dir)
- Bundler.rm_rf dir
- rescue StandardError => e
- raise unless File.exist?(dir)
+ return unless File.exist?(dir)
- raise DirectoryRemovalError.new(e, "Could not delete previous installation of `#{dir}`")
+ parent = File.dirname(dir)
+ parent_st = File.stat(parent)
+
+ if parent_st.world_writable? && !parent_st.sticky?
+ raise InsecureInstallPathError.new(parent)
+ end
+
+ begin
+ FileUtils.remove_entry_secure(dir)
+ rescue StandardError => e
+ raise unless File.exist?(dir)
+
+ raise DirectoryRemovalError.new(e, "Could not delete previous installation of `#{dir}`")
+ end
end
end
end