From 6d734a89751d5553f22dbb674247f25c825e9274 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 13 Jun 2023 13:54:44 +0900 Subject: [PATCH] [ruby/uri] Fix RFC3986 regexps https://github.com/ruby/uri/commit/8e38592241 --- lib/uri/rfc3986_parser.rb | 9 +++++---- test/uri/test_parser.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/uri/rfc3986_parser.rb b/lib/uri/rfc3986_parser.rb index 8cc51f1286..59e2be2805 100644 --- a/lib/uri/rfc3986_parser.rb +++ b/lib/uri/rfc3986_parser.rb @@ -34,6 +34,7 @@ module URI SCHEME = %r[[A-Za-z][+\-.0-9A-Za-z]*+].source SEG = %r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/])].source + SEG_NC = %r[(?:%\h\h|[!$&-.0-9;=@A-Z_a-z~])].source FRAGMENT = %r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+].source RFC3986_URI = %r[\A @@ -43,8 +44,8 @@ module URI (?// (?#{AUTHORITY}) (?(?:/\g*+)?) - | (?/\g*+) - | (?(?!=/)\g++) + | (?/((?!/)\g++)?) + | (?(?!/)\g++) | (?) ) (?:\?(?[^\#]*+))? @@ -58,7 +59,7 @@ module URI (?#{AUTHORITY}) (?(?:/\g*+)?) | (?/\g*+) - | (?(?!=[:/])\g++) + | (?#{SEG_NC}++(?:/\g*+)?) | (?) ) (?:\?(?[^#]*+))? @@ -156,7 +157,7 @@ module URI USERINFO: %r[\A#{USERINFO}\z]o, HOST: %r[\A#{HOST}\z]o, ABS_PATH: %r[\A/#{SEG}*+\z]o, - REL_PATH: %r[\A(?!=/)#{SEG}++\z]o, + REL_PATH: %r[\A(?!/)#{SEG}++\z]o, QUERY: %r[\A(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+\z], FRAGMENT: %r[\A#{FRAGMENT}\z]o, OPAQUE: %r[\A(?:[^/].*)?\z], diff --git a/test/uri/test_parser.rb b/test/uri/test_parser.rb index 72fb5901d9..70d476b0c4 100644 --- a/test/uri/test_parser.rb +++ b/test/uri/test_parser.rb @@ -78,5 +78,13 @@ class URI::TestParser < Test::Unit::TestCase assert_equal(["http", nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("http://[0::0]")) assert_equal([nil, nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("//example.com")) assert_equal([nil, nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("//[0::0]")) + + assert_equal(["a", nil, nil, nil, nil, "", nil, nil, nil], URI.split("a:")) + assert_raise(URI::InvalidURIError) do + URI.parse("::") + end + assert_raise(URI::InvalidURIError) do + URI.parse("foo@example:foo") + end end end