Skip to content

Commit

Permalink
Fix quadratic backtracking on invalid relative URI
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and hsbt committed Jun 29, 2023
1 parent fd21465 commit 9010ee2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/uri/rfc2396_parser.rb
Expand Up @@ -497,8 +497,8 @@ def initialize_regexp(pattern)
ret = {}

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

# for URI::extract
ret[:URI_REF] = Regexp.new(pattern[:URI_REF])
Expand Down
12 changes: 12 additions & 0 deletions test/uri/test_parser.rb
Expand Up @@ -87,4 +87,16 @@ def test_split
URI.parse("foo@example:foo")
end
end

def test_rfc2822_parse_relative_uri
pre = ->(length) {
" " * length + "\0"
}
parser = URI::RFC2396_Parser.new
assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |uri|
assert_raise(URI::InvalidURIError) do
parser.split(uri)
end
end
end
end

0 comments on commit 9010ee2

Please sign in to comment.