Skip to content

Commit a637cd9

Browse files
Improve domain matching routine per the recommendation on PR.
- Also update unit tests to be more accurate in testing domain matching
1 parent 9870d94 commit a637cd9

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Foundation/HTTPCookieStorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ extension HTTPCookie {
352352
// domain string is a %x2E (".") character.
353353
// * The string is a host name (i.e., not an IP address).
354354

355-
let dotlessDomain = domain.first == "." ? String(domain.suffix(domain.count - 1)) : domain
356-
return dotlessDomain == host || (domain.first == "." && host.hasSuffix(domain))
355+
guard domain.hasPrefix(".") else { return host == domain }
356+
return host == domain.dropFirst() || host.hasSuffix(domain)
357357
}
358358

359359
internal func persistableDictionary() -> [String: Any] {

TestFoundation/TestHTTPCookieStorage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,11 @@ class TestHTTPCookieStorage: XCTestCase {
314314
let bugsSwiftOrgUrl = URL(string: "https://BUGS.swift.org")!
315315
let exampleComUrl = URL(string: "http://www.example.com")!
316316
let superSwiftOrgUrl = URL(string: "https://superswift.org")!
317-
XCTAssertEqual(storage.cookies(for: swiftOrgUrl)!.count, 2)
318-
XCTAssertEqual(storage.cookies(for: ciSwiftOrgUrl)!.count, 1)
319-
XCTAssertEqual(storage.cookies(for: bugsSwiftOrgUrl)!.count, 2)
320-
XCTAssertEqual(storage.cookies(for: exampleComUrl)!.count, 0)
321-
XCTAssertEqual(storage.cookies(for: superSwiftOrgUrl)!.count, 0)
317+
XCTAssertEqual(Set(storage.cookies(for: swiftOrgUrl)!), Set([simpleCookie1, simpleCookie2]))
318+
XCTAssertEqual(storage.cookies(for: ciSwiftOrgUrl)!, [simpleCookie2])
319+
XCTAssertEqual(Set(storage.cookies(for: bugsSwiftOrgUrl)!), Set([simpleCookie2, simpleCookie3]))
320+
XCTAssertEqual(storage.cookies(for: exampleComUrl)!, [])
321+
XCTAssertEqual(storage.cookies(for: superSwiftOrgUrl)!, [])
322322
}
323323

324324
func test_cookieInXDGSpecPath() {

0 commit comments

Comments
 (0)