Complete pinned Play bundle inputs and guest prerequisite delivery

This commit is contained in:
maxjivi05 committed 2026-11-03 00:04:07 -05:00
1 parent 2f6e43e3d1
commit 567ca34a48
16 files changed
+10608 -22

No files matched your search

@@ -12,7 +12,12 @@ import java.security.MessageDigest
import java.util.function.BooleanSupplier
import java.util.function.Consumer
internal class ComponentPayloadStore(private val assets: (String) -> InputStream) {
internal class ComponentPayloadStore @JvmOverloads constructor(
private val download: (String, File, String, BooleanSupplier?, Consumer<Float>?) -> Boolean = { url, file, digest, cancelled, progress ->
Downloader.downloadCandidates(url, listOf(url), file, false, digest, cancelled, progress)
},
private val assets: (String) -> InputStream,
) {
private val entries by lazy {
val index = readObject("play-components/index.json", 2 * 1024 * 1024)
if (index.getInt("schema") != 1) throw IOException("Unsupported component index")
@@ -103,13 +108,42 @@ internal class ComponentPayloadStore(private val assets: (String) -> InputStream
Files.createDirectories(parent.toPath())
val temporary = Files.createTempFile(parent.toPath(), destination.name + ".", ".component-part")
try {
Files.newOutputStream(temporary).use { output ->
copy(url, output, Long.MAX_VALUE, expectedSha256, cancelled, progress)
val entry = entries.optJSONObject(url) ?: throw IOException("Component is absent from this build: $url")
if (entry.optString("delivery") == "windows-guest") {
val digest = entry.getString("sha256")
val size = entry.getLong("size")
val source = java.net.URI(entry.getString("download_url"))
if (!entry.getString("id").startsWith("windows-") || !SHA256.matches(digest) || size <= 0 ||
source.scheme != "https" || source.host.isNullOrEmpty() || source.userInfo != null || source.fragment != null ||
expectedSha256 != null && !digest.equals(expectedSha256, ignoreCase = true)) {
throw IOException("Invalid Windows prerequisite identity")
}
checkCancelled(cancelled)
if (!download(source.toString(), temporary.toFile(), digest, cancelled, progress) || Files.size(temporary) != size) {
throw IOException("Windows prerequisite download failed verification")
}
val actual = Files.newInputStream(temporary).use { input ->
val hash = MessageDigest.getInstance("SHA-256")
val buffer = ByteArray(65536)
while (true) {
checkCancelled(cancelled)
val count = input.read(buffer)
if (count < 0) break
hash.update(buffer, 0, count)
}
hex(hash.digest())
}
if (actual != digest) throw IOException("Windows prerequisite failed verification")
} else {
Files.newOutputStream(temporary).use { output ->
copy(url, output, Long.MAX_VALUE, expectedSha256, cancelled, progress)
}
}
checkCancelled(cancelled)
Files.move(temporary, destination.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING)
} finally {
Files.deleteIfExists(temporary)
Files.deleteIfExists(temporary.resolveSibling(temporary.fileName.toString() + ".resume.properties"))
}
}
@@ -177,8 +177,10 @@ object DesktopCatalog {
installLocked(context, entry, cancelled, listener)
}
private fun installLocked(context: Context, entry: Entry, cancelled: Downloader.Cancellation?,
private fun installLocked(context: Context, requested: Entry, cancelled: Downloader.Cancellation?,
listener: LinuxRuntimeInstaller.ProgressListener?): String? {
val current = if (BuildConfig.PLAY_DISTRIBUTION && requested.id == DESKTOP_ID) desktopEntry(context) else null
val entry = current?.takeIf { it.version == requested.version } ?: requested
if (!offered(entry)) return context.getString(R.string.deskpkg_install_failed)
val root = LinuxRuntime.rootDir(context)
if (!root.isDirectory) return context.getString(R.string.deskpkg_runtime_missing)
@@ -13,6 +13,7 @@ import org.junit.runner.RunWith
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.File
import java.nio.file.Files
import java.security.MessageDigest
import java.util.function.BooleanSupplier
@@ -55,6 +56,47 @@ class ComponentPayloadStoreTest {
assertArrayEquals(content, output.toByteArray())
}
private fun windowsStore(source: String = url, install: (File) -> Unit): ComponentPayloadStore {
val entry = JSONObject().put("id", "windows-vcredist").put("delivery", "windows-guest")
.put("download_url", source).put("size", content.size).put("sha256", digest)
val index = JSONObject().put("schema", 1).put("entries", JSONObject().put(url, entry)).toString().toByteArray()
return ComponentPayloadStore(download = { actual, file, hash, _, _ ->
assertEquals(source, actual)
assertEquals(digest, hash)
install(file)
true
}, assets = { path ->
assertEquals("play-components/index.json", path)
ByteArrayInputStream(index)
})
}
@Test fun approvedWindowsPrerequisiteUsesPinnedBytesAndAtomicReplacement() {
val destination = temporary.newFile("runtime")
destination.writeText("previous")
windowsStore { it.writeBytes(content) }.install(url, destination, digest)
assertArrayEquals(content, destination.readBytes())
}
@Test fun corruptWindowsDownloadPreservesExistingRuntime() {
val destination = temporary.newFile("runtime")
destination.writeText("previous")
assertThrows(IOException::class.java) {
windowsStore { it.writeBytes(ByteArray(content.size)) }.install(url, destination)
}
assertEquals("previous", destination.readText())
}
@Test fun wrongWindowsVersionAndInsecureSourceCannotStartDownload() {
val destination = temporary.newFile("runtime")
assertThrows(IOException::class.java) {
windowsStore { throw AssertionError("Unexpected download") }.install(url, destination, "0".repeat(64))
}
assertThrows(IOException::class.java) {
windowsStore("http://example.test/runtime") { throw AssertionError("Unexpected download") }.install(url, destination)
}
}
@Test fun missingComponentCannotUseNetwork() {
assertThrows(IOException::class.java) {
fixture().copy("https://other.test/component", ByteArrayOutputStream(), Long.MAX_VALUE)