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])
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
'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
(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
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
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