2012-11-27 22:14:05 -05:00
|
|
|
module Gitlab
|
|
|
|
module Regex
|
|
|
|
extend self
|
|
|
|
|
2016-11-16 07:24:43 -05:00
|
|
|
# The namespace regex is used in Javascript to validate usernames in the "Register" form. However, Javascript
|
|
|
|
# does not support the negative lookbehind assertion (?<!) that disallows usernames ending in `.git` and `.atom`.
|
|
|
|
# Since this is a non-trivial problem to solve in Javascript (heavily complicate the regex, modify view code to
|
|
|
|
# allow non-regex validatiions, etc), `NAMESPACE_REGEX_STR_SIMPLE` serves as a Javascript-compatible version of
|
|
|
|
# `NAMESPACE_REGEX_STR`, with the negative lookbehind assertion removed. This means that the client-side validation
|
|
|
|
# will pass for usernames ending in `.atom` and `.git`, but will be caught by the server-side validation.
|
2016-11-14 09:55:31 -05:00
|
|
|
PATH_REGEX_STR = '[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*'.freeze
|
|
|
|
NAMESPACE_REGEX_STR_SIMPLE = PATH_REGEX_STR + '[a-zA-Z0-9_\-]|[a-zA-Z0-9_]'.freeze
|
2016-11-21 14:03:18 -05:00
|
|
|
NAMESPACE_REGEX_STR = '(?:' + NAMESPACE_REGEX_STR_SIMPLE + ')(?<!\.git|\.atom)'.freeze
|
2016-11-14 09:55:31 -05:00
|
|
|
PROJECT_REGEX_STR = PATH_REGEX_STR + '(?<!\.git|\.atom)'.freeze
|
2015-03-24 10:21:37 -04:00
|
|
|
|
2015-04-24 09:16:38 -04:00
|
|
|
def namespace_regex
|
|
|
|
@namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze
|
2012-11-27 22:14:05 -05:00
|
|
|
end
|
|
|
|
|
2016-11-07 11:35:14 -05:00
|
|
|
def namespace_route_regex
|
|
|
|
@namespace_route_regex ||= /#{NAMESPACE_REGEX_STR}/.freeze
|
|
|
|
end
|
|
|
|
|
2015-04-24 09:16:38 -04:00
|
|
|
def namespace_regex_message
|
2015-03-24 09:55:14 -04:00
|
|
|
"can contain only letters, digits, '_', '-' and '.'. " \
|
2016-08-25 12:48:08 -04:00
|
|
|
"Cannot start with '-' or end in '.', '.git' or '.atom'." \
|
2015-03-24 09:55:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def namespace_name_regex
|
2015-02-08 18:01:31 -05:00
|
|
|
@namespace_name_regex ||= /\A[\p{Alnum}\p{Pd}_\. ]*\z/.freeze
|
2014-06-26 03:53:01 -04:00
|
|
|
end
|
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
def namespace_name_regex_message
|
2015-02-08 18:01:31 -05:00
|
|
|
"can contain only letters, digits, '_', '.', dash and space."
|
2015-03-24 09:55:14 -04:00
|
|
|
end
|
|
|
|
|
2012-12-25 17:50:41 -05:00
|
|
|
def project_name_regex
|
2016-11-11 06:27:23 -05:00
|
|
|
@project_name_regex ||= /\A[\p{Alnum}\u{00A9}-\u{1f9c0}_][\p{Alnum}\p{Pd}\u{00A9}-\u{1f9c0}_\. ]*\z/.freeze
|
2012-12-25 17:50:41 -05:00
|
|
|
end
|
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
def project_name_regex_message
|
2016-11-11 06:27:23 -05:00
|
|
|
"can contain only letters, digits, emojis, '_', '.', dash, space. " \
|
|
|
|
"It must start with letter, digit, emoji or '_'."
|
2014-06-26 03:53:01 -04:00
|
|
|
end
|
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
def project_path_regex
|
2016-11-14 09:55:31 -05:00
|
|
|
@project_path_regex ||= /\A#{PROJECT_REGEX_STR}\z/.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_route_regex
|
|
|
|
@project_route_regex ||= /#{PROJECT_REGEX_STR}/.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_git_route_regex
|
|
|
|
@project_route_git_regex ||= /#{PATH_REGEX_STR}\.git/.freeze
|
2013-02-18 02:28:18 -05:00
|
|
|
end
|
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
def project_path_regex_message
|
|
|
|
"can contain only letters, digits, '_', '-' and '.'. " \
|
2016-01-29 09:35:21 -05:00
|
|
|
"Cannot start with '-', end in '.git' or end in '.atom'" \
|
2014-06-26 03:53:01 -04:00
|
|
|
end
|
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
def file_name_regex
|
2016-12-08 16:53:15 -05:00
|
|
|
@file_name_regex ||= /\A[[[:alnum:]]_\-\.\@]*\z/.freeze
|
2012-11-27 22:14:05 -05:00
|
|
|
end
|
2014-01-14 12:26:01 -05:00
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
def file_name_regex_message
|
2016-09-20 00:20:48 -04:00
|
|
|
"can contain only letters, digits, '_', '-', '@' and '.'."
|
2014-06-26 03:53:01 -04:00
|
|
|
end
|
|
|
|
|
2015-10-19 17:52:46 -04:00
|
|
|
def file_path_regex
|
2016-12-08 16:53:15 -05:00
|
|
|
@file_path_regex ||= /\A[[[:alnum:]]_\-\.\/\@]*\z/.freeze
|
2015-10-19 17:52:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def file_path_regex_message
|
2016-09-20 00:20:48 -04:00
|
|
|
"can contain only letters, digits, '_', '-', '@' and '.'. Separate directories with a '/'."
|
2015-10-19 17:52:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def directory_traversal_regex
|
|
|
|
@directory_traversal_regex ||= /\.{2}/.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def directory_traversal_regex_message
|
2016-09-20 00:20:48 -04:00
|
|
|
"cannot include directory traversal."
|
2015-10-19 17:52:46 -04:00
|
|
|
end
|
|
|
|
|
2013-12-20 14:12:44 -05:00
|
|
|
def archive_formats_regex
|
2015-03-24 09:55:14 -04:00
|
|
|
# |zip|tar| tar.gz | tar.bz2 |
|
|
|
|
@archive_formats_regex ||= /(zip|tar|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/.freeze
|
2013-12-20 14:12:44 -05:00
|
|
|
end
|
2012-11-27 22:14:05 -05:00
|
|
|
|
2013-09-02 06:48:32 -04:00
|
|
|
def git_reference_regex
|
|
|
|
# Valid git ref regex, see:
|
|
|
|
# https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
|
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
@git_reference_regex ||= %r{
|
2013-09-02 06:48:32 -04:00
|
|
|
(?!
|
2013-12-23 21:01:38 -05:00
|
|
|
(?# doesn't begins with)
|
|
|
|
\/| (?# rule #6)
|
|
|
|
(?# doesn't contain)
|
2013-09-02 06:48:32 -04:00
|
|
|
.*(?:
|
2013-12-23 21:01:38 -05:00
|
|
|
[\/.]\.| (?# rule #1,3)
|
|
|
|
\/\/| (?# rule #6)
|
|
|
|
@\{| (?# rule #8)
|
|
|
|
\\ (?# rule #9)
|
2013-09-02 06:48:32 -04:00
|
|
|
)
|
|
|
|
)
|
2013-12-23 21:01:38 -05:00
|
|
|
[^\000-\040\177~^:?*\[]+ (?# rule #4-5)
|
|
|
|
(?# doesn't end with)
|
|
|
|
(?<!\.lock) (?# rule #1)
|
|
|
|
(?<![\/.]) (?# rule #6-7)
|
2015-03-24 09:55:14 -04:00
|
|
|
}x.freeze
|
2012-11-27 22:14:05 -05:00
|
|
|
end
|
2016-04-18 08:14:40 -04:00
|
|
|
|
2016-05-08 16:50:30 -04:00
|
|
|
def container_registry_reference_regex
|
2016-04-18 08:14:40 -04:00
|
|
|
git_reference_regex
|
|
|
|
end
|
2016-06-14 07:04:21 -04:00
|
|
|
|
|
|
|
def environment_name_regex
|
2016-09-19 03:01:04 -04:00
|
|
|
@environment_name_regex ||= /\A[a-zA-Z0-9_\\\/\${}. -]+\z/.freeze
|
2016-06-14 07:04:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def environment_name_regex_message
|
2016-09-19 03:01:04 -04:00
|
|
|
"can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.' and spaces"
|
2016-06-14 07:04:21 -04:00
|
|
|
end
|
2016-12-08 11:36:26 -05:00
|
|
|
|
|
|
|
def kubernetes_namespace_regex
|
|
|
|
/\A[a-z0-9]([-a-z0-9]*[a-z0-9])?\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def kubernetes_namespace_regex_message
|
|
|
|
"can contain only letters, digits or '-', and cannot start or end with '-'"
|
|
|
|
end
|
2016-12-07 20:09:18 -05:00
|
|
|
|
|
|
|
def environment_slug_regex
|
|
|
|
@environment_slug_regex ||= /\A[a-z]([a-z0-9-]*[a-z0-9])?\z/.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def environment_slug_regex_message
|
|
|
|
"can contain only lowercase letters, digits, and '-'. " \
|
|
|
|
"Must start with a letter, and cannot end with '-'"
|
|
|
|
end
|
2012-11-27 22:14:05 -05:00
|
|
|
end
|
|
|
|
end
|