Blame SOURCES/ruby-bundler-raise-error-in-dependency-confusion.patch

d17082
diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb
d17082
index 8e56d4a9bc..c37946b46c 100644
d17082
--- a/lib/bundler/definition.rb
d17082
+++ b/lib/bundler/definition.rb
d17082
@@ -901,6 +901,8 @@ def source_requirements
d17082
       # Load all specs from remote sources
d17082
       index
d17082
 
d17082
+      validate_dependency_confusion! unless disable_dependency_confusion_check?
d17082
+
d17082
       # Record the specs available in each gem's source, so that those
d17082
       # specs will be available later when the resolver knows where to
d17082
       # look for that gemspec (or its dependencies)
d17082
@@ -980,5 +982,112 @@ def equivalent_rubygems_remotes?(source)
d17082
 
d17082
       Bundler.settings[:allow_deployment_source_credential_changes] && source.equivalent_remotes?(sources.rubygems_remotes)
d17082
     end
d17082
+
d17082
+    def validate_dependency_confusion!
d17082
+      # Continue if there is a scoped repository in the remote case.
d17082
+      return unless @remote && sources.non_global_rubygems_sources.size > 0
d17082
+
d17082
+      # Raise an error unless all the scope repositories implement the dependency API.
d17082
+      # When there is a non-dependency API scoped repository, we cannot get
d17082
+      # indirect dependencies used in a `Gemfile`.
d17082
+      unless sources.non_global_rubygems_sources.all?(&:dependency_api_available?)
d17082
+        non_api_sources = sources.non_global_rubygems_sources.reject(&:dependency_api_available?)
d17082
+        non_api_source_names_str = non_api_sources.map {|d| "  * #{d}" }.join("\n")
d17082
+
d17082
+        msg = String.new
d17082
+        msg << "Your Gemfile contains scoped sources that don't implement a dependency API, namely:\n\n"
d17082
+        msg << non_api_source_names_str
d17082
+        msg << "\n\nUsing the above gem servers may result in installing unexpected gems. " \
d17082
+          "To resolve this warning, make sure you use gem servers that implement dependency APIs, " \
d17082
+          "such as gemstash or geminabox gem servers."
d17082
+        raise_error_or_warn_dependency_confusion(msg)
d17082
+        return
d17082
+      end
d17082
+
d17082
+      indirect_dep_names = indirect_dependency_names_in_non_global_rubygems_soruces
d17082
+      # Get all the gem names from the index made from the default source.
d17082
+      # default_source_dep_names = @index.sources.select(&:default_source_used?).map(&:spec_names).flatten
d17082
+      # Get all the gem names from each source.
d17082
+      all_spec_names_list = @index.sources.map(&:spec_names)
d17082
+
d17082
+      # Only include the indirect dependency gems on the scoped sources that
d17082
+      # also exist on another source. The gems are included in more than 2
d17082
+      # sources (the own source + another source). If the gems don't exist on
d17082
+      # the another source, the dependency confusion doesn't happen.
d17082
+      indirect_dep_names.select! do |name|
d17082
+        source_num = all_spec_names_list.select {|all_names| all_names.include?(name) }
d17082
+        source_num.size >= 2
d17082
+      end
d17082
+
d17082
+      # Raise an error if there is an indirect dependency.
d17082
+      if indirect_dep_names.size > 0
d17082
+        dep_names_str = indirect_dep_names.join(", ")
d17082
+        source_names_str = sources.non_global_rubygems_sources.map {|d| "  * #{d}" }.join("\n")
d17082
+
d17082
+        msg = String.new
d17082
+        msg << "Your Gemfile contains implicit dependency gems #{dep_names_str} on the scoped sources, namely:\n\n"
d17082
+        msg << source_names_str
d17082
+        msg << "\n\nUsing implicit dependency gems on the above sources may result in installing unexpected gems. "
d17082
+        msg << "To suppress this message, make sure you set the gems explicitly in the Gemfile."
d17082
+        raise_error_or_warn_dependency_confusion(msg)
d17082
+        return
d17082
+      end
d17082
+    end
d17082
+
d17082
+    def raise_error_or_warn_dependency_confusion(msg)
d17082
+      if warn_on_dependnecy_confusion?
d17082
+        Bundler.ui.warn msg
d17082
+      else
d17082
+        msg = "#{msg} Or set the environment variable BUNDLE_WARN_ON_DEPENDENCY_CONFUSION."
d17082
+        raise SecurityError, msg
d17082
+      end
d17082
+    end
d17082
+
d17082
+    def indirect_dependency_names_in_non_global_rubygems_soruces
d17082
+      # Indirect dependency gem names
d17082
+      indirect_dep_names = []
d17082
+      # Direct dependency gem names
d17082
+      direct_dep_names = @dependencies.map(&:name)
d17082
+
d17082
+      sources.non_global_rubygems_sources.each do |s|
d17082
+        # If the non dependency API source is used, the `dependency_names`
d17082
+        # returns gems not only used in the `Gemfile`, but also returns ones
d17082
+        # existing in the scoped source too. This method shouldn't be used with
d17082
+        # the non dependency API sources.
d17082
+        s.specs.dependency_names.each do |dep_name|
d17082
+          # Exclude direct dependency gems.
d17082
+          next if direct_dep_names.include?(dep_name)
d17082
+
d17082
+          s.specs.local_search(dep_name).each do |spec|
d17082
+            # Debug gems with unexpected `spec.class`.
d17082
+            Bundler.ui.debug "Found dependency gem #{dep_name} (#{spec.class}) in scoped sources."
d17082
+            # StubSpecification extending RemoteSpecification: the gems by
d17082
+            #   `gem list`. Exclude the gems.
d17082
+            # EndpointSpecification: gems returned by dependency API such as
d17082
+            #   geminabox
d17082
+            # RemoteSpecification: gems returned by non dependency API such as
d17082
+            #   gem server. This method cannot be executed with the non
d17082
+            #   dependency API sources.
d17082
+            indirect_dep_names << dep_name if spec.class == EndpointSpecification
d17082
+          end
d17082
+        end
d17082
+      end
d17082
+
d17082
+      indirect_dep_names.sort.uniq
d17082
+    end
d17082
+
d17082
+    # Print a warning instead of raising an error when this option is enabled.
d17082
+    # Don't use Bundler.settings to minimize the difference to backport easily
d17082
+    # and avoid additional tests.
d17082
+    def warn_on_dependnecy_confusion?
d17082
+      @warn_on_dependnecy_confusion ||= ENV["BUNDLE_WARN_ON_DEPENDENCY_CONFUSION"]
d17082
+    end
d17082
+
d17082
+    # Disable the dependency confusion check when this option is enabled.
d17082
+    # The option can be used as a workaround if the check logic is problematic
d17082
+    # in a case such as a performance issue.
d17082
+    def disable_dependency_confusion_check?
d17082
+      @disable_dependnecy_confusion_check ||= ENV["BUNDLE_DISABLE_DEPENDENCY_CONFUSION_CHECK"]
d17082
+    end
d17082
   end
d17082
 end
d17082
diff --git a/lib/bundler/source/rubygems.rb b/lib/bundler/source/rubygems.rb
d17082
index 485b388a32..48a2ab736b 100644
d17082
--- a/lib/bundler/source/rubygems.rb
d17082
+++ b/lib/bundler/source/rubygems.rb
d17082
@@ -287,6 +287,10 @@ def dependency_names_to_double_check
d17082
         names
d17082
       end
d17082
 
d17082
+      def dependency_api_available?
d17082
+        api_fetchers.any?
d17082
+      end
d17082
+
d17082
     protected
d17082
 
d17082
       def credless_remotes
d17082
diff --git a/lib/bundler/source_list.rb b/lib/bundler/source_list.rb
d17082
index ac2adacb3d..37869878ce 100644
d17082
--- a/lib/bundler/source_list.rb
d17082
+++ b/lib/bundler/source_list.rb
d17082
@@ -64,6 +64,10 @@ def rubygems_sources
d17082
       @rubygems_sources + [default_source]
d17082
     end
d17082
 
d17082
+    def non_global_rubygems_sources
d17082
+      @rubygems_sources
d17082
+    end
d17082
+
d17082
     def rubygems_remotes
d17082
       rubygems_sources.map(&:remotes).flatten.uniq
d17082
     end