Module: Gitlab::Regex

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

Defined Under Namespace

Modules: MergeRequests, Packages

Constant Summary collapse

MARKDOWN_CODE_BLOCK_REGEX =
%r{
  (?<code>
    # Code blocks:
    # ```
    # Anything, including `>>>` blocks which are ignored by this filter
    # ```

    ^```
    .+?
    \n```\ *$
  )
}mx
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 =
%r{
  (?<html>
    # HTML block:
    # <tag>
    # Anything, including `>>>` blocks which are ignored by this filter
    # </tag>

    ^<[^>]+?>\ *\n
    .+?
    \n<\/[^>]+?>\ *$
  )
}mx
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



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

def 
  'must be a 12-digit number'
end

#aws_account_id_regexObject



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

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

#aws_arn_regexObject



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

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

#aws_arn_regex_messageObject



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

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

#base64_regexObject



291
292
293
# File 'lib/gitlab/regex.rb', line 291

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

#breakline_regexObject



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

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

#build_trace_section_regexObject



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

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



89
90
91
# File 'lib/gitlab/regex.rb', line 89

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

#cluster_agent_name_regex_messageObject



93
94
95
# File 'lib/gitlab/regex.rb', line 93

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.



56
57
58
# File 'lib/gitlab/regex.rb', line 56

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

#container_repository_name_regexObject

Docker Distribution Registry repository / tag name rules

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



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

def container_repository_name_regex
  @container_repository_regex ||= %r{\A[a-z0-9]+(([._/]|__|-*)[a-z0-9])*\z}
end

#environment_name_regexObject



68
69
70
# File 'lib/gitlab/regex.rb', line 68

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



60
61
62
# File 'lib/gitlab/regex.rb', line 60

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

#environment_name_regex_chars_without_slashObject



64
65
66
# File 'lib/gitlab/regex.rb', line 64

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

#environment_name_regex_messageObject



72
73
74
# File 'lib/gitlab/regex.rb', line 72

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

#environment_scope_regexObject



80
81
82
# File 'lib/gitlab/regex.rb', line 80

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

#environment_scope_regex_charsObject



76
77
78
# File 'lib/gitlab/regex.rb', line 76

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

#environment_scope_regex_messageObject



84
85
86
# File 'lib/gitlab/regex.rb', line 84

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

#environment_slug_regexObject



112
113
114
# File 'lib/gitlab/regex.rb', line 112

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

#environment_slug_regex_messageObject



116
117
118
119
# File 'lib/gitlab/regex.rb', line 116

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



295
296
297
# File 'lib/gitlab/regex.rb', line 295

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



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

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

#jira_issue_key_project_key_extraction_regexObject



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

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

Based on Jira’s project key format confluence.atlassian.com/adminjiraserver073/changing-the-project-key-format-861253229.html Avoids linking CVE IDs (cve.mitre.org/cve/identifiers/syntaxchange.html#new) as Jira issues. CVE IDs use the format of CVE-YYYY-NNNNNNN



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

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

#jira_transition_id_regexObject



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

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



108
109
110
# File 'lib/gitlab/regex.rb', line 108

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

#kubernetes_namespace_regexObject



97
98
99
# File 'lib/gitlab/regex.rb', line 97

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

#kubernetes_namespace_regex_messageObject



101
102
103
104
# File 'lib/gitlab/regex.rb', line 101

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]



127
128
129
# File 'lib/gitlab/regex.rb', line 127

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

#logs_section_prefix_regexObject

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



122
123
124
# File 'lib/gitlab/regex.rb', line 122

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

#logs_section_suffix_regexObject

The region end, always: re[0K



132
133
134
# File 'lib/gitlab/regex.rb', line 132

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

#markdown_code_or_html_blocksObject



206
207
208
209
210
211
212
# File 'lib/gitlab/regex.rb', line 206

def markdown_code_or_html_blocks
  @markdown_code_or_html_blocks ||= %r{
      #{MARKDOWN_CODE_BLOCK_REGEX}
    |
      #{MARKDOWN_HTML_BLOCK_REGEX}
  }mx
end

#markdown_code_or_html_blocks_or_html_comments_untrustedObject



230
231
232
233
234
235
236
237
238
239
# File 'lib/gitlab/regex.rb', line 230

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



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

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



221
222
223
224
225
226
227
228
# File 'lib/gitlab/regex.rb', line 221

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



312
313
314
# File 'lib/gitlab/regex.rb', line 312

def ml_model_file_name_regex
  maven_file_name_regex
end

#ml_model_name_regexObject



308
309
310
# File 'lib/gitlab/regex.rb', line 308

def ml_model_name_regex
  package_name_regex
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
  'must not start or end with a special character and must not contain consecutive special characters.'
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



300
301
302
# File 'lib/gitlab/regex.rb', line 300

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

#utc_date_regexObject



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

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

#work_itemObject



287
288
289
# File 'lib/gitlab/regex.rb', line 287

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

#x509_subject_key_identifier_regexObject



304
305
306
# File 'lib/gitlab/regex.rb', line 304

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