fix: URL matching fails in presence of a path #1940

This commit is contained in:
J-Jamet committed 2024-11-25 14:21:39 +01:00
1 parent fb1459de9b
commit 9dec308caa
6 files changed
+114 -8

No files matched your search

@@ -26,6 +26,7 @@ import com.kunzisoft.keepass.database.element.node.NodeHandler
import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.otp.OtpEntryFields.OTP_FIELD
import com.kunzisoft.keepass.utils.UuidUtil
import com.kunzisoft.keepass.utils.inTheSameDomainAs
class SearchHelper {
@@ -149,14 +150,12 @@ class SearchHelper {
}
if (searchParameters.searchInUrls) {
if (checkSearchQuery(entry.url, searchParameters) { stringToCheck, word ->
// domain.org
stringToCheck.equals(word, !searchParameters.caseSensitive) ||
// subdomain.domain.org
stringToCheck.endsWith(".$word", !searchParameters.caseSensitive) ||
// https://domain.org
stringToCheck.endsWith("/$word", !searchParameters.caseSensitive)
// Don't allow mydomain.org
})
try {
stringToCheck.inTheSameDomainAs(word, sameSubDomain = true)
} catch (e: Exception) {
false
}
})
return true
}
if (searchParameters.searchInNotes) {
@@ -30,6 +30,8 @@ import android.net.Uri
import android.os.Build
import android.util.Log
import java.io.*
import java.net.MalformedURLException
import java.net.URL
import java.util.*
fun String.parseUri(): Uri? {
@@ -48,6 +50,48 @@ fun Context.getBinaryDir(): File {
}
}
fun String.buildURLFromDomain(): URL? {
return try {
URL(this)
} catch (e: MalformedURLException) {
try {
URL("https://$this")
} catch (e: Exception) {
null
}
}
}
fun URL.inTheSameDomainAs(url: URL?, sameSubDomain: Boolean = false): Boolean {
val hostA = this.host
val hostB = url?.host
if (hostA == null)
return false
if (hostB == null)
return false
// Each domains are equals (ie: domain.org)
if (hostA.equals(hostB, ignoreCase = true))
return true
// If we need exactly the same subdomain, it's not a match
if (sameSubDomain)
return false
// If contains subdomains (ie: subdomain.domain.org)
if (hostB.endsWith(".$hostA", ignoreCase = true))
return true
if (hostA.endsWith(".$hostB", ignoreCase = true))
return true
return false
}
fun String.inTheSameDomainAs(value: String?, sameSubDomain: Boolean = false): Boolean {
// Don't need to construct URL object if strings are equals
if (this.equals(value, true))
return true
// Checks the real domains
return this.buildURLFromDomain()
?.inTheSameDomainAs(value?.buildURLFromDomain(), sameSubDomain) == true
}
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriInputStream(fileUri: Uri?): InputStream? {
if (fileUri == null)