From 8f6d527a42f13c6bdcee2ca3f133dd19751fc37a Mon Sep 17 00:00:00 2001 From: Artem Chepurnoy Date: Thu, 17 Oct 2024 15:07:10 +0300 Subject: [PATCH] fix: Add more headers similar to what Bitwarden desktop uses #643 --- .../keyguard/platform/util/PlatformUserAgent.kt | 5 +++-- .../bitwarden/api/builder/ServerEnvApi.kt | 15 +++++++++++++++ .../keyguard/provider/bitwarden/api/login.kt | 10 ++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt index a32a5abc..f756aba2 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt @@ -6,10 +6,11 @@ expect val Platform.userAgent: String // Taken from: // https://releases.electronjs.org/releases/stable -private const val CHROME_VERSION = "126.0.6478.114" +const val CHROME_MAJOR_VERSION = "126" +const val CHROME_FULL_VERSION = "$CHROME_MAJOR_VERSION.0.6478.114" // Seems like desktop clients always use the Windows user-agents for // privacy reasons. val Platform.defaultUserAgent: String - get() = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$CHROME_VERSION Safari/537.36" + get() = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$CHROME_FULL_VERSION Safari/537.36" diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/builder/ServerEnvApi.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/builder/ServerEnvApi.kt index b75e53c9..cab3fc92 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/builder/ServerEnvApi.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/builder/ServerEnvApi.kt @@ -1,6 +1,7 @@ package com.artemchep.keyguard.provider.bitwarden.api.builder import com.artemchep.keyguard.platform.CurrentPlatform +import com.artemchep.keyguard.platform.util.CHROME_MAJOR_VERSION import com.artemchep.keyguard.provider.bitwarden.ServerEnv import com.artemchep.keyguard.provider.bitwarden.api.BitwardenPersona import com.artemchep.keyguard.provider.bitwarden.api.entity.SyncResponse @@ -31,6 +32,7 @@ import io.ktor.client.statement.HttpResponse import io.ktor.http.ContentType import io.ktor.http.contentType import io.ktor.util.AttributeKey +import java.util.Locale val routeAttribute = AttributeKey("route") @@ -559,6 +561,19 @@ fun HttpRequestBuilder.headers(env: ServerEnv) { .let(BitwardenPersona::of) header("Bitwarden-Client-Name", persona.clientName) header("Bitwarden-Client-Version", persona.clientVersion) + // Cloudflare-pleasing headers that do + // nothing except let Keyguard pass their + // bot detection. + val language = Locale.getDefault().toLanguageTag() + ?: "en-US" + header("Accept-Language", language) + header("Sec-Ch-Ua", """"Not.A/Brand";v="8", "Chromium";v="$CHROME_MAJOR_VERSION"""") + header("Sec-Ch-Ua-Mobile", persona.chUaMobile) + header("Sec-Ch-Ua-Platform", persona.chUaPlatform) + // Potentially needs those: + // header("Sec-Fetch-Dest", "empty") + // header("Sec-Fetch-Mode", "cors") + // header("Sec-Fetch-Site", "cross-site") // App does not work if hidden behind reverse-proxy under // a subdirectory. We should specify the 'referer' so the server // generates correct urls for us. diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt index f53dae96..f607e0e9 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt @@ -52,6 +52,10 @@ data class BitwardenPersona( val deviceType: String, val deviceName: String, val userAgent: String, + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile + val chUaMobile: String, + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform + val chUaPlatform: String, ) { companion object { const val CLIENT_VERSION = "2024.4.0" @@ -78,6 +82,8 @@ data class BitwardenPersona( deviceType = "8", deviceName = "linux", userAgent = defaultUserAgent, + chUaMobile = "?0", + chUaPlatform = "Linux", ) private fun Platform.Desktop.MacOS.bitwardenPersona( @@ -88,6 +94,8 @@ data class BitwardenPersona( deviceType = "7", deviceName = "macos", userAgent = defaultUserAgent, + chUaMobile = "?0", + chUaPlatform = "macOS", ) private fun Platform.Desktop.Windows.bitwardenPersona( @@ -98,6 +106,8 @@ data class BitwardenPersona( deviceType = "6", deviceName = "windows", userAgent = defaultUserAgent, + chUaMobile = "?0", + chUaPlatform = "Windows", ) } }