debian-mirror-gitlab/lib/gitlab/untrusted_regexp.rb

66 lines
1.6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
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-11-08 19:23:39 +05:30
require_dependency 're2'
delegate :===, :source, to: :regexp
def initialize(pattern, multiline: false)
if multiline
pattern = "(?m)#{pattern}"
end
2017-08-17 22:00:37 +05:30
@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-09-10 17:25:29 +05:30
matches = scan_regexp.scan(text).to_a
matches.map!(&:first) if regexp.number_of_capturing_groups.zero?
matches
2017-08-17 22:00:37 +05:30
end
2019-04-03 18:18:56 +05:30
def match?(text)
text.present? && scan(text).present?
end
2017-08-17 22:00:37 +05:30
def replace(text, rewrite)
RE2.Replace(text, regexp, rewrite)
end
2018-11-08 19:23:39 +05:30
def ==(other)
self.source == other.source
end
2017-08-17 22:00:37 +05:30
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 ||=
if regexp.number_of_capturing_groups.zero?
RE2::Regexp.new('(' + regexp.source + ')')
else
regexp
end
end
end
end