mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
fix: URL matching fails in presence of a path #1940
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.kunzisoft.keepass.tests.utils
|
||||
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.inTheSameDomainAs
|
||||
import junit.framework.TestCase
|
||||
|
||||
class UriHelperTest: TestCase() {
|
||||
|
||||
fun testBuildURL() {
|
||||
val expected = "domain.org"
|
||||
|
||||
assertTrue(expected.inTheSameDomainAs("domain.org", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("http://domain.org", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("https://domain.org", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("domain.org/login", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("http://domain.org/login", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("https://domain.org/login", sameSubDomain = false))
|
||||
|
||||
assertTrue(expected.inTheSameDomainAs("https://www.domain.org", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("www.domain.org", sameSubDomain = false))
|
||||
assertTrue(expected.inTheSameDomainAs("ww.domain.org", sameSubDomain = false))
|
||||
|
||||
assertTrue(expected.inTheSameDomainAs("domain.org", sameSubDomain = true))
|
||||
assertTrue(expected.inTheSameDomainAs("http://domain.org", sameSubDomain = true))
|
||||
assertTrue(expected.inTheSameDomainAs("https://domain.org", sameSubDomain = true))
|
||||
assertTrue(expected.inTheSameDomainAs("domain.org/login", sameSubDomain = true))
|
||||
assertTrue(expected.inTheSameDomainAs("http://domain.org/login", sameSubDomain = true))
|
||||
assertTrue(expected.inTheSameDomainAs("https://domain.org/login", sameSubDomain = true))
|
||||
|
||||
assertFalse(expected.inTheSameDomainAs("https://www.domain.org", sameSubDomain = true))
|
||||
assertFalse(expected.inTheSameDomainAs("www.domain.org", sameSubDomain = true))
|
||||
assertFalse(expected.inTheSameDomainAs("ww.domain.org", sameSubDomain = true))
|
||||
|
||||
assertFalse(expected.inTheSameDomainAs("domain.com", sameSubDomain = false))
|
||||
assertFalse(expected.inTheSameDomainAs("omain.org", sameSubDomain = false))
|
||||
assertFalse(expected.inTheSameDomainAs("odomain.org", sameSubDomain = false))
|
||||
assertFalse(expected.inTheSameDomainAs("tcp://domain.org", sameSubDomain = false))
|
||||
assertFalse(expected.inTheSameDomainAs("dom.org/domain.org", sameSubDomain = false))
|
||||
|
||||
assertFalse(expected.inTheSameDomainAs("domain.com", sameSubDomain = true))
|
||||
assertFalse(expected.inTheSameDomainAs("omain.org", sameSubDomain = true))
|
||||
assertFalse(expected.inTheSameDomainAs("odomain.org", sameSubDomain = true))
|
||||
assertFalse(expected.inTheSameDomainAs("tcp://domain.org", sameSubDomain = true))
|
||||
assertFalse(expected.inTheSameDomainAs("dom.org/domain.org", sameSubDomain = true))
|
||||
}
|
||||
|
||||
fun testMaxValue() {
|
||||
val maxValue = UnsignedInt.MAX_VALUE.toKotlinLong()
|
||||
assertEquals(4294967295L, maxValue)
|
||||
val longValue = UnsignedInt.fromKotlinLong(4294967295L).toKotlinLong()
|
||||
assertEquals(longValue, maxValue)
|
||||
}
|
||||
|
||||
fun testLong() {
|
||||
val longValue = UnsignedInt.fromKotlinLong(50L).toKotlinInt()
|
||||
assertEquals(50, longValue)
|
||||
val uIntLongValue = UnsignedInt.fromKotlinLong(4294967290).toKotlinLong()
|
||||
assertEquals(4294967290, uIntLongValue)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user