]> source.dussan.org Git - redmine.git/commitdiff
Stricter validation of given revisions of repositories (#35085).
authorGo MAEDA <maeda@farend.jp>
Fri, 23 Apr 2021 00:46:45 +0000 (00:46 +0000)
committerGo MAEDA <maeda@farend.jp>
Fri, 23 Apr 2021 00:46:45 +0000 (00:46 +0000)
Patch by Holger Just.

git-svn-id: http://svn.redmine.org/redmine/trunk@20962 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 dfa96cc0ea8f3ccaef159044ae5a078801a800b9..45e969ba45b09cbc71d5045184be239db28abbb2 100644 (file)
@@ -330,7 +330,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])
@@ -341,14 +341,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
@@ -433,4 +431,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 e65c800e63727f30752d67a5ffb644c136ee2714..4e76c3453596d7150e3f1aa838e0e7744dc4f514 100644 (file)
@@ -463,6 +463,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 dae6218663a23256da7cca3cbde17775ecd05fa7..d0c3293a6e4f23f88604c9ef5a8c4ad8083f5867 100644 (file)
@@ -176,6 +176,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 b85bd029681cb1fbc3db0a1da7ba9ce5920f4cfc..363476cd44d1024c11f7699cbf7e0a83345e66e7 100644 (file)
@@ -420,6 +420,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 6ab5ec69de65b1fea1bae6f19dfea2017f26234b..075636ce1bd05627562000dbd15f7c84613c9b81 100644 (file)
@@ -296,6 +296,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