94 lignes
3.7 KiB
Kotlin
94 lignes
3.7 KiB
Kotlin
// 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") })
|
|
}
|
|
}
|