Loksi 2026.10.0 (Android 3) : source correspondante
Export du dépôt interne d'Aleop, révision 5fbb921.
Cette révision appartient à :
révision
c3608b3938
91 fichiers modifiés
+7297
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,86 @@
|
||||
// GPL-3.0-only. Loksi: vault name or address entered at login (decision of 04/10/2026).
|
||||
// Copied by mobile/android/loksi.py next to EnvironmentUrlDataJsonExtensions.kt. Same rule
|
||||
// as the Loksi browser extension (extensions/loksi-coffre.ts).
|
||||
package com.bitwarden.data.repository.util
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URI
|
||||
import java.net.URL
|
||||
|
||||
/**
|
||||
* Loksi: domain of the Loksi vaults. The vault name "abc" gives https://abc.loksi.fr.
|
||||
*/
|
||||
const val LOKSI_DOMAIN: String = "loksi.fr"
|
||||
|
||||
// The name chosen in the order funnel: same pattern as the order backend (_NOM), 2 to 20
|
||||
// lowercase letters or digits, a hyphen allowed but neither first nor last. Never
|
||||
// interpolated as typed: it matches this pattern or it is refused.
|
||||
private val LOKSI_VAULT_NAME = Regex("^[a-z0-9](?:[a-z0-9-]{0,18}[a-z0-9])$")
|
||||
|
||||
private const val LOKSI_TIMEOUT_MS: Int = 10_000
|
||||
|
||||
/**
|
||||
* Loksi: base address of the vault for what was typed, or null if it is refused.
|
||||
* A vault name gives https://<name>.loksi.fr. Otherwise a full https address without
|
||||
* credentials, query or fragment, for a customer with their own domain.
|
||||
*/
|
||||
fun loksiVaultAddress(input: String?): String? {
|
||||
val value = input.orEmpty().trim()
|
||||
if (LOKSI_VAULT_NAME.matches(value)) {
|
||||
return "https://$value.$LOKSI_DOMAIN"
|
||||
}
|
||||
if (!value.startsWith("https://")) {
|
||||
return null
|
||||
}
|
||||
val uri = try {
|
||||
URI(value)
|
||||
} catch (_: Exception) {
|
||||
return null
|
||||
}
|
||||
if (uri.scheme != "https" || uri.host.isNullOrEmpty() || uri.rawUserInfo != null ||
|
||||
uri.rawQuery != null || uri.rawFragment != null
|
||||
) {
|
||||
return null
|
||||
}
|
||||
val port = if (uri.port == -1) "" else ":${uri.port}"
|
||||
return "https://${uri.host}$port${uri.rawPath.orEmpty()}".trimEnd('/')
|
||||
}
|
||||
|
||||
/**
|
||||
* Loksi: true only if a Vaultwarden server answers on /api/config at [base].
|
||||
* [fetch] returns the body of a successful GET, or null.
|
||||
*/
|
||||
suspend fun isLoksiVault(
|
||||
base: String,
|
||||
fetch: suspend (String) -> String? = ::loksiGet,
|
||||
): Boolean =
|
||||
try {
|
||||
val body = fetch("$base/api/config")
|
||||
body != null &&
|
||||
Json.parseToJsonElement(body).jsonObject["server"]
|
||||
?.jsonObject?.get("name")?.jsonPrimitive?.content == "Vaultwarden"
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
|
||||
private suspend fun loksiGet(url: String): String? = withContext(Dispatchers.IO) {
|
||||
val connection = URL(url).openConnection() as HttpURLConnection
|
||||
try {
|
||||
connection.connectTimeout = LOKSI_TIMEOUT_MS
|
||||
connection.readTimeout = LOKSI_TIMEOUT_MS
|
||||
connection.instanceFollowRedirects = false
|
||||
connection.setRequestProperty("Accept", "application/json")
|
||||
if (connection.responseCode == HttpURLConnection.HTTP_OK) {
|
||||
connection.inputStream.bufferedReader().use { it.readText() }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} finally {
|
||||
connection.disconnect()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
// GPL-3.0-only. Loksi: vault name or address, then /api/config. *.loksi.fr does not exist
|
||||
// yet: the answers come from local servers, as for the browser extension.
|
||||
package com.bitwarden.data.repository.util
|
||||
|
||||
import com.sun.net.httpserver.HttpServer
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.net.InetSocketAddress
|
||||
|
||||
class LoksiVaultTest {
|
||||
@Test
|
||||
fun `a vault name gives its loksi fr vault`() {
|
||||
assertEquals("https://abc.loksi.fr", loksiVaultAddress("abc"))
|
||||
assertEquals("https://aleop.loksi.fr", loksiVaultAddress(" aleop "))
|
||||
assertEquals("https://ab12cd34ef56.loksi.fr", loksiVaultAddress("ab12cd34ef56"))
|
||||
assertEquals("https://mon-entreprise.loksi.fr", loksiVaultAddress("mon-entreprise"))
|
||||
assertEquals("https://${"a".repeat(20)}.loksi.fr", loksiVaultAddress("a".repeat(20)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a full https address is still accepted`() {
|
||||
assertEquals("https://coffre.client.example", loksiVaultAddress("https://coffre.client.example/"))
|
||||
assertEquals("https://loksi.aleop.eu", loksiVaultAddress("https://loksi.aleop.eu"))
|
||||
assertEquals("https://x.example:8443/coffre", loksiVaultAddress("https://x.example:8443/coffre/"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `canaries are refused`() {
|
||||
listOf(
|
||||
"abc.evil.test", "ABC/../x", "http://abc.loksi.fr", "a", "a".repeat(21), "ABC",
|
||||
"-abc", "abc-", "ab_c", "ab c", "é-corp", "https://user:pw@x.example",
|
||||
"https://x.example/?q=1", "https://x.example/#a", "javascript:alert(1)", "",
|
||||
" ", "https://", null,
|
||||
).forEach { refused ->
|
||||
assertNull(loksiVaultAddress(refused), "accepted: $refused")
|
||||
}
|
||||
}
|
||||
|
||||
private fun server(body: String?): HttpServer =
|
||||
HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0).apply {
|
||||
createContext("/") { exchange ->
|
||||
val answer = body?.takeIf { exchange.requestURI.path == "/api/config" }
|
||||
if (answer == null) {
|
||||
exchange.sendResponseHeaders(404, -1)
|
||||
} else {
|
||||
val bytes = answer.toByteArray()
|
||||
exchange.responseHeaders.add("Content-Type", "application/json")
|
||||
exchange.sendResponseHeaders(200, bytes.size.toLong())
|
||||
exchange.responseBody.use { it.write(bytes) }
|
||||
}
|
||||
exchange.close()
|
||||
}
|
||||
start()
|
||||
}
|
||||
|
||||
private fun <T> withServer(body: String?, block: suspend (String) -> T): T {
|
||||
val running = server(body)
|
||||
return try {
|
||||
runBlocking { block("http://127.0.0.1:${running.address.port}") }
|
||||
} finally {
|
||||
running.stop(0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a local Vaultwarden is recognised as a vault`() {
|
||||
assertTrue(withServer("""{"version":"2026.6.0","server":{"name":"Vaultwarden"}}""") { isLoksiVault(it) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `canary, another server answering JSON is refused`() {
|
||||
assertFalse(withServer("""{"version":"2026.6.0","server":{"name":"Bitwarden"}}""") { isLoksiVault(it) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `canary, a page without api config is refused`() {
|
||||
assertFalse(withServer(null) { isLoksiVault(it) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `canary, a body that is not JSON is refused`() {
|
||||
assertFalse(withServer("<html>Vaultwarden</html>") { isLoksiVault(it) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `canary, an address that does not resolve is refused`() {
|
||||
assertFalse(runBlocking { isLoksiVault("https://coffre-a-renseigner.invalid") })
|
||||
}
|
||||
}
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur