diff options
author | Rémy Coutable <remy@rymai.me> | 2018-05-14 15:23:33 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2018-05-14 15:23:33 +0000 |
commit | 59ef79689e0ffe7e693c1eb3e2db1ed417e63a9c (patch) | |
tree | c8256032e98e69744c69981292f33f5a69d5af92 /lib | |
parent | 801c377c7320707287072282c13b8182bbfc91ff (diff) | |
parent | da5c28a422e3966f6f6780d5e23f13eda4881108 (diff) | |
download | gitlab-ce-59ef79689e0ffe7e693c1eb3e2db1ed417e63a9c.tar.gz |
Merge branch 'backstage/gb/backport-untrusted-regexp-ee-code' into 'master'
Backport EE-specific untrusted regexp implementation
See merge request gitlab-org/gitlab-ce!18938
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/untrusted_regexp.rb | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/gitlab/untrusted_regexp.rb b/lib/gitlab/untrusted_regexp.rb index 7ce2e9d636e..75ba0799058 100644 --- a/lib/gitlab/untrusted_regexp.rb +++ b/lib/gitlab/untrusted_regexp.rb @@ -11,7 +11,11 @@ module Gitlab class UntrustedRegexp delegate :===, to: :regexp - def initialize(pattern) + def initialize(pattern, multiline: false) + if multiline + pattern = "(?m)#{pattern}" + end + @regexp = RE2::Regexp.new(pattern, log_errors: false) raise RegexpError.new(regexp.error) unless regexp.ok? @@ -31,6 +35,19 @@ module Gitlab RE2.Replace(text, regexp, rewrite) end + # 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 + private attr_reader :regexp |