Skip to content

URL.absoluteString crashes if baseURL starts with colon #1119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
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
6 changes: 4 additions & 2 deletions Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,9 @@ public struct URL: Equatable, Sendable, Hashable {
}

if let baseScheme = _baseParseInfo.scheme {
result.scheme = String(baseScheme)
// Scheme might be empty, which URL allows for compatibility,
// but URLComponents does not, so we force it internally.
result.forceScheme(String(baseScheme))
}

if hasAuthority {
Expand Down Expand Up @@ -1498,7 +1500,7 @@ public struct URL: Equatable, Sendable, Hashable {
}
#endif
if _baseParseInfo != nil {
return absoluteURL.path(percentEncoded: percentEncoded)
return absoluteURL.relativePath(percentEncoded: percentEncoded)
}
if percentEncoded {
return String(_parseInfo.path)
Expand Down
13 changes: 10 additions & 3 deletions Sources/FoundationEssentials/URL/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ public struct URLComponents: Hashable, Equatable, Sendable {
return nil
}

mutating func setScheme(_ newValue: String?) throws {
mutating func setScheme(_ newValue: String?, force: Bool = false) throws {
reset(.scheme)
guard Parser.validate(newValue, component: .scheme) else {
throw InvalidComponentError.scheme
if !force {
guard Parser.validate(newValue, component: .scheme) else {
throw InvalidComponentError.scheme
}
}
_scheme = newValue
if encodedHost != nil {
Expand Down Expand Up @@ -716,6 +718,11 @@ public struct URLComponents: Hashable, Equatable, Sendable {
}
}

/// Used by `URL` to allow empty scheme for compatibility.
internal mutating func forceScheme(_ scheme: String) {
try? components.setScheme(scheme, force: true)
}

#if FOUNDATION_FRAMEWORK
/// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
internal mutating func setScheme(_ newValue: String?) throws {
Expand Down
15 changes: 15 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,21 @@ final class URLTests : XCTestCase {
XCTAssertEqual(schemeOnly.absoluteString, "scheme:foo")
}

func testURLEmptySchemeCompatibility() throws {
var url = try XCTUnwrap(URL(string: ":memory:"))
XCTAssertEqual(url.scheme, "")

let base = try XCTUnwrap(URL(string: "://home"))
XCTAssertEqual(base.host(), "home")

url = try XCTUnwrap(URL(string: "/path", relativeTo: base))
XCTAssertEqual(url.scheme, "")
XCTAssertEqual(url.host(), "home")
XCTAssertEqual(url.path, "/path")
XCTAssertEqual(url.absoluteString, "://home/path")
XCTAssertEqual(url.absoluteURL.scheme, "")
}

func testURLComponentsPercentEncodedUnencodedProperties() throws {
var comp = URLComponents()

Expand Down