mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
fix(gog): resolve exact-path depot overlays before validation (#2720)
This commit is contained in:
1 parent
9e405ec92c
commit
5ca0575e67
3 files changed
+118
-2
No files matched your search
@@ -100,6 +100,7 @@ internal object GogContent {
|
||||
val manifest = metadata(build.link, token)
|
||||
onManifest(manifest)
|
||||
val files = JSONArray()
|
||||
val overlayPositions = HashMap<String, Int>()
|
||||
val depots = if (build.generation == 1) manifest.optJSONObject("product")?.optJSONArray("depots") ?: manifest.optJSONArray("depot") else manifest.optJSONArray("depots")
|
||||
check(depots != null) { "GOG build has no depot list" }
|
||||
val resolvedLanguage = StoreLanguage.pick(StoreLanguage.codesFromDepots(depots), old.language)
|
||||
@@ -132,10 +133,15 @@ internal object GogContent {
|
||||
val directory = type == "DepotDirectory" || entry.has("directory")
|
||||
val chunks = entry.optJSONArray("chunks") ?: JSONArray()
|
||||
val size = if (build.generation == 2) chunks.objects().sumOf { it.optLong("size") } else entry.optLong("size")
|
||||
files.put(JSONObject().put("path", path).put("size", size).put("directory", directory)
|
||||
val file = JSONObject().put("path", path).put("size", size).put("directory", directory)
|
||||
.put("md5", entry.optString("md5").ifBlank { entry.optString("hash") }.takeIf { it.matches(Regex("[a-fA-F0-9]{32}")) }.orEmpty())
|
||||
.put("sha256", entry.optString("sha256")).put("chunks", chunks).put("product", product)
|
||||
.put("offset", entry.optLong("offset")).put("remotePath", entry.optString("url").substringBefore('?')))
|
||||
.put("offset", entry.optLong("offset")).put("remotePath", entry.optString("url").substringBefore('?'))
|
||||
val position = if (build.generation == 2) overlayPositions.putIfAbsent(path, files.length()) else null
|
||||
if (position == null) files.put(file) else {
|
||||
require(files.getJSONObject(position).getBoolean("directory") == directory)
|
||||
files.put(position, file)
|
||||
}
|
||||
}
|
||||
}
|
||||
check(files.length() > 0) { "GOG build contains no matching files" }
|
||||
|
||||
@@ -291,6 +291,58 @@ class GogMaintenanceTest {
|
||||
assertEquals("patch_(52404)+special.exe", GogContent.downloadFilename("https://example.invalid/patch_%2852404%29+special.exe?token=temporary"))
|
||||
}
|
||||
|
||||
private fun overlaySnapshot(base: JSONArray, dlc: JSONArray, owned: Set<String> = setOf("2")): GogInstallRecord {
|
||||
val manifest = JSONObject().put("depots", JSONArray().put(JSONObject().put("productId", "1")
|
||||
.put("languages", JSONArray().put("*")).put("files", base).put("items", base)).put(JSONObject().put("productId", "2")
|
||||
.put("languages", JSONArray().put("*")).put("files", dlc).put("items", dlc)))
|
||||
return GogContent.snapshot(GogInstallRecord("1", "depot"), GogBuild("build", "", 2, "", "https://example.invalid/manifest"),
|
||||
"unused", ownership = { owned }, metadata = { _, _ -> manifest })
|
||||
}
|
||||
|
||||
@Test fun generationTwoOwnedDepotOverlaysReplaceExactPathsBeforeValidation() {
|
||||
val fixture = JSONObject(javaClass.classLoader!!.getResource("gog/gen2-overlay-excerpt.json")!!.readText())
|
||||
val base = fixture.getJSONArray("base")
|
||||
val dlc = fixture.getJSONArray("dlc")
|
||||
base.put(JSONObject().put("path", "unique.bin").put("type", "DepotFile").put("chunks", JSONArray()))
|
||||
val snapshot = overlaySnapshot(base, dlc)
|
||||
assertEquals(3, snapshot.files.length())
|
||||
assertEquals(listOf("gog.dat", "gog.idx", "unique.bin"), snapshot.files.objects().map { it.getString("path") })
|
||||
for (i in 0..1) {
|
||||
val file = snapshot.files.getJSONObject(i)
|
||||
assertEquals("2", file.getString("product"))
|
||||
assertEquals(dlc.getJSONObject(i).getJSONArray("chunks").toString(), file.getJSONArray("chunks").toString())
|
||||
assertEquals(dlc.getJSONObject(i).getJSONArray("chunks").getJSONObject(0).getLong("size"), file.getLong("size"))
|
||||
}
|
||||
assertEquals("1", snapshot.files.getJSONObject(2).getString("product"))
|
||||
GogFiles.validate(snapshot.files, temporary.newFolder())
|
||||
val baseOnly = overlaySnapshot(base, dlc, emptySet())
|
||||
assertEquals(base.getJSONObject(0).getJSONArray("chunks").toString(), baseOnly.files.getJSONObject(0).getJSONArray("chunks").toString())
|
||||
assertTrue(baseOnly.files.objects().all { it.getString("product") == "1" })
|
||||
GogFiles.validate(baseOnly.files, temporary.newFolder())
|
||||
}
|
||||
|
||||
@Test fun overlaysDoNotHideCaseAliasesTraversalLinksOrPathTypeChanges() {
|
||||
fun entry(path: String, type: String = "DepotFile") = JSONObject().put("path", path).put("type", type)
|
||||
for (path in listOf("game.bin", "../Game.bin", "Data/../Game.bin", ".droiddeck-store.json")) {
|
||||
val snapshot = overlaySnapshot(JSONArray().put(entry("Game.bin")), JSONArray().put(entry(path)))
|
||||
assertThrows(IllegalArgumentException::class.java) { GogFiles.validate(snapshot.files, temporary.newFolder()) }
|
||||
}
|
||||
assertThrows(IllegalArgumentException::class.java) {
|
||||
overlaySnapshot(JSONArray().put(entry("Game.bin")), JSONArray().put(entry("Game.bin", "DepotDirectory")))
|
||||
}
|
||||
assertThrows(IllegalStateException::class.java) {
|
||||
overlaySnapshot(JSONArray().put(entry("Game.bin")), JSONArray().put(entry("Game.bin", "DepotLink")))
|
||||
}
|
||||
val identical = JSONArray().put(entry("Game.bin"))
|
||||
assertEquals(1, overlaySnapshot(identical, identical).files.length())
|
||||
val directories = JSONArray().put(entry("Data", "DepotDirectory"))
|
||||
val merged = overlaySnapshot(directories, JSONArray().put(entry("Data", "DepotDirectory")).put(entry("Data/game.bin")))
|
||||
assertEquals(2, merged.files.length())
|
||||
GogFiles.validate(merged.files, temporary.newFolder())
|
||||
val aliases = overlaySnapshot(JSONArray().put(entry("Data/a")), JSONArray().put(entry("data/b")))
|
||||
assertThrows(IllegalArgumentException::class.java) { GogFiles.validate(aliases.files, temporary.newFolder()) }
|
||||
}
|
||||
|
||||
@Test fun pathsRejectTraversalSymlinksCaseCollisionsAndMetadataOverlap() {
|
||||
val folder = temporary.newFolder()
|
||||
for (path in listOf("../escape", "/absolute", "C:/file", "a/./file", "a//file")) {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"base": [
|
||||
{
|
||||
"path": "gog.dat",
|
||||
"chunks": [
|
||||
{
|
||||
"md5": "63b62221caca352670bec591f0e4ed7a",
|
||||
"size": 65664,
|
||||
"compressedMd5": "c5695c2838aa020b028334cda81c89e6",
|
||||
"compressedSize": 34046
|
||||
}
|
||||
],
|
||||
"type": "DepotFile"
|
||||
},
|
||||
{
|
||||
"path": "gog.idx",
|
||||
"chunks": [
|
||||
{
|
||||
"md5": "0a1f9d90b61039cb3f4f2952f47aeea7",
|
||||
"size": 28,
|
||||
"compressedMd5": "7a7f3c3d18c10a3890426ed3f11f6220",
|
||||
"compressedSize": 24
|
||||
}
|
||||
],
|
||||
"type": "DepotFile",
|
||||
"sfcRef": {
|
||||
"offset": 11,
|
||||
"size": 28
|
||||
}
|
||||
}
|
||||
],
|
||||
"dlc": [
|
||||
{
|
||||
"path": "gog.dat",
|
||||
"chunks": [
|
||||
{
|
||||
"md5": "dac36aea47f5de743d0e0ca10c79ad58",
|
||||
"size": 16777344,
|
||||
"compressedMd5": "a27cd9af6e5183a6392c25c973fc367c",
|
||||
"compressedSize": 3992002
|
||||
}
|
||||
],
|
||||
"type": "DepotFile"
|
||||
},
|
||||
{
|
||||
"path": "gog.idx",
|
||||
"chunks": [
|
||||
{
|
||||
"md5": "ae818b813bd3585c47846cf1e288b2e5",
|
||||
"size": 28,
|
||||
"compressedMd5": "869fd5dd8c9ad554ca8bccb0317a9757",
|
||||
"compressedSize": 26
|
||||
}
|
||||
],
|
||||
"type": "DepotFile"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user