From eb3fb7cef2009715f2741dc4a20c3f7e3154055b Mon Sep 17 00:00:00 2001 From: Pauljd Date: Sun, 8 Nov 2026 10:21:10 +0000 Subject: [PATCH] One wrong-PIN limit for every PIN entry (0.108.105) Audit S3. AfterDarkLock.check now carries the limit, so the TV unlock, profile switch, PIN change, room approval, phone joining and the phone's profile switch all share it: five wrong PINs, then a one-minute wait that doubles with each further miss up to 30 minutes; no PIN is checked during the wait; the right PIN clears it. The count survives restarts; the PIN itself is never stored. Screens say how long to wait. Co-Authored-By: Claude Opus 5.5 --- android/app/build.gradle.kts | 4 +- .../java/tv/orbit/classic/AfterDarkLock.kt | 41 ++++++++++++++++-- .../main/java/tv/orbit/classic/EmbyClient.kt | 4 +- .../java/tv/orbit/classic/OrbitCompanion.kt | 2 +- .../main/java/tv/orbit/classic/Profiles.kt | 2 +- .../src/main/java/tv/orbit/classic/RoomsUi.kt | 4 +- .../tv/orbit/classic/SecureMediaDataSource.kt | 2 +- .../java/tv/orbit/classic/PinLimitTest.kt | 42 +++++++++++++++++++ 8 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 android/app/src/test/java/tv/orbit/classic/PinLimitTest.kt diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index ada23649..f6e6b818 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -9,8 +9,8 @@ android { applicationId = "tv.orbit.classic" minSdk = 26 targetSdk = 36 - versionCode = 264 - versionName = "0.108.104-morning-fixes" + versionCode = 265 + versionName = "0.108.105-pin-limit" } buildFeatures { compose = true } // The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so diff --git a/android/app/src/main/java/tv/orbit/classic/AfterDarkLock.kt b/android/app/src/main/java/tv/orbit/classic/AfterDarkLock.kt index de4e6ca6..ac187f14 100644 --- a/android/app/src/main/java/tv/orbit/classic/AfterDarkLock.kt +++ b/android/app/src/main/java/tv/orbit/classic/AfterDarkLock.kt @@ -60,11 +60,24 @@ internal object AfterDarkLock { fun setNightly(context: Context, on: Boolean) { nightly = on; prefs(context).edit().putBoolean("nightly_lock", on).apply() } private fun prefs(context: Context) = context.getSharedPreferences("orbit_lock", Context.MODE_PRIVATE) - fun check(context: Context, pin: String): Boolean { + /** + * Every PIN check, wherever it's entered (the TV, a phone, a room asking to join), goes through here and its wrong-PIN + * limit (8 November 2026, audit S3): after [PIN_FREE_MISSES] wrong PINs a wait, doubling with each further miss. During + * the wait no PIN is checked, not even the right one. The count survives restarts; the PIN itself is never stored. + */ + fun check(context: Context, pin: String, nowMs: Long = System.currentTimeMillis()): Boolean { val p = prefs(context) + val before = PinLimit(p.getInt("pin_misses", 0), p.getLong("pin_wait_until", 0L)) + if (before.waiting(nowMs)) return false val salt = p.getString("after_dark_salt", null) ?: return false - return p.getString("after_dark_pin", null) == pinHash(salt, pin) + val right = p.getString("after_dark_pin", null) == pinHash(salt, pin) + val after = before.after(right, nowMs) + if (after != before) p.edit().putInt("pin_misses", after.misses).putLong("pin_wait_until", after.waitUntilMs).apply() + return right } + /** What to say after a refused PIN: how long to wait, when there's a wait. */ + fun refusal(context: Context, wrong: String = "That's not the PIN", nowMs: Long = System.currentTimeMillis()): String = + prefs(context).let { p -> PinLimit(p.getInt("pin_misses", 0), p.getLong("pin_wait_until", 0L)) }.waitText(nowMs) ?: wrong fun set(context: Context, pin: String) { val salt = java.util.UUID.randomUUID().toString() prefs(context).edit().putString("after_dark_salt", salt).putString("after_dark_pin", pinHash(salt, pin)).putLong("pin_changed_at", System.currentTimeMillis()).apply() @@ -86,6 +99,26 @@ internal object AfterDarkLock { fun lock() { unlockedState = false; unlockedAt = 0L; store?.edit()?.remove("unlocked_at")?.apply() } } +/** Wrong PINs allowed before the first wait. */ +internal const val PIN_FREE_MISSES = 5 +internal const val PIN_FIRST_WAIT_MS = 60_000L +internal const val PIN_LONGEST_WAIT_MS = 30 * 60_000L + +/** The wrong-PIN count and any wait it has caused. Pure, so it can be tested without Android. */ +internal data class PinLimit(val misses: Int = 0, val waitUntilMs: Long = 0L) { + fun waiting(nowMs: Long) = nowMs < waitUntilMs + /** After a PIN checked outside a wait: the right one clears it; the fifth wrong one and each after it sets a longer wait. */ + fun after(right: Boolean, nowMs: Long): PinLimit { + if (right) return PinLimit() + val n = misses + 1 + if (n < PIN_FREE_MISSES) return PinLimit(n, 0L) + val wait = (PIN_FIRST_WAIT_MS shl (n - PIN_FREE_MISSES).coerceAtMost(10)).coerceAtMost(PIN_LONGEST_WAIT_MS) + return PinLimit(n, nowMs + wait) + } + fun waitText(nowMs: Long): String? = if (!waiting(nowMs)) null else + ((waitUntilMs - nowMs + 59_999) / 60_000).let { m -> "Too many tries. Try again in $m minute${if (m == 1L) "" else "s"}." } +} + /** The hour the optional nightly lock falls, 3am. */ internal const val NIGHTLY_LOCK_HOUR = 3 @@ -181,10 +214,10 @@ internal fun LockDetail(firstSwitch: FocusRequester, modifier: Modifier) { } } LockStep.Unlock -> PinEntry("Enter the PIN", problem, onDone = { pin -> - if (AfterDarkLock.unlock(context, pin)) { step = LockStep.None; note = if (AfterDarkLock.nightly) "Unlocked until 3am." else "Unlocked until you lock it." } else problem = "That's not the PIN" + if (AfterDarkLock.unlock(context, pin)) { step = LockStep.None; note = if (AfterDarkLock.nightly) "Unlocked until 3am." else "Unlocked until you lock it." } else problem = AfterDarkLock.refusal(context) }, onCancel = { step = LockStep.None }) LockStep.CheckForChange, LockStep.CheckForOff -> PinEntry("Enter the current PIN", problem, onDone = { pin -> - if (!AfterDarkLock.check(context, pin)) problem = "That's not the PIN" + if (!AfterDarkLock.check(context, pin)) problem = AfterDarkLock.refusal(context) else if (step == LockStep.CheckForOff) { AfterDarkLock.clear(context); step = LockStep.None; note = "The lock is off." } else begin(LockStep.NewPin) }, onCancel = { step = LockStep.None }) diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt b/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt index 2ca0366b..71da6bfa 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt @@ -62,9 +62,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI } /** The sign-in headers for this kind of server. */ internal fun authHeaders(token: String?): Map = when (kind) { - ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.104\"") + + ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.105\"") + (token?.let { mapOf("X-Emby-Token" to it) } ?: emptyMap()) - ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.104\"" + + ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.105\"" + (token?.let { ", Token=\"$it\"" } ?: "")) } /** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */ diff --git a/android/app/src/main/java/tv/orbit/classic/OrbitCompanion.kt b/android/app/src/main/java/tv/orbit/classic/OrbitCompanion.kt index e8a2ce9c..95d71070 100644 --- a/android/app/src/main/java/tv/orbit/classic/OrbitCompanion.kt +++ b/android/app/src/main/java/tv/orbit/classic/OrbitCompanion.kt @@ -237,7 +237,7 @@ internal object OrbitCompanion { if (pin.isNullOrEmpty()) return "PIN" if (context == null || !AfterDarkLock.unlock(context, pin)) { if (++pinMisses >= 3) { pinMisses = 0; pinWaitUntil = nowMs + 5 * 60_000L } - return "That's not the PIN." + return context?.let { AfterDarkLock.refusal(it, "That's not the PIN.") } ?: "That's not the PIN." } pinMisses = 0 } diff --git a/android/app/src/main/java/tv/orbit/classic/Profiles.kt b/android/app/src/main/java/tv/orbit/classic/Profiles.kt index 556a130e..71273d5f 100644 --- a/android/app/src/main/java/tv/orbit/classic/Profiles.kt +++ b/android/app/src/main/java/tv/orbit/classic/Profiles.kt @@ -254,7 +254,7 @@ internal fun WhosWatching(onClose: () -> Unit) { ProfileAvatar(p.picture, 90.dp) Text(p.name, color = Ink, fontSize = 20.sp, fontWeight = FontWeight.SemiBold) PinEntry("Enter the PIN", problem, onDone = { pin -> - if (AfterDarkLock.unlock(context, pin)) { asking = null; Profiles.current = p } else problem = "That's not the PIN" + if (AfterDarkLock.unlock(context, pin)) { asking = null; Profiles.current = p } else problem = AfterDarkLock.refusal(context) }, onCancel = { asking = null }) } } diff --git a/android/app/src/main/java/tv/orbit/classic/RoomsUi.kt b/android/app/src/main/java/tv/orbit/classic/RoomsUi.kt index 0a2758ac..c2f2c409 100644 --- a/android/app/src/main/java/tv/orbit/classic/RoomsUi.kt +++ b/android/app/src/main/java/tv/orbit/classic/RoomsUi.kt @@ -124,7 +124,7 @@ internal fun RoomKnockOverlay() { BackHandler { if (asking) asking = false else { offer.refuse(); PhoneLink.offerRoom(null) } } OverlayCard(Navy.copy(alpha = .55f)) { if (asking) { PinEntry("Enter the Adult PIN to allow it", problem, onDone = { pin -> - if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = "That PIN didn't match." + if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = AfterDarkLock.refusal(context, "That PIN didn't match.") }, onCancel = { asking = false }); return@OverlayCard } val first = remember { FocusRequester() } Column(Modifier.width(560.dp).background(Panel, RoundedCornerShape(14.dp)).padding(22.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) { @@ -200,7 +200,7 @@ internal fun AddRoomPanel(onClose: () -> Unit, modifier: Modifier = Modifier) { } BackHandler { onClose() } if (asking) { PinEntry("Enter the Adult PIN to allow it", problem, onDone = { pin -> - if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = "That PIN didn't match." + if (AfterDarkLock.check(context, pin)) { asking = false; offer.allow() } else problem = AfterDarkLock.refusal(context, "That PIN didn't match.") }, onCancel = { asking = false }); return } val first = remember { FocusRequester() } Column(modifier, verticalArrangement = Arrangement.spacedBy(10.dp)) { diff --git a/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt b/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt index f0d83a76..61e2ffcf 100644 --- a/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt +++ b/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt @@ -39,7 +39,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B next.instanceFollowRedirects = false next.connectTimeout = 10_000 next.readTimeout = 20_000 - next.setRequestProperty("User-Agent", "OrbitTV/0.108.104") + next.setRequestProperty("User-Agent", "OrbitTV/0.108.105") next.setRequestProperty("Accept-Encoding", "identity") session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) } if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) { diff --git a/android/app/src/test/java/tv/orbit/classic/PinLimitTest.kt b/android/app/src/test/java/tv/orbit/classic/PinLimitTest.kt new file mode 100644 index 00000000..ff444728 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/PinLimitTest.kt @@ -0,0 +1,42 @@ +package tv.orbit.classic + +import org.junit.Assert.* +import org.junit.Test +import java.io.File + +/** 8 November 2026 (audit S3): one wrong-PIN limit for every place a PIN is entered. */ +class PinLimitTest { + private val now = 1_794_000_000_000L + private val minute = 60_000L + private fun misses(n: Int, at: Long = now) = (1..n).fold(PinLimit()) { l, _ -> l.after(false, at) } + + @Test fun fourWrongPinsCostNothing() { + val l = misses(4) + assertEquals(4, l.misses); assertFalse(l.waiting(now)) + } + @Test fun theFifthWrongPinStartsAOneMinuteWait() { + val l = misses(5) + assertTrue(l.waiting(now)); assertTrue(l.waiting(now + minute - 1)); assertFalse(l.waiting(now + minute)) + assertEquals("Too many tries. Try again in 1 minute.", l.waitText(now)) + } + @Test fun eachFurtherMissDoublesTheWaitUpToHalfAnHour() { + assertEquals(now + 2 * minute, misses(6).waitUntilMs) + assertEquals(now + 4 * minute, misses(7).waitUntilMs) + assertEquals(now + 16 * minute, misses(9).waitUntilMs) + assertEquals(now + 30 * minute, misses(10).waitUntilMs) + assertEquals(now + 30 * minute, misses(60).waitUntilMs) + assertEquals("Too many tries. Try again in 30 minutes.", misses(10).waitText(now)) + } + @Test fun theRightPinAfterTheWaitClearsEverything() { + val cleared = misses(7).after(true, now + 10 * minute) + assertEquals(PinLimit(), cleared); assertNull(cleared.waitText(now)) + } + @Test fun everyPinCheckGoesThroughTheLimit() { + // Only AfterDarkLock.check compares against the stored PIN; every screen and the phone server call it. + val sources = File("src/main/java/tv/orbit/classic").listFiles()!!.filter { it.extension == "kt" } + val comparing = sources.filter { f -> f.readText().contains("== pinHash(") } + assertEquals(listOf("AfterDarkLock.kt"), comparing.map { it.name }) + val reading = sources.filter { f -> f.name != "AfterDarkLock.kt" && f.name != "Rooms.kt" && f.readText().contains("\"after_dark_pin\"") } + assertTrue(reading.map { it.name }.toString(), reading.isEmpty()) + } +}