Skip to content

Commit 9010ee2

Browse files
nobuhsbt
authored andcommitted
Fix quadratic backtracking on invalid relative URI
https://hackerone.com/reports/1958260
1 parent fd21465 commit 9010ee2

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/uri/rfc2396_parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ def initialize_regexp(pattern)
497497
ret = {}
498498

499499
# for URI::split
500-
ret[:ABS_URI] = Regexp.new('\A\s*' + pattern[:X_ABS_URI] + '\s*\z', Regexp::EXTENDED)
501-
ret[:REL_URI] = Regexp.new('\A\s*' + pattern[:X_REL_URI] + '\s*\z', Regexp::EXTENDED)
500+
ret[:ABS_URI] = Regexp.new('\A\s*+' + pattern[:X_ABS_URI] + '\s*\z', Regexp::EXTENDED)
501+
ret[:REL_URI] = Regexp.new('\A\s*+' + pattern[:X_REL_URI] + '\s*\z', Regexp::EXTENDED)
502502

503503
# for URI::extract
504504
ret[:URI_REF] = Regexp.new(pattern[:URI_REF])

test/uri/test_parser.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,16 @@ def test_split
8787
URI.parse("foo@example:foo")
8888
end
8989
end
90+
91+
def test_rfc2822_parse_relative_uri
92+
pre = ->(length) {
93+
" " * length + "\0"
94+
}
95+
parser = URI::RFC2396_Parser.new
96+
assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |uri|
97+
assert_raise(URI::InvalidURIError) do
98+
parser.split(uri)
99+
end
100+
end
101+
end
90102
end

0 commit comments

Comments
 (0)