Skip to content

[SR-15806] [URL] Appending path components can change the host #3179

Open
@karwa

Description

@karwa
Previous ID SR-15806
Radar None
Original Reporter @karwa
Type Bug
Environment

macOS 11.6, Xcode 13.2

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 0e0da9836149665649465557eb3530cf

Issue Description:

import Foundation

var url = URL(string: "foo:/")!

print(url.absoluteString) // "foo:/"
print(url.host)  // nil
print(url.path)  // "/"

url.appendPathComponent("/bar")

print(url.absoluteString) // "foo://bar"
print(url.host)  // "bar"
print(url.path)  // ""

RFC-2396 does say, with regards to paring a URL:

   Although the BNF defines what is allowed in each component, it is
   ambiguous in terms of differentiating between an authority component
   and a path component that begins with two slash characters.  The
   greedy algorithm is used for disambiguation: the left-most matching
   rule soaks up as much of the URI reference string as it is capable of
   matching.  In other words, the authority component wins.

https://datatracker.ietf.org/doc/html/rfc2396#section-4.3

However, that only applies to parsing strings which may be ambiguous. When manipulated via the API, we know the user's precise intent - there is no ambiguity, and by causing the "appendPathComponent" method to instead erase the path and set the host, the API has not honoured the user's intent. Basically, the URL "foo://bar" should not be the serialised URL constructed by Foundation for this sequence of operations.

FWIW, the WHATWG URL Standard disambiguates these by adding a leading slash-dot to the path. So the URL in this case would be 'foo:/.//bar'.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions