fix: Add more headers similar to what Bitwarden desktop uses #643

This commit is contained in:
Artem Chepurnoy 2024-10-17 15:07:10 +03:00
parent 05b1bc5ae0
commit 8f6d527a42
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
3 changed files with 28 additions and 2 deletions

View File

@ -6,10 +6,11 @@ expect val Platform.userAgent: String
// Taken from: // Taken from:
// https://releases.electronjs.org/releases/stable // 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 // Seems like desktop clients always use the Windows user-agents for
// privacy reasons. // privacy reasons.
val Platform.defaultUserAgent: String 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"

View File

@ -1,6 +1,7 @@
package com.artemchep.keyguard.provider.bitwarden.api.builder package com.artemchep.keyguard.provider.bitwarden.api.builder
import com.artemchep.keyguard.platform.CurrentPlatform 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.ServerEnv
import com.artemchep.keyguard.provider.bitwarden.api.BitwardenPersona import com.artemchep.keyguard.provider.bitwarden.api.BitwardenPersona
import com.artemchep.keyguard.provider.bitwarden.api.entity.SyncResponse 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.http.contentType import io.ktor.http.contentType
import io.ktor.util.AttributeKey import io.ktor.util.AttributeKey
import java.util.Locale
val routeAttribute = AttributeKey<String>("route") val routeAttribute = AttributeKey<String>("route")
@ -559,6 +561,19 @@ fun HttpRequestBuilder.headers(env: ServerEnv) {
.let(BitwardenPersona::of) .let(BitwardenPersona::of)
header("Bitwarden-Client-Name", persona.clientName) header("Bitwarden-Client-Name", persona.clientName)
header("Bitwarden-Client-Version", persona.clientVersion) 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 // App does not work if hidden behind reverse-proxy under
// a subdirectory. We should specify the 'referer' so the server // a subdirectory. We should specify the 'referer' so the server
// generates correct urls for us. // generates correct urls for us.

View File

@ -52,6 +52,10 @@ data class BitwardenPersona(
val deviceType: String, val deviceType: String,
val deviceName: String, val deviceName: String,
val userAgent: 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 { companion object {
const val CLIENT_VERSION = "2024.4.0" const val CLIENT_VERSION = "2024.4.0"
@ -78,6 +82,8 @@ data class BitwardenPersona(
deviceType = "8", deviceType = "8",
deviceName = "linux", deviceName = "linux",
userAgent = defaultUserAgent, userAgent = defaultUserAgent,
chUaMobile = "?0",
chUaPlatform = "Linux",
) )
private fun Platform.Desktop.MacOS.bitwardenPersona( private fun Platform.Desktop.MacOS.bitwardenPersona(
@ -88,6 +94,8 @@ data class BitwardenPersona(
deviceType = "7", deviceType = "7",
deviceName = "macos", deviceName = "macos",
userAgent = defaultUserAgent, userAgent = defaultUserAgent,
chUaMobile = "?0",
chUaPlatform = "macOS",
) )
private fun Platform.Desktop.Windows.bitwardenPersona( private fun Platform.Desktop.Windows.bitwardenPersona(
@ -98,6 +106,8 @@ data class BitwardenPersona(
deviceType = "6", deviceType = "6",
deviceName = "windows", deviceName = "windows",
userAgent = defaultUserAgent, userAgent = defaultUserAgent,
chUaMobile = "?0",
chUaPlatform = "Windows",
) )
} }
} }