Commit 8b3e21b6 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add variables expression pattern validation support

parent a1f1e086
...@@ -10,6 +10,10 @@ module Gitlab ...@@ -10,6 +10,10 @@ module Gitlab
def initialize(regexp) def initialize(regexp)
@value = regexp @value = regexp
unless Gitlab::UntrustedRegexp.valid?(@value)
raise Lexer::SyntaxError, 'Invalid regular expression!'
end
end end
def evaluate(variables = {}) def evaluate(variables = {})
......
...@@ -111,7 +111,15 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -111,7 +111,15 @@ describe Gitlab::Ci::Config::Entry::Policy do
context 'when specifying invalid variables expressions token' do context 'when specifying invalid variables expressions token' do
let(:config) { { variables: ['$MY_VAR == 123'] } } let(:config) { { variables: ['$MY_VAR == 123'] } }
it 'reports an error about invalid statement' do it 'reports an error about invalid expression' do
expect(entry.errors).to include /invalid expression syntax/
end
end
context 'when using invalid variables expressions regexp' do
let(:config) { { variables: ['$MY_VAR =~ /some ( thing/'] } }
it 'reports an error about invalid expression' do
expect(entry.errors).to include /invalid expression syntax/ expect(entry.errors).to include /invalid expression syntax/
end end
end end
......
...@@ -6,6 +6,11 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do ...@@ -6,6 +6,11 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
expect(described_class.build('/.*/')) expect(described_class.build('/.*/'))
.to be_a(described_class) .to be_a(described_class)
end end
it 'raises an error if pattern is invalid' do
expect { described_class.build('/ some ( thin/i') }
.to raise_error(Gitlab::Ci::Pipeline::Expression::Lexer::SyntaxError)
end
end end
describe '.type' do describe '.type' do
...@@ -80,6 +85,8 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do ...@@ -80,6 +85,8 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
end end
it 'raises error if evaluated regexp is not valid' do it 'raises error if evaluated regexp is not valid' do
allow(Gitlab::UntrustedRegexp).to receive(:valid?).and_return(true)
regexp = described_class.new('invalid ( .*') regexp = described_class.new('invalid ( .*')
expect { regexp.evaluate } expect { regexp.evaluate }
......
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