Phone pairing: clearer Grown-ups PIN step, per-phone rate limit, address-only Host, outcome logs (from release 0.108.48)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-06 09:45:23 +00:00
co-authored by Claude Opus 5.5
parent 7ea43f84f8
commit 4bbeda2215
4 changed files with 90 additions and 16 deletions
@@ -33,7 +33,7 @@ class PhoneLinkTest {
{ emptyList() }, { c, s -> made = c to s; "${c.name} is ready" }, { emptyList() }), { false }, { watched += it })
PhoneLink.start()
}
@After fun down() { PhoneLink.stop(); PhoneLink.handler = null; PhoneLink.allowed = EmbyServer::isHomeAddress; PhoneLink.bindTo = null; PhoneLink.port = PhoneLink.PORT; CustomChannels.clear() }
@After fun down() { PhoneLink.stop(); PhoneLink.handler = null; PhoneLink.allowed = EmbyServer::isHomeAddress; PhoneLink.bindTo = null; PhoneLink.port = PhoneLink.PORT; PhoneLink.clock = System::currentTimeMillis; CustomChannels.clear() }
private class Reply(val status: Int, val body: String, val cookie: String?)
private fun call(path: String, method: String = "GET", body: String? = null, cookie: String? = null, form: Boolean = false, orbit: Boolean = true): Reply {
@@ -79,16 +79,49 @@ class PhoneLinkTest {
}
@Test fun guessingRunsOutAndThePinIsAskedWhenThereIsOne() {
var now = System.currentTimeMillis(); PhoneLink.clock = { now }
val p = PhoneLink.beginPairing()
val wrong = if (p.code == "1111") "2222" else "1111"
repeat(5) { call("/join", "POST", "code=$wrong", form = true) }
assertTrue(call("/join", "POST", "code=${p.code}", form = true).body.contains("Too many tries"))
val refused = call("/join", "POST", "code=${p.code}", form = true)
assertEquals(429, refused.status); assertTrue(refused.body.contains("Too many wrong tries from this phone")) // this phone, for ten minutes
assertTrue(phones.isEmpty())
now += 11 * 60_000L // ten minutes on: heard again
PhoneLink.pinNeeded = { true }; PhoneLink.pinCheck = { it == "4321" }
val q = PhoneLink.beginPairing()
assertTrue(call("/join", "POST", "code=${q.code}", form = true).body.contains("Grown-ups PIN"))
assertTrue(call("/join", "POST", "code=${q.code}&pin=1234", form = true).body.contains("didn't match"))
val asked = call("/join", "POST", "code=${q.code}", form = true).body
assertTrue(asked.contains("Grown-ups PIN")); assertTrue(asked.contains("isn't the code on the TV screen"))
assertTrue(call("/join", "POST", "code=${q.code}&pin=${q.code}", form = true).body.contains("not the TV's code"))
repeat(3) { call("/join", "POST", "code=${q.code}", form = true) } // asking again spends no tries
assertEquals(302, call("/join", "POST", "code=${q.code}&pin=4321", form = true).status)
PhoneLink.pinNeeded = { false }
}
@Test fun threeWrongPinsEndTheCode() {
PhoneLink.pinNeeded = { true }; PhoneLink.pinCheck = { it == "4321" }
val p = PhoneLink.beginPairing()
repeat(2) { assertTrue(call("/join", "POST", "code=${p.code}&pin=0000", form = true).body.contains("didn't match")) }
assertTrue(call("/join", "POST", "code=${p.code}&pin=0000", form = true).body.contains("three times"))
assertNull(PhoneLink.pairing)
assertTrue(call("/join", "POST", "code=${p.code}&pin=4321", form = true).body.contains("run out"))
PhoneLink.pinNeeded = { false }
}
@Test fun onlyTheTvsAddressIsAnsweredNotABorrowedName() {
// A raw request: Java's own client won't let a Host be made up.
fun statusFor(host: String) = java.net.Socket(InetAddress.getLoopbackAddress(), port).use { s ->
s.getOutputStream().write("GET / HTTP/1.1\r\nHost: $host\r\n\r\n".toByteArray())
s.getInputStream().bufferedReader().readLine() }
assertTrue(statusFor("evil.example:$port").contains(" 400 "))
assertTrue(statusFor("127.0.0.1:$port").contains(" 200 "))
}
@Test fun thePhoneIsGivenCatalogueMetadataOnly() {
val p = PhoneLink.beginPairing()
val cookie = call("/join", "POST", "code=${p.code}", form = true).cookie!!
val suggest = JSONArray(call("/api/suggest?d=sci-fi", cookie = cookie).body).getJSONObject(0)
assertEquals(setOf("id", "title", "year", "type", "reason"), suggest.keys().asSequence().toSet())
val everything = call("/api/channels", cookie = cookie).body + suggest
listOf("token", "api_key", "password", "X-Emby", "MediaBrowser", "http").forEach { assertFalse(it, everything.contains(it, ignoreCase = true)) }
}
}