Skip to content

Commit 2789182

Browse files
hsbtnobu
andcommitted
Fix merger of URI with authority component
https://hackerone.com/reports/2957667 Co-authored-by: Nobuyoshi Nakada <[email protected]>
1 parent 3675494 commit 2789182

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

lib/uri/generic.rb

+7-12
Original file line numberDiff line numberDiff line change
@@ -1133,21 +1133,16 @@ def merge(oth)
11331133
base.fragment=(nil)
11341134

11351135
# RFC2396, Section 5.2, 4)
1136-
if !authority
1137-
base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path
1138-
else
1139-
# RFC2396, Section 5.2, 4)
1140-
base.set_path(rel.path) if rel.path
1136+
if authority
1137+
base.set_userinfo(rel.userinfo)
1138+
base.set_host(rel.host)
1139+
base.set_port(rel.port || base.default_port)
1140+
base.set_path(rel.path)
1141+
elsif base.path && rel.path
1142+
base.set_path(merge_path(base.path, rel.path))
11411143
end
11421144

11431145
# RFC2396, Section 5.2, 7)
1144-
if rel.userinfo
1145-
base.set_userinfo(rel.userinfo)
1146-
else
1147-
base.set_userinfo(nil)
1148-
end
1149-
base.set_host(rel.host) if rel.host
1150-
base.set_port(rel.port) if rel.port
11511146
base.query = rel.query if rel.query
11521147
base.fragment=(rel.fragment) if rel.fragment
11531148

test/uri/test_generic.rb

+7
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ def test_merge
278278
assert_equal(u0, u1)
279279
end
280280

281+
def test_merge_authority
282+
u = URI.parse('http://user:[email protected]:8080')
283+
u0 = URI.parse('http://new.example.org/path')
284+
u1 = u.merge('//new.example.org/path')
285+
assert_equal(u0, u1)
286+
end
287+
281288
def test_route
282289
url = URI.parse('http://hoge/a.html').route_to('http://hoge/b.html')
283290
assert_equal('b.html', url.to_s)

0 commit comments

Comments
 (0)