Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,9 @@ def merge_path(base, rel)
rel_path = split_path(rel)

# RFC2396, Section 5.2, 6), a)
base_path << '' if base_path.last == '..'
# Remove "<segment>/.." pairs in a single left-to-right pass (O(n)).
base_path << '' if base_path.last == '..' || base_path.last == '.'
# Remove "." segments and "<segment>/.." pairs in a single
# left-to-right pass (O(n)).
# This deliberately differs from the relative-path stack handling
# below: a leading ".." (one with no preceding segment left to
# cancel) discards the whole base path, reproducing the previous
Expand All @@ -1031,7 +1032,7 @@ def merge_path(base, rel)
if seg == '..'
break if reduced.empty?
reduced.pop
else
elsif seg != '.'
Comment on lines 1034 to +1035

@bardrop bardrop Nov 18, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now drop the root /. Before, a . right after the root took the hit from the next ... Now the . is skipped, so that .. pops the root '' segment instead, and the merged path comes out empty or relative:

URI.join("http://a/./../b", "..").to_s     # master: "http://a/"    this PR: "http://a"
URI.join("http://a/./..", "?q").to_s       # master: "http://a/?q"  this PR: "http://a?q"
URI.join("http://a/b/./../../c/", "g").path # master: "/c/g"        this PR: "c/g"

I ran 20k random bases built from ., .., b, c, d and empty segments, joined with g, g/, ./g, ../g, ?q, #f, ., .., g/../h. Results with an empty path: master 504, this PR 648. 93 of them are new (fine on master), all from a . that is now skipped next to a .. that removes the root (/./.., or /../. where the . used to stay as a segment).

One fix that removes all of them (0 in the same run) is to never pop the root:

Suggested change
reduced.pop
else
elsif seg != '.'
reduced.pop unless reduced == ['']
elsif seg != '.'

The suite then has one failure, the pinned http://h/a/../..//y + z case in test_merge_path_dot_dot_removal. It becomes http://h//z, which is what RFC 3986 5.2.4 gives, but that test pins the old output on purpose. So you may want a narrower fix there.

reduced << seg
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ def test_merge
url = URI.parse('http://a/b//c') + 'd//e'
assert_equal('http://a/b//d//e', url.to_s)

# "." segments in the base path are removed as well (RFC 3986, 5.2.4)
url = URI.parse('http://a/b/./c/') + 'g'
assert_equal('http://a/b/c/g', url.to_s)
url = URI.parse('http://a/b/./../c/') + 'g'
assert_equal('http://a/c/g', url.to_s)
url = URI.parse('http://a/b/.') + 'g'
assert_equal('http://a/b/g', url.to_s)

u0 = URI.parse('mailto:foo@example.com')
u1 = URI.parse('mailto:foo@example.com#bar')
assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]")
Expand Down
Loading