Skip to content

Revert strict IPv6 validation in URL #1258

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
Apr 23, 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
47 changes: 2 additions & 45 deletions Sources/FoundationEssentials/URL/URLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,41 +435,6 @@ internal struct RFC3986Parser {
return validate(string: password, component: .password, percentEncodingAllowed: percentEncodingAllowed)
}

private static func isIPvFuture(_ innerHost: some StringProtocol) -> Bool {
// precondition: IP-literal == "[" innerHost [ "%25" zoneID ] "]"
var iter = innerHost.utf8.makeIterator()
guard iter.next() == UInt8(ascii: "v") else { return false }
guard let second = iter.next(), second.isValidHexDigit else { return false }
while let next = iter.next() {
if next.isValidHexDigit { continue }
if next == ._dot { return true }
return false
}
return false
}

/// Only checks that the characters are allowed in an IPv6 address.
/// Does not validate the format of the IPv6 address.
private static func validateIPv6Address(_ address: some StringProtocol) -> Bool {
let isValid = address.utf8.withContiguousStorageIfAvailable {
$0.allSatisfy { $0.isValidHexDigit || $0 == UInt8(ascii: ":") || $0 == UInt8(ascii: ".") }
}
if let isValid {
return isValid
}
#if FOUNDATION_FRAMEWORK
if let fastCharacters = address._ns._fastCharacterContents() {
let charsBuffer = UnsafeBufferPointer(start: fastCharacters, count: address._ns.length)
return charsBuffer.allSatisfy {
guard $0 < 128 else { return false }
let v = UInt8($0)
return v.isValidHexDigit || v == UInt8(ascii: ":") || v == UInt8(ascii: ".")
}
}
#endif
return address.utf8.allSatisfy { $0.isValidHexDigit || $0 == UInt8(ascii: ":") || $0 == UInt8(ascii: ".") }
}

/// Validates an IP-literal host string that has leading and trailing brackets.
/// If the host string contains a zone ID delimiter "%", this must be percent encoded to "%25" to be valid.
/// The zone ID may contain any `reg_name` characters, including percent-encoding.
Expand All @@ -483,11 +448,7 @@ internal struct RFC3986Parser {

guard let percentIndex = utf8.firstIndex(of: UInt8(ascii: "%")) else {
// There is no zoneID, so the whole innerHost must be the IP-literal address.
if isIPvFuture(innerHost) {
return validate(string: innerHost, component: .hostIPvFuture, percentEncodingAllowed: false)
} else {
return validateIPv6Address(innerHost)
}
return validate(string: innerHost, component: .hostIPvFuture, percentEncodingAllowed: false)
}

// The first "%" in an IP-literal must be the zone ID delimiter.
Expand All @@ -503,11 +464,7 @@ internal struct RFC3986Parser {
return false
}

if isIPvFuture(innerHost) {
return validate(string: innerHost[..<percentIndex], component: .hostIPvFuture, percentEncodingAllowed: false) && validate(string: innerHost[innerHost.index(after: twoAfterIndex)...], component: .hostZoneID)
} else {
return validateIPv6Address(innerHost[..<percentIndex]) && validate(string: innerHost[innerHost.index(after: twoAfterIndex)...], component: .hostZoneID)
}
return validate(string: innerHost[..<percentIndex], component: .hostIPvFuture, percentEncodingAllowed: false) && validate(string: innerHost[innerHost.index(after: twoAfterIndex)...], component: .hostZoneID)
}

private static func validate(host: some StringProtocol, knownIPLiteral: Bool = false) -> Bool {
Expand Down
5 changes: 5 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ final class URLTests : XCTestCase {
XCTAssertEqual(url.host, "fe80::a%100%CustomZone")
XCTAssertEqual(url.host(percentEncoded: true), "fe80::a%25100%25CustomZone")
XCTAssertEqual(url.host(percentEncoded: false), "fe80::a%100%CustomZone")

// Make sure an IP-literal with invalid characters `{` and `}`
// returns `nil` even if we can percent-encode the zone-ID.
let invalid = URL(string: "http://[{Invalid}%100%EncodableZone]")
XCTAssertNil(invalid)
}

#if !os(Windows)
Expand Down