Skip to content

Commit

Permalink
Fix quadratic backtracking on invalid port number
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and hsbt committed Jun 20, 2023
1 parent ba36c8a commit 7e33934
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/uri/rfc3986_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def default_regexp # :nodoc:
QUERY: /\A(?:%\h\h|[!$&-.0-;=@-Z_a-z~\/?])*\z/,
FRAGMENT: /\A(?:%\h\h|[!$&-.0-;=@-Z_a-z~\/?])*\z/,
OPAQUE: /\A(?:[^\/].*)?\z/,
PORT: /\A[\x09\x0a\x0c\x0d ]*\d*[\x09\x0a\x0c\x0d ]*\z/,
PORT: /\A[\x09\x0a\x0c\x0d ]*+\d*[\x09\x0a\x0c\x0d ]*\z/,
}
end

Expand Down
10 changes: 10 additions & 0 deletions test/uri/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ def test_rfc2822_parse_relative_uri
end
end
end

def test_rfc3986_port_check
pre = ->(length) {"\t" * length + "a"}
uri = URI.parse("http://my.example.com")
assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |port|
assert_raise(URI::InvalidComponentError) do
uri.port = port
end
end
end
end

0 comments on commit 7e33934

Please sign in to comment.