Commit a1f1e086 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add anti-corruption layer above expressions pattern matching

parent 0ce63efe
...@@ -6,14 +6,14 @@ module Gitlab ...@@ -6,14 +6,14 @@ module Gitlab
require_dependency 're2' require_dependency 're2'
class Pattern < Lexeme::Value class Pattern < Lexeme::Value
PATTERN = %r{/(?<regexp>.+)/}.freeze PATTERN = %r{^(?<regexp>/.+/[ismU]*)$}.freeze
def initialize(regexp) def initialize(regexp)
@value = regexp @value = regexp
end end
def evaluate(variables = {}) def evaluate(variables = {})
Gitlab::UntrustedRegexp.new(@value) Gitlab::UntrustedRegexp.fabricate(@value)
rescue RegexpError rescue RegexpError
raise Expression::RuntimeError, 'Invalid regular expression!' raise Expression::RuntimeError, 'Invalid regular expression!'
end end
......
...@@ -42,6 +42,34 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do ...@@ -42,6 +42,34 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
expect(token).to be_nil expect(token).to be_nil
end end
it 'support single flag' do
scanner = StringScanner.new('/pattern/i')
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate)
.to eq Gitlab::UntrustedRegexp.new('(?i)pattern')
end
it 'support multiple flags' do
scanner = StringScanner.new('/pattern/im')
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate)
.to eq Gitlab::UntrustedRegexp.new('(?im)pattern')
end
it 'does not support arbitrary flags' do
scanner = StringScanner.new('/pattern/x')
token = described_class.scan(scanner)
expect(token).to be_nil
end
end end
describe '#evaluate' do describe '#evaluate' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment