Add variables expression pattern validation support
This commit is contained in:
parent
a1f1e08670
commit
8b3e21b66b
|
@ -10,6 +10,10 @@ module Gitlab
|
|||
|
||||
def initialize(regexp)
|
||||
@value = regexp
|
||||
|
||||
unless Gitlab::UntrustedRegexp.valid?(@value)
|
||||
raise Lexer::SyntaxError, 'Invalid regular expression!'
|
||||
end
|
||||
end
|
||||
|
||||
def evaluate(variables = {})
|
||||
|
|
|
@ -111,7 +111,15 @@ describe Gitlab::Ci::Config::Entry::Policy do
|
|||
context 'when specifying invalid variables expressions token' do
|
||||
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/
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,11 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
|
|||
expect(described_class.build('/.*/'))
|
||||
.to be_a(described_class)
|
||||
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
|
||||
|
||||
describe '.type' do
|
||||
|
@ -80,6 +85,8 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
|
|||
end
|
||||
|
||||
it 'raises error if evaluated regexp is not valid' do
|
||||
allow(Gitlab::UntrustedRegexp).to receive(:valid?).and_return(true)
|
||||
|
||||
regexp = described_class.new('invalid ( .*')
|
||||
|
||||
expect { regexp.evaluate }
|
||||
|
|
Loading…
Reference in New Issue