Skip to content

Remove "." segments from the base path in URI#merge - #270

Open
0z5a wants to merge 1 commit into
ruby:masterfrom
0z5a:fix/merge-base-dot-segments
Open

0z5a wants to merge 1 commit into
ruby:masterfrom
0z5a:fix/merge-base-dot-segments

Conversation

@0z5a

@0z5a 0z5a commented Nov 15, 2026

Copy link
Copy Markdown

URI#merge (and so URI.join) removes .. segments from the base path, but leaves . segments in place. Besides keeping a /./ in the result, this lets a later .. cancel the . instead of the real directory before it, so the result points at the wrong directory:

URI.join("http://a/b/./c/", "g").to_s
# => "http://a/b/./c/g"   (expected "http://a/b/c/g")

URI.join("http://a/b/./../c/", "g").to_s
# => "http://a/b/c/g"     (expected "http://a/c/g")

RFC 3986 section 5.2.2 merges the base path with the reference path and then applies remove_dot_segments (section 5.2.4) to the whole merged path, which removes . segments from the base part too. Python's urllib.parse.urljoin and Go's url.ResolveReference both return http://a/b/c/g and http://a/c/g for the two examples above.

This skips . segments in the existing single pass over the base path. A trailing . is treated like a trailing .. (a directory), so URI.join("http://a/b/.", "g") still returns http://a/b/g. For a reference that has only a query, the base path was already cleaned of .. segments and is now also cleaned of . segments; RFC 3986 section 5.2.1 allows the base URI to be normalized. Bases without . segments are not affected, including the existing behavior for a trailing ...

I added assertions to test_merge that fail without this change. With the change, the full test suite (93 tests) passes.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

merge_path removed "<segment>/.." 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.
Comment thread lib/uri/generic.rb
Comment on lines 1034 to +1035
reduced.pop
else
elsif seg != '.'

@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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants