Skip to content

Commit 6b9a5b3

Browse files
committed
Fix old cookies being used when domain is preceded by "."
1 parent c75c8c1 commit 6b9a5b3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

templates/kmp/shared/src/androidMain/kotlin/io/package/cookies/stores/DataStoreCookieStorage.kt.twig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ class DataStoreCookieStorage(private val dataStoreManager: DataStoreManager) : C
3535
return cookie.expiration.let { System.currentTimeMillis() < it }
3636
}
3737

38-
private fun domainMatches(cookieDomain: String, requestHost: String): Boolean {
39-
val cd = cookieDomain.lowercase()
40-
val host = requestHost.lowercase()
41-
return if (cd.startsWith(".")) {
42-
host == cd.substring(1) || host.endsWith(cd)
38+
private fun domainMatches(cookieDomain: String?, requestHost: String?): Boolean {
39+
val cd = cookieDomain?.lowercase()
40+
val host = requestHost?.lowercase()
41+
return if (cd != null && host != null) {
42+
if (cd.startsWith(".")) {
43+
host == cd.substring(1) || host.endsWith(cd)
44+
} else if (requestHost.startsWith(".")){
45+
host == host.substring(1) || cd.endsWith(cd)
46+
} else {
47+
host == cd
48+
}
4349
} else {
44-
host == cd
50+
false
4551
}
4652
}
4753

@@ -74,7 +80,7 @@ class DataStoreCookieStorage(private val dataStoreManager: DataStoreManager) : C
7480
val cookies = loadCookies().toMutableList()
7581
cookies.removeAll { existing ->
7682
existing.name == newCookie.name &&
77-
existing.domain == newCookie.domain &&
83+
domainMatches(existing.domain, newCookie.domain) &&
7884
existing.path == newCookie.path
7985
}
8086
cookies.add(newCookie)

0 commit comments

Comments
 (0)