Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
da5c28a4
Commit
da5c28a4
authored
May 14, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backport EE-specific untrusted regexp implementation
parent
f1a2ada1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletion
+26
-1
lib/gitlab/untrusted_regexp.rb
lib/gitlab/untrusted_regexp.rb
+18
-1
spec/lib/gitlab/untrusted_regexp_spec.rb
spec/lib/gitlab/untrusted_regexp_spec.rb
+8
-0
No files found.
lib/gitlab/untrusted_regexp.rb
View file @
da5c28a4
...
...
@@ -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
...
...
spec/lib/gitlab/untrusted_regexp_spec.rb
View file @
da5c28a4
...
...
@@ -39,6 +39,14 @@ describe Gitlab::UntrustedRegexp do
expect
(
result
).
to
be_falsy
end
it
'can handle regular expressions in multiline mode'
do
regexp
=
described_class
.
new
(
'^\d'
,
multiline:
true
)
result
=
regexp
===
"Header
\n\n
1. Content"
expect
(
result
).
to
be_truthy
end
end
describe
'#scan'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment