mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
Restrict Xbox credential IPC and address launch review findings (#1164)
* Restrict Xbox credentials to host backends and lease game launches * Verify mapped PRoot backend images and retry lease heartbeat timeouts * Bound Xbox backend shutdown and isolate service socket lifetimes * Keep unique Xbox service sockets within Unix address limits * Own licensed launch directories and recover abandoned views safely * Serialize Xbox account revocation with sign-in commits and launch authorization
This commit is contained in:
1 parent
a63f3b1fbc
commit
7eae6ca3e7
27 files changed
+834
-85
No files matched your search
@@ -0,0 +1,60 @@
|
||||
package com.droiddeck.launcher.stores.xbox
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
@org.junit.runner.RunWith(org.robolectric.RobolectricTestRunner::class)
|
||||
@org.robolectric.annotation.Config(sdk = [33], application = android.app.Application::class)
|
||||
class XboxAccountLifecycleTest {
|
||||
private val app = ApplicationProvider.getApplicationContext<android.content.Context>()
|
||||
|
||||
@Test fun staleSignInCommitsAndBackendStartsCannotAcquireANewerAccount() {
|
||||
val original = XboxCredentials.lifecycle(app) { it }
|
||||
XboxCredentials.invalidate(app)
|
||||
var committed = false
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
XboxCredentials.lifecycle(app, original) { committed = true }
|
||||
}
|
||||
assertFalse(committed)
|
||||
assertThrows(IllegalStateException::class.java) { XboxCredentials(app, original) }
|
||||
XboxCredentials.lifecycle(app) { current -> assertNotEquals(original, current) }
|
||||
}
|
||||
|
||||
@Test fun accountCommitAndLaunchAuthorizationWaitUntilRevocationFinishes() {
|
||||
val revoked = CountDownLatch(1)
|
||||
val releaseStop = CountDownLatch(1)
|
||||
val attempted = CountDownLatch(1)
|
||||
val committed = AtomicBoolean(false)
|
||||
val authorized = CountDownLatch(1)
|
||||
val signOut = Thread {
|
||||
XboxCredentials.invalidate(app) {
|
||||
revoked.countDown()
|
||||
check(releaseStop.await(5, TimeUnit.SECONDS))
|
||||
}
|
||||
}.apply { start() }
|
||||
assertTrue(revoked.await(5, TimeUnit.SECONDS))
|
||||
val signIn = Thread {
|
||||
attempted.countDown()
|
||||
XboxCredentials.lifecycle(app) {
|
||||
committed.set(true)
|
||||
authorized.countDown()
|
||||
}
|
||||
}.apply { start() }
|
||||
try {
|
||||
assertTrue(attempted.await(5, TimeUnit.SECONDS))
|
||||
assertFalse(authorized.await(100, TimeUnit.MILLISECONDS))
|
||||
assertFalse(committed.get())
|
||||
} finally {
|
||||
releaseStop.countDown()
|
||||
signOut.join(5000)
|
||||
signIn.join(5000)
|
||||
}
|
||||
assertTrue(committed.get())
|
||||
assertFalse(signOut.isAlive)
|
||||
assertFalse(signIn.isAlive)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.droiddeck.launcher.stores.xbox
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class XboxBackendMappingTest {
|
||||
private val path = "/runtime/xodus-cli"
|
||||
private fun mapping(permissions: String = "r-xp", inode: Long = 42, name: String = path) =
|
||||
"7a0000-7b0000 $permissions 00001000 00:31 $inode $name\n"
|
||||
|
||||
@Test fun prootLoaderDoesNotHideTheVerifiedBackendImage() {
|
||||
val maps = mapping(name = "/android/libproot-loader.so", inode = 1) + mapping()
|
||||
assertTrue(XboxCredentials.mapsBackend(maps, path, 42))
|
||||
}
|
||||
@Test fun loaderAloneOrAnotherGuestExecutableCannotAuthenticate() {
|
||||
assertFalse(XboxCredentials.mapsBackend(mapping(name = "/android/libproot-loader.so"), path, 42))
|
||||
assertFalse(XboxCredentials.mapsBackend(mapping(name = "/runtime/other-cli"), path, 42))
|
||||
assertFalse(XboxCredentials.mapsBackend(mapping(inode = 43), path, 42))
|
||||
assertFalse(XboxCredentials.mapsBackend(mapping(permissions = "r--p"), path, 42))
|
||||
assertFalse(XboxCredentials.mapsBackend(mapping(name = "$path (deleted)"), path, 42))
|
||||
assertFalse(XboxCredentials.mapsBackend("malformed\n", path, 42))
|
||||
}
|
||||
}
|
||||
@@ -21,10 +21,19 @@ class XboxCatalogTest {
|
||||
private fun access(vararg rows: JSONObject) = XboxCatalog.access(envelope(*rows), "test@example.test", "US", now)
|
||||
|
||||
@Test fun purchasedAndSubscriptionAccessRemainDistinct() {
|
||||
assertEquals(mapOf(game to "owned", parent to "gamepass"), access(row(game), row(parent, plan)))
|
||||
assertEquals(mapOf(game to "owned", parent to "gamepass"), access(row(game), row(parent, plan), row(plan)))
|
||||
assertFalse(access(row(plan)).containsKey(plan))
|
||||
}
|
||||
|
||||
@Test fun subscriptionRequiresAnActivePlanRow() {
|
||||
assertTrue(access(row(game, plan)).isEmpty())
|
||||
for ((key, value) in listOf("status" to "Expired", "isTrial" to true, "special" to true,
|
||||
"sharingSource" to "Family", "endDate" to now.toString())) {
|
||||
assertTrue(access(row(game, plan), row(plan).put(key, value)).isEmpty())
|
||||
}
|
||||
assertEquals(mapOf(game to "gamepass", parent to "gamepass"), access(row(game, parent), row(parent, plan), row(plan)))
|
||||
}
|
||||
|
||||
@Test fun backendRfc3339OffsetsAndNanosecondsAreAccepted() {
|
||||
val checked = "2026-10-19T19:02:38.173609086+00:00"
|
||||
val response = envelope(row(game).put("endDate", "2099-01-01T01:00:00.000000001+01:00"))
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.droiddeck.launcher.stores.xbox
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.io.Closeable
|
||||
|
||||
class XboxLaunchLeasesTest {
|
||||
private var time = 0L
|
||||
private val leases = XboxLaunchLeases { time }
|
||||
private var closed = 0
|
||||
private fun launch(account: String = "account", session: String = "session"): String {
|
||||
leases.authorize("game", account)
|
||||
return leases.begin("game", account, session).also { leases.attach(it, Closeable { closed++ }) }
|
||||
}
|
||||
private fun reap(account: String? = "account", session: String? = "session", suspended: Boolean = false) {
|
||||
leases.stale(account, session, suspended).forEach { it.close() }
|
||||
}
|
||||
|
||||
@Test fun guestPreparationRequiresAnUnexpiredAccountPermit() {
|
||||
assertThrows(IllegalStateException::class.java) { leases.begin("game", "account", "session") }
|
||||
leases.authorize("game", "account")
|
||||
assertThrows(IllegalStateException::class.java) { leases.begin("game", "other", "session") }
|
||||
leases.authorize("game", "account")
|
||||
time = 180_000
|
||||
assertThrows(IllegalStateException::class.java) { leases.begin("game", "account", "session") }
|
||||
}
|
||||
@Test fun releaseIsIdempotentAndCannotDecrementANewLaunch() {
|
||||
val old = launch()
|
||||
leases.release(old)?.close()
|
||||
leases.release(old)?.close()
|
||||
assertEquals(1, closed)
|
||||
assertFalse(leases.active("game"))
|
||||
val current = launch()
|
||||
leases.release(old)?.close()
|
||||
assertTrue(leases.active("game"))
|
||||
leases.release(current)?.close()
|
||||
assertEquals(2, closed)
|
||||
}
|
||||
@Test fun renewalCannotCrossAccountSessionOrGameBoundaries() {
|
||||
val token = launch()
|
||||
assertFalse(leases.renew(token, "other", "account", "session"))
|
||||
assertFalse(leases.renew(token, "game", "other", "session"))
|
||||
assertFalse(leases.renew(token, "game", "account", "other"))
|
||||
assertTrue(leases.renew(token, "game", "account", "session"))
|
||||
time = 60_000
|
||||
assertFalse(leases.renew(token, "game", "account", "session"))
|
||||
reap()
|
||||
assertEquals(1, closed)
|
||||
assertFalse(leases.active("game"))
|
||||
}
|
||||
@Test fun abandonedPreparationAndSessionEndCleanUp() {
|
||||
launch()
|
||||
time = 180_000
|
||||
reap()
|
||||
assertEquals(1, closed)
|
||||
launch()
|
||||
reap(session = null)
|
||||
assertEquals(2, closed)
|
||||
}
|
||||
@Test fun accountSwitchInvalidatesOldLaunchWithoutAffectingItsReplacement() {
|
||||
val old = launch()
|
||||
reap(account = "next")
|
||||
launch("next")
|
||||
leases.release(old)?.close()
|
||||
assertEquals(1, closed)
|
||||
assertTrue(leases.active("game"))
|
||||
}
|
||||
@Test fun lateBackendCannotAttachAfterStop() {
|
||||
leases.authorize("game", "account")
|
||||
val token = leases.begin("game", "account", "session")
|
||||
leases.clear().forEach { it.close() }
|
||||
assertFalse(leases.attach(token, Closeable { closed++ }))
|
||||
assertFalse(leases.active("game"))
|
||||
assertThrows(IllegalStateException::class.java) { leases.begin("game", "account", "session") }
|
||||
}
|
||||
@Test fun suspensionKeepsLiveLeasesUntilResume() {
|
||||
val token = launch()
|
||||
time = 200_000
|
||||
reap(suspended = true)
|
||||
time += 20_000
|
||||
assertTrue(leases.renew(token, "game", "account", "session"))
|
||||
reap()
|
||||
assertEquals(0, closed)
|
||||
}
|
||||
@Test fun activityQueriesDoNotWaitForBackendClose() {
|
||||
val token = launch()
|
||||
val detached = leases.release(token)
|
||||
val gate = java.util.concurrent.CountDownLatch(1)
|
||||
val worker = Thread { gate.await(); detached?.close() }.apply { start() }
|
||||
try { assertFalse(leases.active("game")); launch("next") } finally { gate.countDown(); worker.join(1000) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.droiddeck.launcher.stores.xbox
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.nio.file.Files
|
||||
|
||||
class XboxLaunchViewsTest {
|
||||
@Test fun cleanupRemovesLicensedCopiesWithoutFollowingGameLinks() {
|
||||
val root = Files.createTempDirectory("xbox-view-test").toFile()
|
||||
try {
|
||||
val game = root.resolve("installed").apply { mkdirs() }
|
||||
game.resolve("Game.exe").writeText("encrypted original")
|
||||
val view = XboxLaunchViews().create(root)
|
||||
view.resolve("Game.exe").writeText("licensed copy")
|
||||
Files.createSymbolicLink(view.resolve("originals").toPath(), game.toPath())
|
||||
XboxLaunchViews.remove(view)
|
||||
XboxLaunchViews.remove(view)
|
||||
assertFalse(view.exists())
|
||||
assertEquals("encrypted original", game.resolve("Game.exe").readText())
|
||||
} finally { root.deleteRecursively() }
|
||||
}
|
||||
|
||||
@Test fun recoveryRejectsARedirectedStorageRoot() {
|
||||
val root = Files.createTempDirectory("xbox-view-links").toFile()
|
||||
try {
|
||||
val outside = root.resolve("installed").apply { mkdirs() }
|
||||
val game = outside.resolve("launch-game").apply { mkdirs() }.resolve("Game.exe")
|
||||
game.writeText("preserve")
|
||||
val runtime = root.resolve("xbox-runtime").apply { mkdirs() }
|
||||
Files.createSymbolicLink(runtime.resolve("views").toPath(), outside.toPath())
|
||||
assertThrows(IllegalStateException::class.java) { XboxLaunchViews().create(root) }
|
||||
assertEquals("preserve", game.readText())
|
||||
} finally { root.deleteRecursively() }
|
||||
}
|
||||
|
||||
@Test fun recoveryRemovesAbandonedViewsAndPreservesNewLiveViews() {
|
||||
val root = Files.createTempDirectory("xbox-view-recovery").toFile()
|
||||
try {
|
||||
val old = XboxLaunchViews().create(root)
|
||||
old.resolve("Game.exe").writeText("abandoned copy")
|
||||
val evidence = old.parentFile!!.resolve("diagnostics").apply { mkdirs() }.resolve("report")
|
||||
evidence.writeText("preserve")
|
||||
val fresh = XboxLaunchViews()
|
||||
val first = fresh.create(root)
|
||||
first.resolve("Game.exe").writeText("running copy")
|
||||
val second = fresh.create(root)
|
||||
assertFalse(old.exists())
|
||||
assertEquals("running copy", first.resolve("Game.exe").readText())
|
||||
assertTrue(second.isDirectory)
|
||||
assertEquals("preserve", evidence.readText())
|
||||
} finally { root.deleteRecursively() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.droiddeck.launcher.stores.xbox
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
class XboxServiceSocketTest {
|
||||
@Test fun serviceSocketsFitUnixAddressesForEveryPackageAndRemainDistinct() {
|
||||
val variants = generateSequence(File(requireNotNull(System.getProperty("user.dir")))) { it.parentFile }
|
||||
.map { File(it, "tools/release/variants.txt") }.first { it.isFile }
|
||||
val packages = variants.readLines().filter { it.isNotBlank() && !it.startsWith('#') }
|
||||
.map { it.trim().split(Regex("\\s+"))[1] }
|
||||
for (pkg in packages) for (user in listOf(0, 99999)) {
|
||||
val files = File("/data/user/$user/$pkg/files")
|
||||
val first = XboxRuntime.newServiceSocket(files)
|
||||
val second = XboxRuntime.newServiceSocket(files)
|
||||
assertTrue(first.path, first.path.toByteArray(Charsets.UTF_8).size < 108)
|
||||
assertNotEquals(first, second)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user