Skip to content

URLComponents.string should percent-encode colons in first path segment if needed #1117

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 13, 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
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,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
19 changes: 18 additions & 1 deletion Sources/FoundationEssentials/URL/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,17 @@ public struct URLComponents: Hashable, Equatable, Sendable {
return ""
}

private var percentEncodedPathNoColon: String {
guard percentEncodedPath.utf8.first(where: { $0 == ._colon || $0 == ._slash }) == ._colon else {
return percentEncodedPath
}
let colonEncodedPath = Array(percentEncodedPath.utf8).replacing(
[._colon],
with: [UInt8(ascii: "%"), UInt8(ascii: "3"), UInt8(ascii: "A")]
)
return String(decoding: colonEncodedPath, as: UTF8.self)
}

mutating func setPercentEncodedPath(_ newValue: String) throws {
reset(.path)
guard Parser.validate(newValue, component: .path) else {
Expand Down Expand Up @@ -451,7 +462,13 @@ public struct URLComponents: Hashable, Equatable, Sendable {
// The parser already validated a special-case (e.g. addressbook:).
result += ":\(portString)"
}
result += percentEncodedPath
if result.isEmpty {
// We must percent-encode colons in the first path segment
// as they could be misinterpreted as a scheme separator.
result += percentEncodedPathNoColon
} else {
result += percentEncodedPath
}
if let percentEncodedQuery {
result += "?\(percentEncodedQuery)"
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,19 @@ final class URLTests : XCTestCase {
comp = try XCTUnwrap(URLComponents(string: legalURLString))
XCTAssertEqual(comp.string, legalURLString)
XCTAssertEqual(comp.percentEncodedPath, colonFirstPath)

// Colons should be percent-encoded by URLComponents.string if
// they could be misinterpreted as a scheme separator.

comp = URLComponents()
comp.percentEncodedPath = "not%20a%20scheme:"
XCTAssertEqual(comp.string, "not%20a%20scheme%3A")

// These would fail if we did not percent-encode the colon.
// .string should always produce a valid URL string, or nil.

XCTAssertNotNil(URL(string: comp.string!))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference: we prefer using XCTUnwrap to unwrap the string (like you did above on line 1327). I'd also appreciate if we can use XCTAssertEqual to assert the exact content instead of less-informational XCTAssertNotNil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that makes sense, I'll do a cleanup of all the URL tests in a follow-up PR to prefer that pattern

XCTAssertNotNil(URLComponents(string: comp.string!))
}

func testURLComponentsInvalidPaths() {
Expand Down