Module: Gitlab::Regex

Extended by:
Regex, MergeRequests, Packages
Included in:
Regex
Defined in:
lib/gitlab/regex.rb,
lib/gitlab/regex/sem_ver.rb,
lib/gitlab/regex/packages.rb,
lib/gitlab/regex/merge_requests.rb,
lib/gitlab/regex/packages/protection/rules.rb,
lib/gitlab/regex/container_registry/protection/rules.rb

Defined Under Namespace

Modules: ContainerRegistry, MergeRequests, Packages, SemVer

Constant Summary collapse

MARKDOWN_CODE_BLOCK_REGEX_UNTRUSTED =

Code blocks: “‘ Anything, including `>>>` blocks which are ignored by this filter “`

'(?P<code>' \
  '^```.*?\n' \
  '(?:\n|.)*?' \
  '\n```\ *$' \
')'
MARKDOWN_HTML_BLOCK_REGEX_UNTRUSTED =

HTML block: <tag> Anything, including ‘>>>` blocks which are ignored by this filter </tag>

'(?P<html>' \
  '^<[^>]+?>\ *\n' \
  '(?:\n|.)*?' \
  '\n<\/[^>]+?>\ *$' \
')'
MARKDOWN_HTML_COMMENT_LINE_REGEX_UNTRUSTED =

HTML comment line: <!– some commented text –>

'(?P<html_comment_line>' \
  '^<!--\ .*?\ -->\ *$' \
')'
MARKDOWN_HTML_COMMENT_BLOCK_REGEX_UNTRUSTED =
'(?P<html_comment_block>' \
  '^<!--.*?\n' \
  '(?:\n|.)*?' \
  '\n.*?-->\ *$' \
')'

Constants included from Packages

Packages::API_PATH_REGEX, Packages::CONAN_PACKAGE_FILES, Packages::CONAN_RECIPE_FILES, Packages::MAVEN_SNAPSHOT_DYNAMIC_PARTS, Packages::PYPI_NORMALIZED_NAME_REGEX_STRING

Instance Method Summary collapse

Methods included from MergeRequests

git_diff_prefix, merge_request, merge_request_draft

Methods included from Packages

_semver_major_minor_patch_regex, _semver_major_regex, _semver_minor_regex, _semver_patch_regex, _semver_prerelease_build_regex, composer_dev_version_regex, composer_package_version_regex, conan_package_reference_regex, conan_recipe_component_regex, conan_recipe_user_channel_regex, conan_revision_regex, debian_architecture_regex, debian_component_regex, debian_direct_upload_filename_regex, debian_distribution_regex, debian_package_name_regex, debian_version_regex, generic_package_file_name_regex, generic_package_name_regex, generic_package_version_regex, go_package_regex, helm_channel_regex, helm_package_regex, helm_version_regex, maven_app_group_regex, maven_app_name_regex, maven_file_name_regex, maven_path_regex, maven_version_regex, npm_package_name_regex, npm_package_name_regex_message, nuget_package_name_regex, nuget_version_regex, package_name_regex, prefixed_semver_regex, pypi_version_regex, semver_regex, semver_regex_message, sha256_regex, slack_link_regex, terraform_module_package_name_regex, unbounded_semver_regex

Instance Method Details

#aws_account_id_messageObject



232
233
234
# File 'lib/gitlab/regex.rb', line 232

def 
  'must be a 12-digit number'
end

#aws_account_id_regexObject



228
229
230
# File 'lib/gitlab/regex.rb', line 228

def 
  /\A\d{12}\z/
end

#aws_arn_regexObject



237
238
239
# File 'lib/gitlab/regex.rb', line 237

def aws_arn_regex
  /\Aarn:\S+\z/
end

#aws_arn_regex_messageObject



241
242
243
# File 'lib/gitlab/regex.rb', line 241

def aws_arn_regex_message
  'must be a valid Amazon Resource Name'
end

#base64_regexObject



257
258
259
# File 'lib/gitlab/regex.rb', line 257

def base64_regex
  @base64_regex ||= %r{(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?}
end

#breakline_regexObject



223
224
225
# File 'lib/gitlab/regex.rb', line 223

def breakline_regex
  @breakline_regex ||= /\r\n|\r|\n/
end

#build_trace_section_regexObject



138
139
140
141
142
143
144
# File 'lib/gitlab/regex.rb', line 138

def build_trace_section_regex
  @build_trace_section_regexp ||= %r{
    #{logs_section_prefix_regex}
    #{logs_section_options_regex}
    #{logs_section_suffix_regex}
  }x
end

#cluster_agent_name_regexObject



91
92
93
# File 'lib/gitlab/regex.rb', line 91

def cluster_agent_name_regex
  /\A[a-z0-9]([-a-z0-9]*[a-z0-9])?\z/
end

#cluster_agent_name_regex_messageObject



95
96
97
# File 'lib/gitlab/regex.rb', line 95

def cluster_agent_name_regex_message
  %q(can contain only lowercase letters, digits, and '-', but cannot start or end with '-')
end

#container_registry_tag_regexObject

We do not use regexp anchors here because these are not allowed when used as a routing constraint.



58
59
60
# File 'lib/gitlab/regex.rb', line 58

def container_registry_tag_regex
  @container_registry_tag_regex ||= /\w[\w.-]{0,127}/
end

#container_repository_name_regex(other_accepted_chars = nil) ⇒ Object

Docker Distribution Registry repository / tag name rules

See github.com/docker/distribution/blob/master/reference/regexp.go.



48
49
50
51
52
# File 'lib/gitlab/regex.rb', line 48

def container_repository_name_regex(other_accepted_chars = nil)
  strong_memoize_with(:container_repository_name_regex, other_accepted_chars) do
    %r{\A[a-z0-9]+(([._/]|__|-*)[a-z0-9#{other_accepted_chars}])*\z}
  end
end

#environment_name_regexObject



70
71
72
# File 'lib/gitlab/regex.rb', line 70

def environment_name_regex
  @environment_name_regex ||= /\A[#{environment_name_regex_chars_without_slash}]([#{environment_name_regex_chars}]*[#{environment_name_regex_chars_without_slash}])?\z/
end

#environment_name_regex_charsObject



62
63
64
# File 'lib/gitlab/regex.rb', line 62

def environment_name_regex_chars
  'a-zA-Z0-9_/\\$\\{\\}\\. \\-'
end

#environment_name_regex_chars_without_slashObject



66
67
68
# File 'lib/gitlab/regex.rb', line 66

def environment_name_regex_chars_without_slash
  'a-zA-Z0-9_\\$\\{\\}\\. -'
end

#environment_name_regex_messageObject



74
75
76
# File 'lib/gitlab/regex.rb', line 74

def environment_name_regex_message
  "can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.', and spaces, but it cannot start or end with '/'"
end

#environment_scope_regexObject



82
83
84
# File 'lib/gitlab/regex.rb', line 82

def environment_scope_regex
  @environment_scope_regex ||= /\A[#{environment_scope_regex_chars}]+\z/
end

#environment_scope_regex_charsObject



78
79
80
# File 'lib/gitlab/regex.rb', line 78

def environment_scope_regex_chars
  "#{environment_name_regex_chars}\\*"
end

#environment_scope_regex_messageObject



86
87
88
# File 'lib/gitlab/regex.rb', line 86

def environment_scope_regex_message
  "can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.', '*' and spaces"
end

#environment_slug_regexObject



114
115
116
# File 'lib/gitlab/regex.rb', line 114

def environment_slug_regex
  @environment_slug_regex ||= /\A[a-z]([a-z0-9-]*[a-z0-9])?\z/
end

#environment_slug_regex_messageObject



118
119
120
121
# File 'lib/gitlab/regex.rb', line 118

def environment_slug_regex_message
  "can contain only lowercase letters, digits, and '-'. " \
  "Must start with a letter, and cannot end with '-'"
end

#feature_flag_regexObject



261
262
263
# File 'lib/gitlab/regex.rb', line 261

def feature_flag_regex
  /\A[a-z]([-_a-z0-9]*[a-z0-9])?\z/
end

#group_name_regexObject



30
31
32
# File 'lib/gitlab/regex.rb', line 30

def group_name_regex
  @group_name_regex ||= /\A#{group_name_regex_chars}\z/
end

#group_name_regex_charsObject



34
35
36
# File 'lib/gitlab/regex.rb', line 34

def group_name_regex_chars
  @group_name_regex_chars ||= /[\p{Alnum}\u{00A9}-\u{1f9ff}_][\p{Alnum}\p{Pd}\u{00A9}-\u{1f9ff}_()\. ]*/
end

#group_name_regex_messageObject



38
39
40
41
# File 'lib/gitlab/regex.rb', line 38

def group_name_regex_message
  "can contain only letters, digits, emoji, '_', '.', dash, space, parenthesis. " \
  "It must start with letter, digit, emoji or '_'."
end

#issueObject



249
250
251
# File 'lib/gitlab/regex.rb', line 249

def issue
  @issue ||= /(?<issue>\d+)(?<format>\+s{,1})?(?=\W|\z)/
end

#jira_issue_key_project_key_extraction_regexObject



215
216
217
# File 'lib/gitlab/regex.rb', line 215

def jira_issue_key_project_key_extraction_regex
  @jira_issue_key_project_key_extraction_regex ||= /-\d+/
end

#jira_issue_key_regex(expression_escape: '\b') ⇒ Object



211
212
213
# File 'lib/gitlab/regex.rb', line 211

def jira_issue_key_regex(expression_escape: '\b')
  /#{expression_escape}([A-Z][A-Z_0-9]+-\d+)/
end

#jira_transition_id_regexObject



219
220
221
# File 'lib/gitlab/regex.rb', line 219

def jira_transition_id_regex
  @jira_transition_id_regex ||= /\d+/
end

#kubernetes_dns_subdomain_regexObject

Pod name adheres to DNS Subdomain Names(RFC 1123) naming convention kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names



110
111
112
# File 'lib/gitlab/regex.rb', line 110

def kubernetes_dns_subdomain_regex
  /\A[a-z0-9]([a-z0-9\-\.]*[a-z0-9])?\z/
end

#kubernetes_namespace_regexObject



99
100
101
# File 'lib/gitlab/regex.rb', line 99

def kubernetes_namespace_regex
  /\A[a-z0-9]([-a-z0-9]*[a-z0-9])?\z/
end

#kubernetes_namespace_regex_messageObject



103
104
105
106
# File 'lib/gitlab/regex.rb', line 103

def kubernetes_namespace_regex_message
  "can contain only lowercase letters, digits, and '-'. " \
  "Must start with a letter, and cannot end with '-'"
end

#logs_section_options_regexObject

The optional section options, e.g. [collapsed=true]



129
130
131
# File 'lib/gitlab/regex.rb', line 129

def logs_section_options_regex
  /(\[(?:\w+=\w+)(?:, ?(?:\w+=\w+))*\])?/
end

#logs_section_prefix_regexObject

The section start, e.g. section_start:12345678:NAME



124
125
126
# File 'lib/gitlab/regex.rb', line 124

def logs_section_prefix_regex
  /section_((?:start)|(?:end)):(\d+):([a-zA-Z0-9_.-]+)/
end

#logs_section_suffix_regexObject

The region end, always: re[0K



134
135
136
# File 'lib/gitlab/regex.rb', line 134

def logs_section_suffix_regex
  /\r\033\[0K/
end

#markdown_code_or_html_blocks_or_html_comments_untrustedObject



198
199
200
201
202
203
204
205
206
207
# File 'lib/gitlab/regex.rb', line 198

def markdown_code_or_html_blocks_or_html_comments_untrusted
  @markdown_code_or_html_comments_untrusted ||=
    "#{MARKDOWN_CODE_BLOCK_REGEX_UNTRUSTED}" \
    "|" \
    "#{MARKDOWN_HTML_BLOCK_REGEX_UNTRUSTED}" \
    "|" \
    "#{MARKDOWN_HTML_COMMENT_LINE_REGEX_UNTRUSTED}" \
    "|" \
    "#{MARKDOWN_HTML_COMMENT_BLOCK_REGEX_UNTRUSTED}"
end

#markdown_code_or_html_blocks_untrustedObject



182
183
184
185
186
187
# File 'lib/gitlab/regex.rb', line 182

def markdown_code_or_html_blocks_untrusted
  @markdown_code_or_html_blocks_untrusted ||=
    "#{MARKDOWN_CODE_BLOCK_REGEX_UNTRUSTED}" \
    "|" \
    "#{MARKDOWN_HTML_BLOCK_REGEX_UNTRUSTED}"
end

#markdown_code_or_html_comments_untrustedObject



189
190
191
192
193
194
195
196
# File 'lib/gitlab/regex.rb', line 189

def markdown_code_or_html_comments_untrusted
  @markdown_code_or_html_comments_untrusted ||=
    "#{MARKDOWN_CODE_BLOCK_REGEX_UNTRUSTED}" \
    "|" \
    "#{MARKDOWN_HTML_COMMENT_LINE_REGEX_UNTRUSTED}" \
    "|" \
    "#{MARKDOWN_HTML_COMMENT_BLOCK_REGEX_UNTRUSTED}"
end

#ml_model_file_name_regexObject



282
283
284
# File 'lib/gitlab/regex.rb', line 282

def ml_model_file_name_regex
  @ml_model_file_name_regex ||= %r{\A[A-Za-z0-9\.\_\-\+ ]+\z}
end

#ml_model_name_regexObject



274
275
276
# File 'lib/gitlab/regex.rb', line 274

def ml_model_name_regex
  package_name_regex
end

#ml_model_version_name_regexObject



278
279
280
# File 'lib/gitlab/regex.rb', line 278

def ml_model_version_name_regex
  Regexp.union(semver_regex, /candidate_\d+/)
end

#oci_repository_path_regexObject

Project path must conform to this regex. See gitlab.com/gitlab-org/gitlab/-/issues/27483



22
23
24
# File 'lib/gitlab/regex.rb', line 22

def oci_repository_path_regex
  @oci_repository_path_regex ||= %r{\A[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*\z}
end

#oci_repository_path_regex_messageObject



26
27
28
# File 'lib/gitlab/regex.rb', line 26

def oci_repository_path_regex_message
  "can only include non-accented letters, digits, '_', '-' and '.'. It must not start with '-', end in '.', '.git', or '.atom'."
end

#project_name_regexObject



9
10
11
12
13
14
# File 'lib/gitlab/regex.rb', line 9

def project_name_regex
  # The character range \p{Alnum} overlaps with \u{00A9}-\u{1f9ff}
  # hence the Ruby warning.
  # https://gitlab.com/gitlab-org/gitlab/merge_requests/23165#not-easy-fixable
  @project_name_regex ||= /\A[\p{Alnum}\u{00A9}-\u{1f9ff}_][\p{Alnum}\p{Pd}\u{002B}\u{00A9}-\u{1f9ff}_\. ]*\z/
end

#project_name_regex_messageObject



16
17
18
19
# File 'lib/gitlab/regex.rb', line 16

def project_name_regex_message
  "can contain only letters, digits, emoji, '_', '.', '+', dashes, or spaces. " \
  "It must start with a letter, digit, emoji, or '_'."
end

#sep_by_1(separator, part) ⇒ Object

One or more ‘part`s, separated by separator



266
267
268
# File 'lib/gitlab/regex.rb', line 266

def sep_by_1(separator, part)
  %r{#{part} (#{separator} #{part})*}x
end

#utc_date_regexObject



245
246
247
# File 'lib/gitlab/regex.rb', line 245

def utc_date_regex
  @utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/
end

#work_itemObject



253
254
255
# File 'lib/gitlab/regex.rb', line 253

def work_item
  @work_item ||= /(?<work_item>\d+)(?<format>\+s{,1})?(?=\W|\z)/
end

#x509_subject_key_identifier_regexObject



270
271
272
# File 'lib/gitlab/regex.rb', line 270

def x509_subject_key_identifier_regex
  @x509_subject_key_identifier_regex ||= /\A(?:\h{2}:)*\h{2}\z/
end