# frozen_string_literal: true module Gitlab module PathRegex extend self # All routes that appear on the top level must be listed here. # This will make sure that groups cannot be created with these names # as these routes would be masked by the paths already in place. # # Example: # /api/api-project # # the path `api` shouldn't be allowed because it would be masked by `api/*` # TOP_LEVEL_ROUTES = %w[ - .well-known 404.html 422.html 500.html 502.html 503.html admin api apple-touch-icon.png assets dashboard deploy.html explore favicon.ico favicon.png files groups health_check help import jwt login oauth profile projects public robots.txt s search sitemap sitemap.xml sitemap.xml.gz slash-command-logo.png snippets unsubscribes uploads users v2 ].freeze # NOTE: Do not add new items to this list unless necessary as this will # cause conflicts with existing namespaced routes for groups or projects. # See https://docs.gitlab.com/ee/development/routing.html#project-routes # # This list should contain all words following `/*namespace_id/:project_id` in # routes that contain a second wildcard. # # Example: # /*namespace_id/:project_id/badges/*ref/build # # If `badges` was allowed as a project/group name, we would not be able to access the # `badges` route for those projects: # # Consider a namespace with path `foo/bar` and a project called `badges`. # The route to the build badge would then be `/foo/bar/badges/badges/master/build.svg` # # When accessing this path the route would be matched to the `badges` path # with the following params: # - namespace_id: `foo` # - project_id: `bar` # - ref: `badges/master` # # Failing to find the project, this would result in a 404. # # By rejecting `badges` the router can _count_ on the fact that `badges` will # be preceded by the `namespace/project`. PROJECT_WILDCARD_ROUTES = %w[ - badges blame blob builds commits create create_dir edit environments/folders files find_file gitlab-lfs/objects info/lfs/objects new preview raw refs tree update wikis ].freeze # NOTE: Do not add new items to this list unless necessary as this will # cause conflicts with existing namespaced routes for groups or projects. # See https://docs.gitlab.com/ee/development/routing.html#group-routes # # These are all the paths that follow `/groups/*id/ or `/groups/*group_id` # We need to reject these because we have a `/groups/*id` page that is the same # as the `/*id`. # # If we would allow a subgroup to be created with the name `activity` then # this group would not be accessible through `/groups/parent/activity` since # this would map to the activity-page of its parent. GROUP_ROUTES = %w[ - ].freeze ILLEGAL_PROJECT_PATH_WORDS = PROJECT_WILDCARD_ROUTES ILLEGAL_GROUP_PATH_WORDS = (PROJECT_WILDCARD_ROUTES | GROUP_ROUTES).freeze # The namespace regex is used in JavaScript to validate usernames in the "Register" form. However, Javascript # does not support the negative lookbehind assertion (?#{full_namespace_route_regex})\/(?#{project_route_regex})\.git\z} end def namespace_format_regex @namespace_format_regex ||= /\A#{NAMESPACE_FORMAT_REGEX}\z/o.freeze end def namespace_format_message "can contain only letters, digits, '_', '-' and '.'. " \ "Cannot start with '-' or end in '.', '.git' or '.atom'." \ end def project_path_format_regex @project_path_format_regex ||= /\A#{PROJECT_PATH_FORMAT_REGEX}\z/o.freeze end def project_path_format_message "can contain only letters, digits, '_', '-' and '.'. " \ "Cannot start with '-', end in '.git' or end in '.atom'" \ end def archive_formats_regex # |zip|tar| tar.gz | tar.bz2 | @archive_formats_regex ||= /(zip|tar|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/.freeze end def git_reference_regex # Valid git ref regex, see: # https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html @git_reference_regex ||= single_line_regexp %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) (?