From d48098547c5b64ef2c025f94ee50bdd3ab7ab8bc Mon Sep 17 00:00:00 2001 From: 0z5a Date: Sun, 15 Nov 2026 15:15:27 +0100 Subject: [PATCH] Remove "." segments from the base path in URI#merge merge_path removed "/.." pairs from the base path but kept "." segments, so URI.join("http://a/b/./c/", "g") gave "http://a/b/./c/g" and URI.join("http://a/b/./../c/", "g") gave "http://a/b/c/g", where a ".." cancelled the "." instead of the directory before it. RFC 3986 5.2.4 removes dot segments from the whole merged path. Skip "." segments in the same pass and treat a trailing "." as a directory. --- lib/uri/generic.rb | 7 ++++--- test/uri/test_generic.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index fce8a8a..112d7bf 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -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 "/.." pairs in a single left-to-right pass (O(n)). + base_path << '' if base_path.last == '..' || base_path.last == '.' + # Remove "." segments and "/.." 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 @@ -1031,7 +1032,7 @@ def merge_path(base, rel) if seg == '..' break if reduced.empty? reduced.pop - else + elsif seg != '.' reduced << seg end end diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index 9006125..fe0bd5f 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -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]")