]> source.dussan.org Git - redmine.git/commitdiff
Merged r20962 from trunk to 4.1-stable (#35085).
authorGo MAEDA <maeda@farend.jp>
Fri, 23 Apr 2021 01:44:28 +0000 (01:44 +0000)
committerGo MAEDA <maeda@farend.jp>
Fri, 23 Apr 2021 01:44:28 +0000 (01:44 +0000)
git-svn-id: http://svn.redmine.org/redmine/branches/4.1-stable@20964 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/repositories_controller.rb
app/models/repository.rb
lib/redmine/scm/adapters/abstract_adapter.rb
lib/redmine/scm/adapters/git_adapter.rb
lib/redmine/scm/adapters/mercurial_adapter.rb

index ef903f251f805c81f37844970d01e558b41904cf..69c38584e54c60479c914b94a70de7454d51143c 100644 (file)
@@ -307,7 +307,7 @@ class RepositoriesController < ApplicationController
     render_404
   end
 
-  REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
+  REV_PARAM_RE = %r{\A[a-f0-9]*\z}i
 
   def find_project_repository
     @project = Project.find(params[:id])
@@ -318,14 +318,12 @@ class RepositoriesController < ApplicationController
     end
     (render_404; return false) unless @repository
     @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
-    @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
-    @rev_to = params[:rev_to]
 
-    unless REV_PARAM_RE.match?(@rev.to_s) && REV_PARAM_RE.match?(@rev_to.to_s)
-      if @repository.branches.blank?
-        raise InvalidRevisionParam
-      end
-    end
+    @rev = params[:rev].to_s.strip.presence || @repository.default_branch
+    raise InvalidRevisionParam unless valid_name?(@rev)
+
+    @rev_to = params[:rev_to].to_s.strip.presence
+    raise InvalidRevisionParam unless valid_name?(@rev_to)
   rescue ActiveRecord::RecordNotFound
     render_404
   rescue InvalidRevisionParam
@@ -410,4 +408,11 @@ class RepositoriesController < ApplicationController
       'attachment'
     end
   end
+
+  def valid_name?(rev)
+    return true if rev.nil?
+    return true if REV_PARAM_RE.match?(rev)
+
+    @repository ? @repository.valid_name?(rev) : true
+  end
 end
index 089027c9d3843784a1943dce9caa602344ce9d4d..4aca619e0cbfcec84e9a4429c07f9b27bb8d0853 100644 (file)
@@ -461,6 +461,10 @@ class Repository < ActiveRecord::Base
     scope
   end
 
+  def valid_name?(name)
+    scm.valid_name?(name)
+  end
+
   protected
 
   # Validates repository url based against an optional regular expression
index 14f8f09c46b13e0e3609523dc0266f6746222daa..226c8a148672098cff152fc93dfcbc4c4bded8fa 100644 (file)
@@ -178,6 +178,14 @@ module Redmine
           (path[-1,1] == "/") ? path[0..-2] : path
         end
 
+        def valid_name?(name)
+          return true if name.nil?
+          return true if name.is_a?(Integer) && name > 0
+          return true if name.is_a?(String) && name =~ /\A[0-9]*\z/
+
+          false
+        end
+
         private
 
         def retrieve_root_url
index ddb1055f320fe0d49cab1c0b5b7d78464206cb1b..d833369b6c6c4739c853fb99eeaa41d8e672035b 100644 (file)
@@ -388,6 +388,18 @@ module Redmine
           nil
         end
 
+        def valid_name?(name)
+          return false unless name.is_a?(String)
+
+          return false if name.start_with?('-', '/', 'refs/heads/', 'refs/remotes/')
+          return false if name == 'HEAD'
+
+          git_cmd ['show-ref', '--heads', '--tags', '--quiet', '--', name]
+          true
+        rescue ScmCommandAborted
+          false
+        end
+
         class Revision < Redmine::Scm::Adapters::Revision
           # Returns the readable identifier
           def format_identifier
index fd3c80de3f43046fc66cf6b0ad074d4c5aab4ad5..1ef808f0308b73258bad9c6103b1afb901b6322d 100644 (file)
@@ -291,6 +291,15 @@ module Redmine
           Annotate.new
         end
 
+        def valid_name?(name)
+          return false unless name.nil? || name.is_a?(String)
+
+          # Mercurials names don't need to be checked further as its CLI
+          # interface is restrictive enough to reject any invalid names on its
+          # own.
+          true
+        end
+
         class Revision < Redmine::Scm::Adapters::Revision
           # Returns the readable identifier
           def format_identifier