Update the HOST type cipher matching to ignore the port (#3611)

This commit is contained in:
David Perez 2024-07-23 10:38:55 -05:00 committed by GitHub
parent 05dc220303
commit 779cd1356a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 22 deletions

View file

@ -7,7 +7,7 @@ import com.bitwarden.vault.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.util.firstWithTimeoutOrNull
import com.x8bit.bitwarden.data.platform.util.getDomainOrNull
import com.x8bit.bitwarden.data.platform.util.getHostWithPortOrNull
import com.x8bit.bitwarden.data.platform.util.getHostOrNull
import com.x8bit.bitwarden.data.platform.util.getWebHostFromAndroidUriOrNull
import com.x8bit.bitwarden.data.platform.util.isAndroidApp
import com.x8bit.bitwarden.data.platform.util.regexOrNull
@ -210,8 +210,8 @@ private fun LoginUriView.checkForMatch(
UriMatchType.EXACT -> exactIfTrue(loginViewUri == matchUri)
UriMatchType.HOST -> {
val loginUriHost = loginViewUri.getHostWithPortOrNull()
val matchUriHost = matchUri.getHostWithPortOrNull()
val loginUriHost = loginViewUri.getHostOrNull()
val matchUriHost = matchUri.getHostOrNull()
exactIfTrue(matchUriHost != null && loginUriHost == matchUriHost)
}

View file

@ -53,22 +53,10 @@ fun String.getDomainOrNull(context: Context): String? =
?.parseDomainOrNull(context = context)
/**
* Extract the host with port from this [String] if possible, otherwise return null.
* Extract the host from this [String] if possible, otherwise return null.
*/
@OmitFromCoverage
fun String.getHostWithPortOrNull(): String? =
this
.toUriOrNull()
?.let { uri ->
val host = uri.host
val port = uri.port
if (host != null && port != -1) {
"$host:$port"
} else {
null
}
}
fun String.getHostOrNull(): String? = this.toUriOrNull()?.host
/**
* Find the indices of the last occurrences of [substring] within this [String]. Return null if no

View file

@ -8,7 +8,7 @@ import com.bitwarden.vault.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.DataState
import com.x8bit.bitwarden.data.platform.util.getDomainOrNull
import com.x8bit.bitwarden.data.platform.util.getHostWithPortOrNull
import com.x8bit.bitwarden.data.platform.util.getHostOrNull
import com.x8bit.bitwarden.data.platform.util.getWebHostFromAndroidUriOrNull
import com.x8bit.bitwarden.data.platform.util.isAndroidApp
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
@ -357,7 +357,7 @@ class CipherMatchingManagerTest {
with(uri) {
every { isAndroidApp() } returns isAndroidApp
every { getDomainOrNull(context = context) } returns this.takeIf { isAndroidApp }
every { getHostWithPortOrNull() } returns HOST_WITH_PORT
every { getHostOrNull() } returns HOST
every {
getWebHostFromAndroidUriOrNull()
} returns ANDROID_APP_WEB_URL.takeIf { isAndroidApp }
@ -378,8 +378,8 @@ class CipherMatchingManagerTest {
DEFAULT_LOGIN_VIEW_URI_FIVE.getDomainOrNull(context = context)
} returns null
every { HOST_LOGIN_VIEW_URI_MATCHING.getHostWithPortOrNull() } returns HOST_WITH_PORT
every { HOST_LOGIN_VIEW_URI_NOT_MATCHING.getHostWithPortOrNull() } returns null
every { HOST_LOGIN_VIEW_URI_MATCHING.getHostOrNull() } returns HOST
every { HOST_LOGIN_VIEW_URI_NOT_MATCHING.getHostOrNull() } returns null
}
}
@ -415,4 +415,4 @@ private const val DEFAULT_LOGIN_VIEW_URI_FIVE: String = "DEFAULT_LOGIN_VIEW_URI_
// Setup state for host ciphers
private const val HOST_LOGIN_VIEW_URI_MATCHING: String = "DEFAULT_LOGIN_VIEW_URI_MATCHING"
private const val HOST_LOGIN_VIEW_URI_NOT_MATCHING: String = "DEFAULT_LOGIN_VIEW_URI_NOT_MATCHING"
private const val HOST_WITH_PORT: String = "HOST_WITH_PORT"
private const val HOST: String = "HOST"