Repository navigation
Conversation
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.
| reduced.pop | ||
| else | ||
| elsif seg != '.' |
There was a problem hiding this comment.
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:
| 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.
URI#merge(and soURI.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: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'surllib.parse.urljoinand Go'surl.ResolveReferenceboth returnhttp://a/b/c/gandhttp://a/c/gfor 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), soURI.join("http://a/b/.", "g")still returnshttp://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_mergethat 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.