diff --git a/config/routes.rb b/config/routes.rb index 6d73586..78f75d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -222,14 +222,14 @@ Gitlab::Application.routes.draw do end end - resources :branches, only: [:index, :new, :create, :destroy], constraints: { id: /[a-zA-Z.\/0-9_\-#%+]+/ } do + resources :branches, only: [:index, :new, :create, :destroy], constraints: { id: Gitlab::Regex.git_reference_regex } do collection do - get :recent + get :recent, constraints: { id: Gitlab::Regex.git_reference_regex } end end - resources :tags, only: [:index, :new, :create, :destroy], constraints: { id: /[a-zA-Z.\/0-9_\-#%+]+/ } - resources :protected_branches, only: [:index, :create, :destroy], constraints: { id: /[a-zA-Z.\/0-9_\-#%+]+/ } + resources :tags, only: [:index, :new, :create, :destroy], constraints: { id: Gitlab::Regex.git_reference_regex } + resources :protected_branches, only: [:index, :create, :destroy], constraints: { id: Gitlab::Regex.git_reference_regex } resources :refs, only: [] do collection do @@ -238,11 +238,11 @@ Gitlab::Application.routes.draw do member do # tree viewer logs - get "logs_tree", constraints: { id: /[a-zA-Z.\/0-9_\-#%+]+/ } + get "logs_tree", constraints: { id: Gitlab::Regex.git_reference_regex } get "logs_tree/:path" => "refs#logs_tree", as: :logs_file, constraints: { - id: /[a-zA-Z.0-9\/_\-#%+]+/, + id: Gitlab::Regex.git_reference_regex, path: /.*/ } end diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index b4be46d..55aa240 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -18,6 +18,29 @@ module Gitlab default_regex end + def git_reference_regex + # Valid git ref regex, see: + # https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html + + %r{ + (?! + # doesn't begins with + \/| # (rule #6) + # doesn't contain + .*(?: + [\/.]\.| # (rule #1,3) + \/\/| # (rule #6) + @\{| # (rule #8) + \\ # (rule #9) + ) + ) + [^\000-\040\177~^:?*\[]+ # (rule #4-5) + # doesn't end with + (?