Skip to content

Commit

Permalink
remove expired cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Jan 24, 2024
1 parent 4ea9f25 commit 4a9120d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Core/CookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class CookieStorage {
}
}

private(set) var cookies: [HTTPCookie] {
/// Use the `updateCookies` function rather than the setter which is only visible for testing.
var cookies: [HTTPCookie] {
get {
var storedCookies = [HTTPCookie]()
if let cookies = userDefaults.object(forKey: Keys.allowedCookies) as? [[String: Any?]] {
Expand Down Expand Up @@ -118,7 +119,9 @@ public class CookieStorage {
}
}

let now = Date()
self.cookies = persistedCookiesByDomain.map { $0.value }.joined().compactMap { $0 }
.filter { $0.expiresDate == nil || $0.expiresDate! > now }

return diagnosticResult
}
Expand Down
46 changes: 44 additions & 2 deletions DuckDuckGoTests/CookieStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ public class CookieStorageTests: XCTestCase {
logins.clearAll()
}

func testWhenUpdatedThenCookiesWithFutureExpirationAreNotRemoved() {
storage.updateCookies([
make("test.com", name: "x", value: "1", expires: .distantFuture),
], keepingPreservedLogins: logins)

storage.updateCookies([
make("example.com", name: "x", value: "1"),
], keepingPreservedLogins: logins)

XCTAssertEqual(2, storage.cookies.count)
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "test.com" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "example.com" }))

}

func testWhenUpdatingThenExistingExpiredCookiesAreRemoved() {
storage.cookies = [
make("test.com", name: "x", value: "1", expires: Date(timeIntervalSinceNow: -100)),
]
XCTAssertEqual(1, storage.cookies.count)

storage.updateCookies([
make("example.com", name: "x", value: "1"),
], keepingPreservedLogins: logins)

XCTAssertEqual(1, storage.cookies.count)
XCTAssertFalse(storage.cookies.contains(where: { $0.domain == "test.com" }))
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "example.com" }))

}

func testWhenExpiredCookieIsAddedThenItIsNotPersisted() {

storage.updateCookies([
make("example.com", name: "x", value: "1", expires: Date(timeIntervalSinceNow: -100)),
], keepingPreservedLogins: logins)

XCTAssertEqual(0, storage.cookies.count)

}

func testWhenUpdatedThenNoLongerPreservedDomainsAreCleared() {
storage.updateCookies([
make("test.com", name: "x", value: "1"),
Expand Down Expand Up @@ -118,13 +159,14 @@ public class CookieStorageTests: XCTestCase {
XCTAssertTrue(storage.cookies.contains(where: { $0.domain == "test.com" && $0.name == "y" && $0.value == "3" }))
}

func make(_ domain: String, name: String, value: String) -> HTTPCookie {
func make(_ domain: String, name: String, value: String, expires: Date? = nil) -> HTTPCookie {
logins.addToAllowed(domain: domain)
return HTTPCookie(properties: [
.domain: domain,
.name: name,
.value: value,
.path: "/"
.path: "/",
.expires: expires as Any
])!
}

Expand Down

0 comments on commit 4a9120d

Please sign in to comment.