Commit 65b20f35 authored by Marius Bobin's avatar Marius Bobin Committed by Stan Hu

Ensure CI matching operator receives an object

Ensure the evaluation of right-hand side expression always
results in the returning of an object or an empty String
parent 9a0c1f64
---
title: Fix 500 errors caused by pattern matching with variables in CI Lint
merge_request: 31719
author:
type: fixed
......@@ -11,8 +11,9 @@ module Gitlab
def evaluate(variables = {})
text = @left.evaluate(variables)
regexp = @right.evaluate(variables)
return false unless regexp
regexp.scan(text.to_s).any?
regexp.scan(text.to_s).present?
end
def self.build(_value, behind, ahead)
......
......@@ -11,8 +11,9 @@ module Gitlab
def evaluate(variables = {})
text = @left.evaluate(variables)
regexp = @right.evaluate(variables)
return true unless regexp
regexp.scan(text.to_s).none?
regexp.scan(text.to_s).empty?
end
def self.build(_value, behind, ahead)
......
......@@ -69,6 +69,34 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Matches do
it { is_expected.to eq(false) }
end
context 'when right is nil' do
let(:left_value) { 'my-awesome-string' }
let(:right_value) { nil }
it { is_expected.to eq(false) }
end
context 'when left and right are nil' do
let(:left_value) { nil }
let(:right_value) { nil }
it { is_expected.to eq(false) }
end
context 'when left is an empty string' do
let(:left_value) { '' }
let(:right_value) { Gitlab::UntrustedRegexp.new('pattern') }
it { is_expected.to eq(false) }
end
context 'when left and right are empty strings' do
let(:left_value) { '' }
let(:right_value) { Gitlab::UntrustedRegexp.new('') }
it { is_expected.to eq(true) }
end
context 'when left is a multiline string and matches right' do
let(:left_value) do
<<~TEXT
......
......@@ -69,6 +69,34 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::NotMatches do
it { is_expected.to eq(true) }
end
context 'when right is nil' do
let(:left_value) { 'my-awesome-string' }
let(:right_value) { nil }
it { is_expected.to eq(true) }
end
context 'when left and right are nil' do
let(:left_value) { nil }
let(:right_value) { nil }
it { is_expected.to eq(true) }
end
context 'when left is an empty string' do
let(:left_value) { '' }
let(:right_value) { Gitlab::UntrustedRegexp.new('pattern') }
it { is_expected.to eq(true) }
end
context 'when left and right are empty strings' do
let(:left_value) { '' }
let(:right_value) { Gitlab::UntrustedRegexp.new('') }
it { is_expected.to eq(false) }
end
context 'when left is a multiline string and matches right' do
let(:left_value) do
<<~TEXT
......
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