From bb5988b06fda9d5e75c8e5f3c8eb5247ff79e52a Mon Sep 17 00:00:00 2001 From: Seb3thehacker Date: Sun, 29 Nov 2026 20:47:53 -0800 Subject: [PATCH] Updates come only from Gearslip's GitHub releases, must match GitHub's checksum, and must be signed with the same key as the installed app --- .../gearslip/update/UpdateChecker.kt | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/update/UpdateChecker.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/update/UpdateChecker.kt index 53ed80e..6adb3e2 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/update/UpdateChecker.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/update/UpdateChecker.kt @@ -95,6 +95,8 @@ object UpdateChecker { private const val DAY_MS = 24 * 60 * 60 * 1000L private const val LATEST = "https://api.github.com/repos/twinksoftware/gearslip/releases/latest" /** Newest first, pre-releases included. Ten covers any run of betas between two releases. */ + /** Where GitHub serves a release's files from, lowercased. */ + private const val RELEASES_PATH = "/twinksoftware/gearslip/releases/download/" private const val ALL = "https://api.github.com/repos/twinksoftware/gearslip/releases?per_page=10" /** Set when the driver tapped Install but first had to allow installs from Gearslip. */ @@ -331,6 +333,7 @@ object UpdateChecker { val apk = File(dir, "Gearslip-${release.version}.apk") flow.value = runCatching { fetchApk(release, apk) + checkSigner(app, apk) GearslipLog.i("update: downloaded ${release.version}") State.Ready(release, apk) }.getOrElse { @@ -510,7 +513,17 @@ object UpdateChecker { ) } + /** + * Only Gearslip's own releases: the address has to be on GitHub, under twinksoftware, over + * HTTPS, and GitHub has to say what the file's SHA-256 is, so a damaged or swapped file never + * reaches the installer. + */ private fun fetchApk(release: Release, into: File) { + val url = runCatching { java.net.URI(release.apkUrl) }.getOrNull() + if (url?.scheme != "https" || url.host != "github.com" || !url.path.lowercase().startsWith(RELEASES_PATH)) { + error("That update isn't from Gearslip's releases, so it wasn't downloaded.") + } + if (release.sha256 == null) error("GitHub didn't say what the update should contain. Try again later.") val conn = (URL(release.apkUrl).openConnection() as HttpURLConnection).apply { connectTimeout = 15_000 readTimeout = 30_000 @@ -542,7 +555,7 @@ object UpdateChecker { } if (release.size > 0 && done != release.size) error("The download stopped early. Try again.") val hex = digest.digest().joinToString("") { "%02x".format(it) } - if (release.sha256 != null && !hex.equals(release.sha256, ignoreCase = true)) { + if (!hex.equals(release.sha256, ignoreCase = true)) { error("The download came out damaged. Try again.") } } finally { @@ -550,6 +563,29 @@ object UpdateChecker { } } + /** + * The update must be signed with the same key as the Gearslip that's installed. Android + * refuses a different key anyway, but only after the driver has been through its screens; + * this stops it first, with a plain reason. + */ + private fun checkSigner(app: Context, apk: File) { + val pm = app.packageManager + val flags = android.content.pm.PackageManager.GET_SIGNING_CERTIFICATES + val candidate = pm.getPackageArchiveInfo(apk.path, flags)?.signingInfo + ?: error("The update couldn't be read. Try again.") + val mine = pm.getPackageInfo(app.packageName, flags).signingInfo + ?: error("Gearslip couldn't read its own signature.") + fun keys(info: android.content.pm.SigningInfo) = + (if (info.hasMultipleSigners()) info.apkContentsSigners else info.signingCertificateHistory) + .map { it.toByteArray() } + val ours = keys(mine) + val theirs = keys(candidate) + if (theirs.none { t -> ours.any { it.contentEquals(t) } }) { + apk.delete() + error("The update isn't signed by Gearslip, so it wasn't installed.") + } + } + /** Installing waits until the car is unplugged. */ val carConnected: Boolean get() = SessionStatus.state.value.phase.let { it == SessionStatus.Phase.CONNECTING || it == SessionStatus.Phase.PROJECTING }