Phones stay joined: Lax cookie and a kept token to put it back (0.110.2)

Paul, 8 November 2026: the phone kept asking to join again on the same Wi-Fi. The TV never forgot it (two joins
today); the phone arrived without its cookie. SameSite=Strict cookies are withheld when the page is opened from
another app (a home-screen shortcut, the camera), so the cookie is now Lax; changes still need X-Orbit and an address
as Host. The phone also keeps the token (handed over in the #fragment, never sent to the network again) and the join
page quietly puts the cookie back via POST /rejoin; wrong tokens count as strikes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 13:33:03 +00:00
co-authored by Claude Opus 5.5
parent fe7cc6c5b4
commit a904f5c60d
8 changed files with 43 additions and 7 deletions
@@ -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<String, String> = when (kind) {
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.1\"") +
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.2\"") +
(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.110.1\"" +
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.2\"" +
(token?.let { ", Token=\"$it\"" } ?: ""))
}
/** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */
@@ -184,6 +184,23 @@ internal object PhoneLink {
return prefs().get(id)?.let { id }
}
/**
* The phone's cookie. Lax rather than Strict (8 November 2026): Strict is withheld when the page is opened from another
* app, such as a home-screen shortcut or the camera, so a joined phone looked new. Changes still need the X-Orbit header
* and an address as Host, so another site can't act through it.
*/
private fun cookieHeader(token: String) = "Set-Cookie: $COOKIE=$token; HttpOnly; SameSite=Lax; Path=/; Max-Age=31536000"
/** A joined phone putting its cookie back from the token it kept: only a token this TV issued, and wrong ones count as strikes. */
private fun rejoin(r: Request, out: OutputStream, who: String) {
if (refused(who)) return json(out, 429, JSONObject().put("error", "Too many tries."))
val token = r.body.trim()
val known = token.length == 64 && token.all { it in '0'..'9' || it in 'a'..'f' } && prefs().get(hash(token)) != null
if (!known) { strike(who); note("rejoin_refused"); return json(out, 403, JSONObject().put("joined", false)) }
note("rejoined")
respond(out, 200, "application/json; charset=utf-8", JSONObject().put("joined", true).toString().toByteArray(), listOf(cookieHeader(token)))
}
/** The Host a request names: an address, never a name a page elsewhere could point at this TV. */
private fun hostIsAnAddress(host: String?): Boolean {
val name = host?.trim()?.let { if (it.startsWith("[")) it.substringBefore(']') + "]" else it.substringBefore(':') } ?: return false
@@ -238,6 +255,7 @@ internal object PhoneLink {
val phone = phoneOf(r)
when {
r.path == "/join" -> join(r, out, socket.inetAddress.hostAddress.orEmpty())
r.path == "/rejoin" && r.method == "POST" && r.headers["x-orbit"] == "1" -> rejoin(r, out, socket.inetAddress.hostAddress.orEmpty())
phone == null -> page(out, PhonePages.join(null))
r.path == "/" -> page(out, PhonePages.companion())
r.path == "/create" -> page(out, PhonePages.app())
@@ -284,7 +302,9 @@ internal object PhoneLink {
val name = phoneName(r.headers["user-agent"].orEmpty())
prefs().put(hash(token), "$name|${System.currentTimeMillis()}")
pairing = null; loadPhones(); active = name; strikes.remove(who); note("joined")
respond(out, 302, "text/plain", ByteArray(0), listOf("Location: /", "Set-Cookie: $COOKIE=$token; HttpOnly; SameSite=Strict; Path=/; Max-Age=31536000"))
// The phone also keeps the token itself (8 November 2026: phones kept being asked to join again), handed over in the
// address's #fragment, which never leaves the phone, so a lost or withheld cookie can be put back without the code.
respond(out, 302, "text/plain", ByteArray(0), listOf("Location: /#keep=$token", cookieHeader(token)))
}
private fun phoneName(agent: String) = when {
@@ -8,9 +8,12 @@ body{margin:0;background:#071632;color:#fff;font:16px/1.4 system-ui,sans-serif;d
main{width:min(92vw,380px)}h1{font-size:30px;margin:0 0 6px}p{color:#b7cbe8}input{width:100%;box-sizing:border-box;font-size:28px;letter-spacing:8px;text-align:center;
padding:12px;border-radius:12px;border:2px solid #194c91;background:#12356b;color:#fff;margin:12px 0}button{width:100%;padding:14px;border:0;border-radius:12px;
background:#ffde59;color:#071632;font-size:18px;font-weight:700}.note{color:#ffde59}</style>"""
/** A phone that joined before, but arrived without its cookie, puts it back from the token it kept, once per visit. */
private const val REJOIN = """<script>(function(){try{var t=localStorage.getItem('orbit.phone');if(!t||sessionStorage.getItem('orbit.rejoin'))return;sessionStorage.setItem('orbit.rejoin','1');
fetch('/rejoin',{method:'POST',headers:{'X-Orbit':'1','Content-Type':'text/plain'},body:t}).then(function(r){if(r.ok)location.reload();else localStorage.removeItem('orbit.phone');});}catch(e){}})();</script>"""
fun join(note: String?) = """<!doctype html><html><head><title>Orbit</title>$STYLE</head><body><main><h1>Orbit</h1>
<p>Enter the code shown on the TV in Settings › Rooms and phones › Connect a phone.</p>${note?.let { "<p class='note'>${esc(it)}</p>" }.orEmpty()}
<form method="post" action="/join"><input name="code" inputmode="numeric" maxlength="4" autocomplete="one-time-code" autofocus><button>Join</button></form></main></body></html>"""
<form method="post" action="/join"><input name="code" inputmode="numeric" maxlength="4" autocomplete="one-time-code" autofocus><button>Join</button></form></main>$REJOIN</body></html>"""
fun pin(k: String?, code: String?, wrong: Boolean) = """<!doctype html><html><head><title>Orbit</title>$STYLE</head><body><main><h1>Adult PIN</h1>
<p>The code worked. Joining a phone is for adults, so now enter your <b>Adult PIN</b>: the four digits that unlock After Dark on the TV.</p>
<p>It isn't the code on the TV screen.</p>${if (wrong) "<p class='note'>That PIN didn't match. It's the After Dark PIN, not the TV's code.</p>" else ""}
@@ -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.110.1")
next.setRequestProperty("User-Agent", "OrbitTV/0.110.2")
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()) {