Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.siloserver.silo.common.pairing

import android.util.Log
import org.siloserver.silo.common.network.CleartextConsentStore
import org.siloserver.silo.model.auth.DeviceLoginPollResponse
import org.siloserver.silo.model.auth.DeviceLoginStartResponse
import org.siloserver.silo.network.api.ServerIdentityProbe
import org.siloserver.silo.network.canonicalHttpOrigin
import org.siloserver.silo.network.requiresApproval
import org.siloserver.silo.pairing.PairingEndpoint
import org.siloserver.silo.pairing.PairingFailureCode
import org.siloserver.silo.pairing.PairingMessage
Expand Down Expand Up @@ -101,10 +104,13 @@ sealed class PairingReceiverStatus {
/**
* A phone pushed a server and the TV user must allow it before the
* device-login (and its sign-in code) starts — tvOS-parity consent step.
* [unencrypted] = the address is plain HTTP this TV hasn't approved yet:
* the TV user is warned, and allowing approves that exact origin.
*/
data class ConsentRequested(
val serverURL: String,
val serverName: String,
val unencrypted: Boolean = false,
) : PairingReceiverStatus()

/** Checking the server's address and starting device sign-in there (interim). */
Expand Down Expand Up @@ -178,6 +184,12 @@ class PairingReceiver(
private val identityProvider: () -> PairingDeviceIdentity,
/** Classifies one address; production probes `GET /api/v2/system/identity`. */
private val identityProbe: suspend (serverUrl: String) -> ServerIdentityProbe = { ServerIdentityProbe.Unreachable },
/**
* This TV's cleartext approvals. Every credential-bearing request to a
* plain-HTTP origin needs one, so a pushed HTTP server is approved only
* through the consent step, with the TV user warned.
*/
private val cleartextConsent: CleartextConsentStore,
) {
private companion object {
private const val TAG = "PairingReceiver"
Expand Down Expand Up @@ -251,10 +263,10 @@ class PairingReceiver(
val push = pendingPush ?: return
val transport = activeTransport ?: return
val scope = sessionScope ?: return
if (_status.value !is PairingReceiverStatus.ConsentRequested) return
val consent = _status.value as? PairingReceiverStatus.ConsentRequested ?: return
consented = true
pendingPush = null
beginAttempt(push, transport, scope)
beginAttempt(push, transport, scope, approveCleartext = consent.unencrypted)
}

/**
Expand Down Expand Up @@ -414,10 +426,17 @@ class PairingReceiver(
pushJob = null
// tvOS parity: the first push of a session needs the TV user's consent
// before device-login starts (and before any sign-in code shows). A
// newer push while consent is pending supersedes the earlier one.
if (!consented) {
// newer push while consent is pending supersedes the earlier one. A
// plain-HTTP server this TV hasn't approved asks again even in a
// consented session: the approval covers that one origin.
val unencrypted = cleartextConsent.requiresApproval(message.serverURL)
if (!consented || unencrypted) {
pendingPush = message
_status.value = PairingReceiverStatus.ConsentRequested(message.serverURL, message.displayName())
_status.value = PairingReceiverStatus.ConsentRequested(
message.serverURL,
message.displayName(),
unencrypted = unencrypted,
)
return
}
beginAttempt(message, transport, sessionScope)
Expand All @@ -427,12 +446,16 @@ class PairingReceiver(
push: PairingMessage.PushServer,
transport: PairingTransport,
sessionScope: CoroutineScope,
approveCleartext: Boolean = false,
) {
// "Automatic" only once a sign-in has been COMMITTED: the phone
// auto-approves only after its user confirmed the first code.
val automatic = signedInNames.isNotEmpty()
val ad = advertisement
pushJob = sessionScope.launch {
if (approveCleartext) {
canonicalHttpOrigin(push.serverURL)?.let { cleartextConsent.approve(it) }
}
if (ad.state == PairingReceiverState.Login) {
runLoginAttempt(push, transport, ad)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,31 @@ private class FakeDeviceLogin : DeviceLoginPort {
}
}

/** In-memory cleartext approvals, keyed by the exact origin. */
private class FakeCleartextConsent : org.siloserver.silo.common.network.CleartextConsentStore {
val approved = mutableSetOf<String>()

override suspend fun isApproved(origin: String) = origin in approved

override suspend fun approve(origin: String) {
approved += origin
}
}

class PairingReceiverTest {

private fun receiver(
auth: FakeAuthPort,
login: FakeDeviceLogin,
state: PairingReceiverState = PairingReceiverState.Setup,
identities: Map<String, ServerIdentityProbe> = emptyMap(),
cleartext: FakeCleartextConsent = FakeCleartextConsent(),
) = PairingReceiver(
authPort = auth,
deviceLogin = login,
identityProvider = { PairingDeviceIdentity(name = "Test TV", deviceId = "device-1") },
identityProbe = { url -> identities[url] ?: ServerIdentityProbe.Unreachable },
cleartextConsent = cleartext,
).also { it.advertisement = PairingAdvertisement(state) }

@Test
Expand Down Expand Up @@ -357,6 +370,64 @@ class PairingReceiverTest {
job.join()
}

/**
* A plain-HTTP server needs the TV's own cleartext approval before any
* credential-bearing request; the setup consent is where the TV user
* gives it, warned that the connection is unencrypted.
*/
@Test
fun allowingAPlainHttpServerApprovesItsOriginBeforeSigningIn() = runTest {
val transport = FakeTransport()
val login = FakeDeviceLogin()
val cleartext = FakeCleartextConsent()
val recv = receiver(FakeAuthPort(), login, cleartext = cleartext)

val job = launch { recv.run(transport) }
yield()
transport.deliver(PairingMessage.PushServer(serverURL = "http://192.168.1.20:8096", serverName = "Home"))
repeat(5) { yield() }
assertEquals(
PairingReceiverStatus.ConsentRequested("http://192.168.1.20:8096", "Home", unencrypted = true),
recv.status.value,
)
assertEquals(emptySet(), cleartext.approved)

recv.allowPendingServer()
repeat(10) { yield() }
assertEquals(setOf("http://192.168.1.20:8096"), cleartext.approved)
assertEquals("http://192.168.1.20:8096", login.beganWith?.serverUrl)

transport.deliver(PairingMessage.Done)
job.join()
}

@Test
fun aLaterPlainHttpServerAsksAgainInAConsentedSession() = runTest {
val transport = FakeTransport()
val login = FakeDeviceLogin()
val recv = receiver(FakeAuthPort(), login)

val job = launch { recv.run(transport) }
yield()
transport.deliver(PairingMessage.PushServer(serverURL = "https://one.test", serverName = "One"))
repeat(5) { yield() }
recv.allowPendingServer()
repeat(10) { yield() }
login.approve()
repeat(10) { yield() }

transport.deliver(PairingMessage.PushServer(serverURL = "http://192.168.1.20:8096", serverName = "Home"))
repeat(10) { yield() }
assertEquals(
PairingReceiverStatus.ConsentRequested("http://192.168.1.20:8096", "Home", unencrypted = true),
recv.status.value,
)
assertEquals("https://one.test", login.beganWith?.serverUrl)

transport.deliver(PairingMessage.Done)
job.join()
}

private suspend fun kotlinx.coroutines.test.TestScope.settle() = repeat(20) { yield() }

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ val androidTvModule = module {
)
},
identityProbe = { url -> identityApi.probeIdentity(url) },
cleartextConsent = get<CleartextConsentStore>(),
)
}
// Silo servers on the local network, for server setup. One per screen visit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ internal fun ActivePairingPanel(
stringResource(if (signIn) R.string.tv_pairing_consent_title_signin else R.string.tv_pairing_consent_title),
)
PanelDetail(stringResource(R.string.tv_pairing_consent_detail, status.serverName))
if (status.unencrypted) {
PanelDetail(stringResource(R.string.tv_pairing_consent_unencrypted, DeviceCodeFormat.host(status.serverURL)))
}
PanelActions {
TvMarqueeButton(text = stringResource(R.string.tv_pairing_allow), onClick = onAllow, focusRequester = primaryFocus)
TvMarqueeButton(text = stringResource(R.string.tv_pairing_dont_allow), onClick = onDeny, kind = TvMarqueeButtonKind.Plain)
Expand Down
1 change: 1 addition & 0 deletions androidTvApp/src/androidMain/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<string name="tv_pairing_consent_title">Allow this setup?</string>
<string name="tv_pairing_consent_title_signin">Allow this sign-in?</string>
<string name="tv_pairing_consent_detail">A nearby phone or tablet wants to sign this TV in to %1$s.</string>
<string name="tv_pairing_consent_unencrypted">Your password and what you watch will be sent unencrypted to %1$s. Only do this on a network you trust.</string>
<string name="tv_pairing_allow">Allow</string>
<string name="tv_pairing_dont_allow">Don\'t allow</string>
<string name="tv_pairing_cancel">Cancel</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class TvLoginScreenNetworkSignInTest {
serverRegistry = OneServer,
externalSignIn = ExternalSignInRepository(Providers(providers)),
)
val receiver = PairingReceiver(NoPairing, DeviceLoginRepositoryPort(deviceLogin), { PairingDeviceIdentity("TV", "tv-1") })
val receiver = PairingReceiver(
NoPairing,
DeviceLoginRepositoryPort(deviceLogin),
{ PairingDeviceIdentity("TV", "tv-1") },
cleartextConsent = NoCleartextApprovals,
)
composeRule.setContent {
SiloTvTheme {
TvLoginScreen(
Expand Down Expand Up @@ -187,6 +192,12 @@ class TvLoginScreenNetworkSignInTest {
override suspend fun linkWithNetwork(scope: AuthScopeSnapshot, installationId: String, password: String) = error("unused")
}

private object NoCleartextApprovals : org.siloserver.silo.common.network.CleartextConsentStore {
override suspend fun isApproved(origin: String) = false

override suspend fun approve(origin: String) = Unit
}

private object NoPairing : PairingAuthPort {
override suspend fun persistApprovedSession(
serverUrl: String, serverName: String?, accessToken: String, refreshToken: String, expiresIn: Long,
Expand Down
Loading