diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-17 12:35:20 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-17 12:35:20 +0200 |
commit | a1f1e08670a7f8bd5499e16c778be16106210a44 (patch) | |
tree | 466ccc5ad50a841347b66b75766e2ced6401cb7b | |
parent | 0ce63efe966840edb6e6184cf1abcef272a24dfc (diff) | |
download | gitlab-ce-a1f1e08670a7f8bd5499e16c778be16106210a44.tar.gz |
Add anti-corruption layer above expressions pattern matching
-rw-r--r-- | lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb | 4 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/pipeline/expression/lexeme/pattern_spec.rb | 28 |
2 files changed, 30 insertions, 2 deletions
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb b/lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb index 62927441035..53fb5f769d8 100644 --- a/lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb +++ b/lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb @@ -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 diff --git a/spec/lib/gitlab/ci/pipeline/expression/lexeme/pattern_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/lexeme/pattern_spec.rb index a14a28056d8..6435ee5c915 100644 --- a/spec/lib/gitlab/ci/pipeline/expression/lexeme/pattern_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/expression/lexeme/pattern_spec.rb @@ -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 |