@@ -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