2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
module Gitlab
|
|
|
|
# An untrusted regular expression is any regexp containing patterns sourced
|
|
|
|
# from user input.
|
|
|
|
#
|
|
|
|
# Ruby's built-in regular expression library allows patterns which complete in
|
|
|
|
# exponential time, permitting denial-of-service attacks.
|
|
|
|
#
|
|
|
|
# Not all regular expression features are available in untrusted regexes, and
|
|
|
|
# there is a strict limit on total execution time. See the RE2 documentation
|
|
|
|
# at https://github.com/google/re2/wiki/Syntax for more details.
|
|
|
|
class UntrustedRegexp
|
2018-05-17 06:29:47 -04:00
|
|
|
require_dependency 're2'
|
|
|
|
|
2018-05-11 07:28:51 -04:00
|
|
|
delegate :===, :source, to: :regexp
|
2017-06-22 11:33:17 -04:00
|
|
|
|
2018-05-14 04:43:45 -04:00
|
|
|
def initialize(pattern, multiline: false)
|
|
|
|
if multiline
|
|
|
|
pattern = "(?m)#{pattern}"
|
|
|
|
end
|
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
@regexp = RE2::Regexp.new(pattern, log_errors: false)
|
|
|
|
|
|
|
|
raise RegexpError.new(regexp.error) unless regexp.ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_all(text, rewrite)
|
|
|
|
RE2.GlobalReplace(text, regexp, rewrite)
|
|
|
|
end
|
|
|
|
|
|
|
|
def scan(text)
|
2017-07-24 05:53:57 -04:00
|
|
|
matches = scan_regexp.scan(text).to_a
|
2020-08-05 11:09:59 -04:00
|
|
|
matches.map!(&:first) if regexp.number_of_capturing_groups == 0
|
2017-07-24 05:53:57 -04:00
|
|
|
matches
|
2017-06-22 11:33:17 -04:00
|
|
|
end
|
|
|
|
|
2021-03-23 11:09:28 -04:00
|
|
|
def match(text)
|
|
|
|
scan_regexp.match(text)
|
|
|
|
end
|
|
|
|
|
2019-02-25 09:41:52 -05:00
|
|
|
def match?(text)
|
|
|
|
text.present? && scan(text).present?
|
|
|
|
end
|
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
def replace(text, rewrite)
|
|
|
|
RE2.Replace(text, regexp, rewrite)
|
|
|
|
end
|
|
|
|
|
2018-05-11 07:28:51 -04:00
|
|
|
def ==(other)
|
|
|
|
self.source == other.source
|
|
|
|
end
|
|
|
|
|
2019-04-03 03:06:50 -04:00
|
|
|
# Handles regular expressions with the preferred RE2 library where possible
|
|
|
|
# via UntustedRegex. Falls back to Ruby's built-in regular expression library
|
|
|
|
# when the syntax would be invalid in RE2.
|
|
|
|
#
|
|
|
|
# One difference between these is `(?m)` multi-line mode. Ruby regex enables
|
|
|
|
# this by default, but also handles `^` and `$` differently.
|
|
|
|
# See: https://www.regular-expressions.info/modifiers.html
|
|
|
|
def self.with_fallback(pattern, multiline: false)
|
|
|
|
UntrustedRegexp.new(pattern, multiline: multiline)
|
|
|
|
rescue RegexpError
|
|
|
|
Regexp.new(pattern)
|
|
|
|
end
|
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :regexp
|
|
|
|
|
|
|
|
# RE2 scan operates differently to Ruby scan when there are no capture
|
|
|
|
# groups, so work around it
|
|
|
|
def scan_regexp
|
|
|
|
@scan_regexp ||=
|
2020-08-05 11:09:59 -04:00
|
|
|
if regexp.number_of_capturing_groups == 0
|
2017-06-22 11:33:17 -04:00
|
|
|
RE2::Regexp.new('(' + regexp.source + ')')
|
|
|
|
else
|
|
|
|
regexp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|