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

This commit is contained in:
Seb3thehacker committed 2026-11-29 20:47:53 -08:00
1 parent 8593e59b30
commit bb5988b06f
1 file changed
+37 -1
@@ -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 }