fix: refuse overwriting in-use component packages and clear unpacked store on replace (#1144)

* fix: refuse overwriting in-use component packages and clear unpacked store on replace

* fix: preserve component store until replacement succeeds

* fix: invalidate completed component cache before replace

* fix: constrain unpacked component cache path

* Test rejected package cache dot segments

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-25 17:50:03 -04:00
1 parent b4b7883648
commit e76489ccea
3 files changed
+197 -2

No files matched your search

@@ -780,6 +780,27 @@ object ComponentsManager {
true
}
private fun checkPackageUnused(context: Context, state: JSONObject, name: String) {
val installed = protons(context).distinctBy { it.id }.associateBy { it.id }
for (key in listOf("queued", "active")) {
val byProton = state.sub(key)
for (pid in byProton.keys()) {
val comps = byProton.optJSONObject(pid) ?: continue
for (comp in comps.keys()) {
val entry = comps.optJSONObject(comp) ?: continue
check(entry.optString("file") != name || !liveSelection(key, entry, installed[pid])) {
context.getString(R.string.cmgr_package_in_use, name, installed[pid]?.name ?: pid)
}
}
}
}
val profiles = state.sub("profiles")
for (scope in profiles.keys()) {
val profile = profiles.optJSONObject(scope) ?: continue
check(COMPONENTS.none { profile.optString(it) == name }) { context.getString(R.string.cmgr_package_in_use, name, scope) }
}
}
fun carryOver(context: Context, fromDir: String, toDir: String) {
synchronized(lock) {
val target = protons(context).firstOrNull { it.id == safeName(toDir) } ?: return
@@ -828,6 +849,33 @@ object ComponentsManager {
}
}
private fun storePackage(context: Context, part: File, dest: File) {
try {
if (dest.isFile && runCatching { Hashes.sha256(dest) }.getOrNull()?.equals(Hashes.sha256(part), true) == true) return
if (dest.exists()) checkPackageUnused(context, loadState(context), dest.name)
val store = File(launchDir(context), "store").canonicalFile
val cacheKey = unpackedCacheKey(dest.name)
val unpacked = File(store, cacheKey).canonicalFile
check(unpacked.parentFile == store) { "Invalid unpacked component cache path for ${dest.name}" }
val complete = File(unpacked, ".complete")
if (complete.exists()) check(complete.delete()) { "Could not invalidate unpacked component ${dest.name}" }
if (!part.renameTo(dest)) {
dest.delete()
check(part.renameTo(dest)) { "Could not store ${dest.name}" }
}
if (!unpacked.deleteRecursively() && unpacked.exists()) {
check(!complete.exists()) { "Could not invalidate unpacked component ${dest.name}" }
Log.w(TAG, "Could not remove stale unpacked files for ${dest.name}; cache marker is invalidated")
}
} finally {
part.delete()
}
}
internal fun unpackedCacheKey(filename: String): String = safeName(filename.removeSuffix(".wcp")).also {
require(it != "." && it != "..") { "Invalid component cache name" }
}
fun deletePackage(context: Context, file: String): String = synchronized(lock) {
val name = safeName(file)
val state = loadState(context)
@@ -877,7 +925,7 @@ object ComponentsManager {
fun importPackage(context: Context, source: File, name: String): Package = synchronized(lock) {
val info = packageInfo(context, source)
val dest = File(packagesDir(context), safeName(name).let { if (it.endsWith(".wcp")) it else "$it.wcp" })
source.copyTo(File(dest.parentFile, dest.name + ".part"), overwrite = true).renameTo(dest)
storePackage(context, source.copyTo(File(dest.parentFile, dest.name + ".part"), overwrite = true), dest)
info.copy(file = dest.name)
}
@@ -1030,7 +1078,7 @@ object ComponentsManager {
}
if (!Hashes.sha256(part).equals(item.digest.substringAfter(':'), true)) { part.delete(); error(context.getString(R.string.cmgr_checksum_mismatch)) }
val info = runCatching { packageInfo(context, part) }.getOrElse { part.delete(); throw it }
synchronized(lock) { part.renameTo(dest) }
synchronized(lock) { storePackage(context, part, dest) }
return info.copy(file = dest.name)
}
}
@@ -0,0 +1,135 @@
package com.droiddeck.launcher.session
import android.content.Context
import com.droiddeck.launcher.core.Hashes
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.github.luben.zstd.ZstdOutputStream
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class ComponentImportTest {
private lateinit var context: Context
@Before fun setUp() {
context = RuntimeEnvironment.getApplication()
File(context.filesDir, "components").deleteRecursively()
LinuxRuntime.rootDir(context).deleteRecursively()
}
@Test fun refusesOverwritingReferencedPackageWithDifferentContent() {
val wcpA = createWcp("src-a.wcp", "A")
val wcpB = createWcp("src-b.wcp", "B")
val shaA = Hashes.sha256(wcpA)
ComponentsManager.importPackage(context, wcpA, "dxvk.wcp")
ComponentsManager.setGameComponent(context, "10", "dxvk", "dxvk.wcp")
assertThrows(IllegalStateException::class.java) {
ComponentsManager.importPackage(context, wcpB, "dxvk.wcp")
}
val stored = File(context.filesDir, "components/packages/dxvk.wcp")
assertEquals(shaA, Hashes.sha256(stored))
assertFalse(File(context.filesDir, "components/packages/dxvk.wcp.part").exists())
}
@Test fun allowsReimportingIdenticalPackageWhileReferenced() {
val wcpA = createWcp("src-a.wcp", "A")
val shaA = Hashes.sha256(wcpA)
ComponentsManager.importPackage(context, wcpA, "dxvk.wcp")
ComponentsManager.setGameComponent(context, "10", "dxvk", "dxvk.wcp")
val reimported = ComponentsManager.importPackage(context, wcpA, "dxvk.wcp")
assertEquals("A", reimported.version)
assertEquals("dxvk.wcp", reimported.file)
val stored = File(context.filesDir, "components/packages/dxvk.wcp")
assertEquals(shaA, Hashes.sha256(stored))
assertFalse(File(context.filesDir, "components/packages/dxvk.wcp.part").exists())
}
@Test fun invalidatesUnpackedStoreWhenReplacingUnreferencedPackage() {
val wcpA = createWcp("src-a.wcp", "A")
val wcpB = createWcp("src-b.wcp", "B")
val shaB = Hashes.sha256(wcpB)
ComponentsManager.importPackage(context, wcpA, "dxvk.wcp")
val unpacked = File(LinuxRuntime.rootDir(context), "root/.local/share/droiddeck-components/store/dxvk")
unpacked.mkdirs()
File(unpacked, ".complete").writeText("ok")
assertTrue(File(unpacked, ".complete").isFile)
val replaced = ComponentsManager.importPackage(context, wcpB, "dxvk.wcp")
assertEquals("B", replaced.version)
val stored = File(context.filesDir, "components/packages/dxvk.wcp")
assertEquals(shaB, Hashes.sha256(stored))
assertFalse(unpacked.exists())
}
@Test fun deletingAPackageAGameProfileUsesClearsThatProfileEntry() {
val wcpA = createWcp("src-a.wcp", "A")
ComponentsManager.importPackage(context, wcpA, "dxvk.wcp")
writeGameProfile("10", "dxvk", "dxvk.wcp")
ComponentsManager.deletePackage(context, "dxvk.wcp")
assertFalse(File(context.filesDir, "components/packages/dxvk.wcp").exists())
val state = JSONObject(File(context.filesDir, "components/state.json").readText())
assertFalse(state.optJSONObject("profiles")?.optJSONObject("10")?.has("dxvk") == true)
}
private fun writeGameProfile(scope: String, comp: String, file: String) {
val state = File(context.filesDir, "components/state.json")
state.parentFile!!.mkdirs()
state.writeText(
JSONObject()
.put("active", JSONObject())
.put("queued", JSONObject())
.put("profiles", JSONObject().put(scope, JSONObject().put(comp, file)))
.toString(),
)
}
private fun createWcp(fileName: String, version: String): File {
val out = File(context.cacheDir, fileName)
val profile = JSONObject()
.put("type", "DXVK-Linux")
.put("versionName", version)
.put("description", "DXVK $version")
.toString()
.toByteArray(Charsets.UTF_8)
val dll = "dxvk-dll-$version".toByteArray(Charsets.UTF_8)
TarArchiveOutputStream(ZstdOutputStream(BufferedOutputStream(FileOutputStream(out)))).use { tar ->
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX)
val profEntry = TarArchiveEntry("profile.json").apply { size = profile.size.toLong() }
tar.putArchiveEntry(profEntry)
tar.write(profile)
tar.closeArchiveEntry()
val dllEntry = TarArchiveEntry("files/lib/wine/dxvk/aarch64-windows/d3d11.dll").apply { size = dll.size.toLong() }
tar.putArchiveEntry(dllEntry)
tar.write(dll)
tar.closeArchiveEntry()
}
return out
}
}
@@ -29,6 +29,18 @@ class ForcedPackageTest {
LinuxRuntime.rootDir(context).deleteRecursively()
}
@Test fun unpackedCacheNameCannotEscapeStoreDirectory() {
assertEquals("dxvk-2.7.1-linux", ComponentsManager.unpackedCacheKey(dxvk2))
for (filename in listOf("..wcp", "...wcp")) {
try {
ComponentsManager.unpackedCacheKey(filename)
fail("Expected cache traversal name to be rejected: $filename")
} catch (_: IllegalArgumentException) {
// Expected: the derived path would be a dot segment.
}
}
}
@Test fun adreno6xxForcesDxvk2AndNothingElseDoes() {
val rule = ComponentsManager.FORCED_PACKAGES.single { GpuInfo.Family.A6XX in it.families }
assertEquals("dxvk", rule.comp)