Skip to content

Commit fa20301

Browse files
committed
avoid infinite loop by removing comments when receiving extended regexp
Fixes #60
1 parent 6f2a904 commit fa20301

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mustermann/lib/mustermann/regular.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Regular < RegexpBased
2121
# @see (see Mustermann::Pattern#initialize)
2222
def initialize(string, check_anchors: true, **options)
2323
string = $1 if string.to_s =~ /\A\(\?\-mix\:(.*)\)\Z/ && string.inspect == "/#$1/"
24+
string = string.inspect.gsub!(/(?<!\\)(?:\s|#.*(?:$|\/[mix]))/, '') if extended_regexp?(string)
2425
@check_anchors = check_anchors
2526
super(string, **options)
2627
end
@@ -40,6 +41,10 @@ def check_anchors(scanner)
4041
raise CompileError, "regular expression should not contain %s: %p" % [illegal.to_s, @string]
4142
end
4243

43-
private :compile, :check_anchors
44+
def extended_regexp?(string)
45+
not (Regexp.new(string).options & Regexp::EXTENDED).zero?
46+
end
47+
48+
private :compile, :check_anchors, :extended_regexp?
4449
end
4550
end

mustermann/spec/regular_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22
require 'support'
3+
require 'timeout'
34
require 'mustermann/regular'
45

56
describe Mustermann::Regular do
@@ -38,6 +39,21 @@
3839
it { should match('/foo%2Fbar') .capturing foo: 'foo%2Fbar' }
3940
end
4041

42+
43+
context 'with Regexp::EXTENDED' do
44+
let(:pattern) {
45+
%r{
46+
\/compare\/ # match any URL beginning with \/compare\/
47+
(.+) # extract the full path (including any directories)
48+
\/ # match the final slash
49+
([^.]+) # match the first SHA1
50+
\.{2,3} # match .. or ...
51+
(.+) # match the second SHA1
52+
}x
53+
}
54+
example { expect { Timeout.timeout(1){ Mustermann::Regular.new(pattern) }}.not_to raise_error(Timeout::Error) }
55+
end
56+
4157
describe :check_achnors do
4258
context 'raises on anchors' do
4359
example { expect { Mustermann::Regular.new('^foo') }.to raise_error(Mustermann::CompileError) }

0 commit comments

Comments
 (0)