2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-17 06:29:47 -04:00
|
|
|
require 'fast_spec_helper'
|
2020-01-20 22:08:37 -05:00
|
|
|
require 'support/shared_examples/lib/gitlab/malicious_regexp_shared_examples'
|
2017-06-22 11:33:17 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::UntrustedRegexp do
|
2017-06-22 11:33:17 -04:00
|
|
|
describe '#initialize' do
|
|
|
|
subject { described_class.new(pattern) }
|
|
|
|
|
|
|
|
context 'invalid regexp' do
|
|
|
|
let(:pattern) { '[' }
|
|
|
|
|
|
|
|
it { expect { subject }.to raise_error(RegexpError) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#replace_all' do
|
|
|
|
it 'replaces all instances of the match in a string' do
|
|
|
|
result = described_class.new('foo').replace_all('foo bar foo', 'oof')
|
|
|
|
|
|
|
|
expect(result).to eq('oof bar oof')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#replace' do
|
|
|
|
it 'replaces the first instance of the match in a string' do
|
|
|
|
result = described_class.new('foo').replace('foo bar foo', 'oof')
|
|
|
|
|
|
|
|
expect(result).to eq('oof bar foo')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#===' do
|
|
|
|
it 'returns true for a match' do
|
|
|
|
result = described_class.new('foo') === 'a foo here'
|
|
|
|
|
|
|
|
expect(result).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for no match' do
|
|
|
|
result = described_class.new('foo') === 'a bar here'
|
|
|
|
|
|
|
|
expect(result).to be_falsy
|
|
|
|
end
|
2018-05-14 04:43:45 -04:00
|
|
|
|
|
|
|
it 'can handle regular expressions in multiline mode' do
|
|
|
|
regexp = described_class.new('^\d', multiline: true)
|
|
|
|
|
|
|
|
result = regexp === "Header\n\n1. Content"
|
|
|
|
|
|
|
|
expect(result).to be_truthy
|
|
|
|
end
|
2017-06-22 11:33:17 -04:00
|
|
|
end
|
|
|
|
|
2019-02-25 09:41:52 -05:00
|
|
|
describe '#match?' do
|
|
|
|
subject { described_class.new(regexp).match?(text) }
|
|
|
|
|
|
|
|
context 'malicious regexp' do
|
|
|
|
let(:text) { malicious_text }
|
|
|
|
let(:regexp) { malicious_regexp_re2 }
|
|
|
|
|
|
|
|
include_examples 'malicious regexp'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'matching regexp' do
|
|
|
|
let(:regexp) { 'foo' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns an array of nil matches' do
|
|
|
|
is_expected.to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-matching regexp' do
|
|
|
|
let(:regexp) { 'boo' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns an array of nil matches' do
|
|
|
|
is_expected.to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
describe '#scan' do
|
|
|
|
subject { described_class.new(regexp).scan(text) }
|
2019-02-25 09:41:52 -05:00
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
context 'malicious regexp' do
|
|
|
|
let(:text) { malicious_text }
|
2019-02-25 09:41:52 -05:00
|
|
|
let(:regexp) { malicious_regexp_re2 }
|
2017-07-20 09:16:00 -04:00
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
include_examples 'malicious regexp'
|
|
|
|
end
|
|
|
|
|
2017-07-20 09:16:00 -04:00
|
|
|
context 'empty regexp' do
|
|
|
|
let(:regexp) { '' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
2017-07-24 05:53:57 -04:00
|
|
|
it 'returns an array of nil matches' do
|
|
|
|
is_expected.to eq([nil, nil, nil, nil])
|
2017-07-20 09:16:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'empty capture group regexp' do
|
|
|
|
let(:regexp) { '()' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
2017-07-24 05:53:57 -04:00
|
|
|
it 'returns an array of nil matches in an array' do
|
|
|
|
is_expected.to eq([[nil], [nil], [nil], [nil]])
|
2017-07-20 09:16:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-22 11:33:17 -04:00
|
|
|
context 'no capture group' do
|
|
|
|
let(:regexp) { '.+' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns the whole match' do
|
|
|
|
is_expected.to eq(['foo'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'one capture group' do
|
|
|
|
let(:regexp) { '(f).+' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns the captured part' do
|
|
|
|
is_expected.to eq([%w[f]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'two capture groups' do
|
|
|
|
let(:regexp) { '(f).(o)' }
|
|
|
|
let(:text) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns the captured parts' do
|
|
|
|
is_expected.to eq([%w[f o]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|