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
require_dependency 're2'
class Pattern < Lexeme::Value
PATTERN = %r{/(?<regexp>.+)/}.freeze
PATTERN = %r{^(?<regexp>/.+/[ismU]*)$}.freeze
def initialize(regexp)
@value = regexp
end
def evaluate(variables = {})
Gitlab::UntrustedRegexp.new(@value)
Gitlab::UntrustedRegexp.fabricate(@value)
rescue RegexpError
raise Expression::RuntimeError, 'Invalid regular expression!'
end
......
......@@ -42,6 +42,34 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
expect(token).to be_nil
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
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