fix(security): require sha256 for every catalog and release download

This commit is contained in:
xXJsonDeruloXx committed 2026-09-27 10:54:05 -04:00
1 parent 7242e3b21a
commit 61b7b5486f
3 files changed
+27 -11

No files matched your search

@@ -2,6 +2,7 @@ package com.droiddeck.launcher.gpu
import android.content.Context
import com.droiddeck.launcher.core.FileUtils
import com.droiddeck.launcher.runtime.LinuxRuntimeInstaller
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
@@ -27,7 +28,9 @@ object TurnipReleases {
private const val KEY_DOWNLOADS = "downloads"
/** [label] names the variant for the menu: the GPUs it is for, and a build flavour. */
class Asset(val source: String, val tag: String, val name: String, val url: String, val size: Long, val linux: Boolean, val label: String)
/** [sha256] is the release asset's digest from GitHub; empty for a list cached before it was kept. */
class Asset(val source: String, val tag: String, val name: String, val url: String, val size: Long, val linux: Boolean, val label: String,
val sha256: String = "")
/** [latest] = each source's newest release tag, for the refresh line. */
class Check(val assets: List<Asset>, val latest: List<Pair<String, String>>, val failed: List<String>, val checkedAt: Long)
@@ -89,11 +92,13 @@ object TurnipReleases {
val a = list.getJSONObject(j)
val name = a.getString("name")
val (linux, label) = src.classify(name, tag) ?: continue
// Only assets GitHub has a sha256 for are offered: the download is checked against it.
val sha = sha256Of(a) ?: continue
// Newest first: the first release carrying a variant is the one offered.
if (!seen.add("$linux|$label")) continue
assets.put(JSONObject().put("source", src.label).put("tag", tag).put("name", name)
.put("url", a.getString("browser_download_url")).put("size", a.optLong("size"))
.put("linux", linux).put("label", label))
.put("linux", linux).put("label", label).put("sha256", sha))
}
}
if (newest != null) latest.put(JSONObject().put("source", src.label).put("tag", newest))
@@ -109,6 +114,12 @@ object TurnipReleases {
return parse(stored)
}
/** The asset's sha256 from GitHub's "digest" field ("sha256:<hex>"), or null when it has none. */
private fun sha256Of(asset: JSONObject): String? =
asset.optString("digest").takeIf { it.matches(SHA256_DIGEST) }?.substringAfter(':')
private val SHA256_DIGEST = Regex("(?i)sha256:[0-9a-f]{64}")
private fun get(url: String): String {
val c = URL(url).openConnection() as HttpURLConnection
c.connectTimeout = 15_000
@@ -131,7 +142,7 @@ object TurnipReleases {
for (i in 0 until list.length()) {
val a = list.getJSONObject(i)
out.add(Asset(a.getString("source"), a.getString("tag"), a.getString("name"), a.getString("url"),
a.optLong("size"), a.getBoolean("linux"), a.getString("label")))
a.optLong("size"), a.getBoolean("linux"), a.getString("label"), a.optString("sha256", "")))
}
val latest = ArrayList<Pair<String, String>>()
val l = json.optJSONArray("latest") ?: JSONArray()
@@ -167,6 +178,7 @@ object TurnipReleases {
/** Download an asset into the cache; the caller imports it and deletes the file. */
fun download(context: Context, asset: Asset, progress: (Int) -> Unit): File {
if (asset.sha256.isEmpty()) throw IOException("This driver list predates checksums - refresh it and try again")
val target = File(context.cacheDir, asset.name)
val c = URL(asset.url).openConnection() as HttpURLConnection
c.connectTimeout = 15_000
@@ -191,6 +203,9 @@ object TurnipReleases {
}
}
}
if (!asset.sha256.equals(LinuxRuntimeInstaller.sha256(target), ignoreCase = true)) {
throw IOException("Checksum mismatch - the download was discarded")
}
return target
} catch (e: Exception) {
FileUtils.delete(target)
@@ -81,6 +81,8 @@ object DesktopCatalog {
fun install(context: Context, entry: Entry, listener: LinuxRuntimeInstaller.ProgressListener?): String? {
val root = LinuxRuntime.rootDir(context)
if (!root.isDirectory) return "The Linux runtime is not installed"
// Every catalog row carries a sha256; one without is refused rather than trusted.
if (entry.sha256.isEmpty()) return "The catalog has no checksum for ${entry.name}"
val download = File(context.cacheDir, "pkg-${entry.id}.download")
try {
listener?.onProgress("Downloading ${entry.name}", 0)
@@ -88,11 +90,9 @@ object DesktopCatalog {
listener?.onProgress("Downloading ${entry.name}", if (f < 0) -1 else Math.round(f * 100f))
}
if (!ok) return "Download failed"
if (entry.sha256.isNotEmpty()) {
listener?.onProgress("Verifying", -1)
val actual = LinuxRuntimeInstaller.sha256(download)
if (!entry.sha256.equals(actual, ignoreCase = true)) return "Checksum mismatch - nothing was changed"
}
listener?.onProgress("Verifying", -1)
val actual = LinuxRuntimeInstaller.sha256(download)
if (!entry.sha256.equals(actual, ignoreCase = true)) return "Checksum mismatch - nothing was changed"
listener?.onProgress("Installing ${entry.name}", -1)
when (entry.kind) {
"appimage" -> {
@@ -629,6 +629,8 @@ object ComponentsManager {
val a = assets.getJSONObject(i)
val name = a.optString("name")
if (!name.endsWith(".wcp")) continue
// Only packages GitHub has a sha256 for are offered: the download is checked against it.
if (!a.optString("digest").matches(Regex("(?i)sha256:[0-9a-f]{64}"))) continue
items.put(JSONObject().put("file", name).put("comp", comp).put("release", tag)
.put("url", a.optString("browser_download_url")).put("size", a.optLong("size")).put("digest", a.optString("digest")))
}
@@ -650,13 +652,12 @@ object ComponentsManager {
/** Downloads a catalog item into storage, verifying the release's sha256 and the package type. */
fun download(context: Context, item: CatalogItem, progress: (Int) -> Unit): Package {
require(item.url.startsWith("https://github.com/$NIGHTLIES/releases/download/")) { "Downloads come only from the Nightlies releases" }
require(item.digest.matches(Regex("(?i)sha256:[0-9a-f]{64}"))) { "This package list predates checksums - refresh it and try again" }
val dest = File(packagesDir(context), safeName(item.file))
val part = File(dest.parentFile, dest.name + ".part")
part.delete()
check(Downloader.downloadFile(item.url, part, false) { f -> progress(if (f < 0) -1 else (f * 100).toInt().coerceIn(0, 100)) }) { "Download failed" }
if (item.digest.startsWith("sha256:")) {
if (!sha256(part).equals(item.digest.substringAfter(':'), true)) { part.delete(); error("Checksum mismatch") }
}
if (!sha256(part).equals(item.digest.substringAfter(':'), true)) { part.delete(); error("Checksum mismatch") }
val info = runCatching { packageInfo(part) }.getOrElse { part.delete(); throw it }
synchronized(lock) { part.renameTo(dest) }
return info.copy(file = dest.name)