Skip to content

Commit

Permalink
Disable cipher key encryption for older server versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mpbw2 committed Oct 1, 2024
1 parent b3e885b commit 7ab88ee
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.manager
import com.x8bit.bitwarden.data.platform.datasource.disk.model.ServerConfig
import com.x8bit.bitwarden.data.platform.manager.model.FlagKey
import com.x8bit.bitwarden.data.platform.repository.ServerConfigRepository
import com.x8bit.bitwarden.data.platform.util.isServerVersionAtLeast
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

Expand All @@ -16,7 +17,8 @@ class FeatureFlagManagerImpl(
) : FeatureFlagManager {

override val sdkFeatureFlags: Map<String, Boolean>
get() = mapOf(CIPHER_KEY_ENCRYPTION_KEY to true)
get() = mapOf(CIPHER_KEY_ENCRYPTION_KEY to
isServerVersionAtLeast(serverConfigRepository, "2024.2.0"))

override fun <T : Any> getFeatureFlagFlow(key: FlagKey<T>): Flow<T> =
serverConfigRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ interface ServerConfigRepository {
* updates the values using server side data.
*/
suspend fun getServerConfig(forceRefresh: Boolean): ServerConfig?

/**
* Gets the state [ServerConfig] synchronously from local storage only.
*/
fun getLocalServerConfig(): ServerConfig?
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class ServerConfigRepositoryImpl(
return localConfig
}

override fun getLocalServerConfig(): ServerConfig? {
return configDiskSource.serverConfig
}

private companion object {
private const val MINIMUM_CONFIG_SYNC_INTERVAL_SEC: Long = 60 * 60
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.x8bit.bitwarden.data.platform.util

import com.x8bit.bitwarden.data.platform.repository.ServerConfigRepository
import kotlin.text.split
import kotlin.text.toIntOrNull

private const val VERSION_SEPARATOR = "."
private const val SUFFIX_SEPARATOR = "-"

/**
* Checks if the server version is greater than another provided version, returns true if it is.
*/
fun isServerVersionAtLeast(serverConfig: ServerConfigRepository, version: String): Boolean {
val serverVersion = serverConfig
.getLocalServerConfig()
?.serverData
?.version

val serverVersionParts = getVersionComponents(serverVersion)
val otherVersionParts = getVersionComponents(version)

if (serverVersionParts == null || otherVersionParts == null) {
return false
}

for (i in serverVersionParts.indices) {
val serverPart = serverVersionParts.getOrNull(i)?.toIntOrNull() ?: 0
val otherPart = otherVersionParts.getOrNull(i)?.toIntOrNull() ?: 0

if (serverPart > otherPart) {
return true
} else if (serverPart < otherPart) {
return false
}
}

return true // Versions are equal
}

/**
* Extracts the version components from a version string, disregarding any suffixes.
*/
private fun getVersionComponents(version: String?): List<String>? {
val versionComponents = version?.split(SUFFIX_SEPARATOR)?.first()
return versionComponents?.split(VERSION_SEPARATOR)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class FakeServerConfigRepository : ServerConfigRepository {
return serverConfigValue
}

override fun getLocalServerConfig(): ServerConfig? {
return SERVER_CONFIG
}

override val serverConfigStateFlow: StateFlow<ServerConfig?>
get() = mutableServerConfigFlow
}
Expand Down

0 comments on commit 7ab88ee

Please sign in to comment.