bca5ca
From 65cfebb041c454c246aaf32a177b0243915a9998 Mon Sep 17 00:00:00 2001
bca5ca
From: fatkodima <fatkodima123@gmail.com>
bca5ca
Date: Fri, 1 Nov 2019 23:06:10 +0200
bca5ca
Subject: [PATCH] Don't use insecure temporary directory as home directory
bca5ca
bca5ca
---
bca5ca
 lib/bundler.rb                | 29 +++++++++++---------------
bca5ca
 spec/bundler/bundler_spec.rb  | 38 +++++++++--------------------------
bca5ca
 spec/bundler/settings_spec.rb |  2 +-
bca5ca
 3 files changed, 22 insertions(+), 47 deletions(-)
bca5ca
bca5ca
diff --git a/lib/bundler.rb b/lib/bundler.rb
bca5ca
index 2ada6fe7891..b184f7e69c6 100644
bca5ca
--- a/lib/bundler.rb
bca5ca
+++ b/lib/bundler.rb
bca5ca
@@ -170,8 +170,7 @@ def user_home
bca5ca
         end
bca5ca
 
bca5ca
         if warning
bca5ca
-          Kernel.send(:require, "etc")
bca5ca
-          user_home = tmp_home_path(Etc.getlogin, warning)
bca5ca
+          user_home = tmp_home_path(warning)
bca5ca
           Bundler.ui.warn "#{warning}\nBundler will use `#{user_home}' as your home directory temporarily.\n"
bca5ca
           user_home
bca5ca
         else
bca5ca
@@ -180,21 +180,6 @@ def user_home
bca5ca
       end
bca5ca
     end
bca5ca
 
bca5ca
-    def tmp_home_path(login, warning)
bca5ca
-      login ||= "unknown"
bca5ca
-      Kernel.send(:require, "tmpdir")
bca5ca
-      path = Pathname.new(Dir.tmpdir).join("bundler", "home")
bca5ca
-      SharedHelpers.filesystem_access(path) do |tmp_home_path|
bca5ca
-        unless tmp_home_path.exist?
bca5ca
-          tmp_home_path.mkpath
bca5ca
-          tmp_home_path.chmod(0o777)
bca5ca
-        end
bca5ca
-        tmp_home_path.join(login).tap(&:mkpath)
bca5ca
-      end
bca5ca
-    rescue RuntimeError => e
bca5ca
-      raise e.exception("#{warning}\nBundler also failed to create a temporary home directory at `#{path}':\n#{e}")
bca5ca
-    end
bca5ca
-
bca5ca
     def user_bundle_path(dir = "home")
bca5ca
       env_var, fallback = case dir
bca5ca
                           when "home"
bca5ca
@@ -555,6 +555,17 @@ def configure_gem_home
bca5ca
       Bundler.rubygems.clear_paths
bca5ca
     end
bca5ca
 
bca5ca
+    def tmp_home_path(warning)
bca5ca
+      Kernel.send(:require, "tmpdir")
bca5ca
+      SharedHelpers.filesystem_access(Dir.tmpdir) do
bca5ca
+        path = Bundler.tmp
bca5ca
+        at_exit { Bundler.rm_rf(path) }
bca5ca
+        path
bca5ca
+      end
bca5ca
+    rescue RuntimeError => e
bca5ca
+      raise e.exception("#{warning}\nBundler also failed to create a temporary home directory':\n#{e}")
bca5ca
+    end
bca5ca
+
bca5ca
     # @param env [Hash]
bca5ca
     def with_env(env)
bca5ca
       backup = ENV.to_hash
bca5ca
diff --git a/spec/bundler/bundler/bundler_spec.rb b/spec/bundler/bundler/bundler_spec.rb
bca5ca
index 74cf7ae05d3..247838600bf 100644
bca5ca
--- a/spec/bundler/bundler/bundler_spec.rb
bca5ca
+++ b/spec/bundler/bundler/bundler_spec.rb
07f2a6
@@ -233,16 +233,13 @@
bca5ca
           path = "/home/oggy"
bca5ca
           allow(Bundler.rubygems).to receive(:user_home).and_return(path)
bca5ca
           allow(File).to receive(:directory?).with(path).and_return false
bca5ca
-          allow(Etc).to receive(:getlogin).and_return("USER")
bca5ca
-          allow(Dir).to receive(:tmpdir).and_return("/TMP")
bca5ca
-          allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(true)
bca5ca
-          expect(FileUtils).to receive(:mkpath).with("/TMP/bundler/home/USER")
bca5ca
+          allow(Bundler).to receive(:tmp).and_return(Pathname.new("/tmp/trulyrandom"))
bca5ca
           message = <
bca5ca
 `/home/oggy` is not a directory.
bca5ca
-Bundler will use `/TMP/bundler/home/USER' as your home directory temporarily.
bca5ca
+Bundler will use `/tmp/trulyrandom' as your home directory temporarily.
bca5ca
 EOF
bca5ca
           expect(Bundler.ui).to receive(:warn).with(message)
bca5ca
-          expect(Bundler.user_home).to eq(Pathname("/TMP/bundler/home/USER"))
bca5ca
+          expect(Bundler.user_home).to eq(Pathname("/tmp/trulyrandom"))
bca5ca
         end
bca5ca
       end
bca5ca
 
07f2a6
@@ -255,16 +252,13 @@
bca5ca
           allow(File).to receive(:directory?).with(path).and_return true
bca5ca
           allow(File).to receive(:writable?).with(path).and_return false
bca5ca
           allow(File).to receive(:directory?).with(dotbundle).and_return false
bca5ca
-          allow(Etc).to receive(:getlogin).and_return("USER")
bca5ca
-          allow(Dir).to receive(:tmpdir).and_return("/TMP")
bca5ca
-          allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(true)
bca5ca
-          expect(FileUtils).to receive(:mkpath).with("/TMP/bundler/home/USER")
bca5ca
+          allow(Bundler).to receive(:tmp).and_return(Pathname.new("/tmp/trulyrandom"))
bca5ca
           message = <
bca5ca
 `/home/oggy` is not writable.
bca5ca
-Bundler will use `/TMP/bundler/home/USER' as your home directory temporarily.
bca5ca
+Bundler will use `/tmp/trulyrandom' as your home directory temporarily.
bca5ca
 EOF
bca5ca
           expect(Bundler.ui).to receive(:warn).with(message)
bca5ca
-          expect(Bundler.user_home).to eq(Pathname("/TMP/bundler/home/USER"))
bca5ca
+          expect(Bundler.user_home).to eq(Pathname("/tmp/trulyrandom"))
bca5ca
         end
bca5ca
 
bca5ca
         context ".bundle exists and have correct permissions" do
07f2a6
@@ -283,31 +277,17 @@
bca5ca
     context "home directory is not set" do
bca5ca
       it "should issue warning and return a temporary user home" do
bca5ca
         allow(Bundler.rubygems).to receive(:user_home).and_return(nil)
bca5ca
-        allow(Etc).to receive(:getlogin).and_return("USER")
bca5ca
-        allow(Dir).to receive(:tmpdir).and_return("/TMP")
bca5ca
-        allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(true)
bca5ca
-        expect(FileUtils).to receive(:mkpath).with("/TMP/bundler/home/USER")
bca5ca
+        allow(Bundler).to receive(:tmp).and_return(Pathname.new("/tmp/trulyrandom"))
bca5ca
         message = <
bca5ca
 Your home directory is not set.
bca5ca
-Bundler will use `/TMP/bundler/home/USER' as your home directory temporarily.
bca5ca
+Bundler will use `/tmp/trulyrandom' as your home directory temporarily.
bca5ca
 EOF
bca5ca
         expect(Bundler.ui).to receive(:warn).with(message)
bca5ca
-        expect(Bundler.user_home).to eq(Pathname("/TMP/bundler/home/USER"))
bca5ca
+        expect(Bundler.user_home).to eq(Pathname("/tmp/trulyrandom"))
bca5ca
       end
bca5ca
     end
bca5ca
   end
bca5ca
 
bca5ca
-  describe "#tmp_home_path" do
bca5ca
-    it "should create temporary user home" do
bca5ca
-      allow(Dir).to receive(:tmpdir).and_return("/TMP")
bca5ca
-      allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(false)
bca5ca
-      expect(FileUtils).to receive(:mkpath).once.ordered.with("/TMP/bundler/home")
bca5ca
-      expect(FileUtils).to receive(:mkpath).once.ordered.with("/TMP/bundler/home/USER")
bca5ca
-      expect(File).to receive(:chmod).with(0o777, "/TMP/bundler/home")
bca5ca
-      expect(Bundler.tmp_home_path("USER", "")).to eq(Pathname("/TMP/bundler/home/USER"))
bca5ca
-    end
bca5ca
-  end
bca5ca
-
bca5ca
   describe "#requires_sudo?" do
bca5ca
     let!(:tmpdir) { Dir.mktmpdir }
bca5ca
     let(:bundle_path) { Pathname("#{tmpdir}/bundle") }
bca5ca
diff --git a/spec/bundler/bundler/settings_spec.rb b/spec/bundler/bundler/settings_spec.rb
bca5ca
index 7e1dadded76..2a285fdcf37 100644
bca5ca
--- a/spec/bundler/bundler/settings_spec.rb
bca5ca
+++ b/spec/bundler/bundler/settings_spec.rb
bca5ca
@@ -67,7 +67,7 @@
bca5ca
       context "when $TMPDIR is not writable" do
bca5ca
         it "does not raise" do
bca5ca
           expect(Bundler.rubygems).to receive(:user_home).twice.and_return(nil)
bca5ca
-          expect(FileUtils).to receive(:mkpath).twice.with(File.join(Dir.tmpdir, "bundler", "home")).and_raise(Errno::EROFS, "Read-only file system @ dir_s_mkdir - /tmp/bundler")
bca5ca
+          expect(Bundler).to receive(:tmp).twice.and_raise(Errno::EROFS, "Read-only file system @ dir_s_mkdir - /tmp/bundler")
bca5ca
 
bca5ca
           expect(subject.send(:global_config_file)).to be_nil
bca5ca
         end