From 88c71b6c061740bbb2bea3ca171ce7c68782938c Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 8 Mar 2022 11:59:28 +0900 Subject: [PATCH] hash checking for module's jarfile --- assets/mods/basegame/metadata.properties | 3 +++ assets/mods/dwarventech/metadata.properties | 1 + src/net/torvald/terrarum/ModMgr.kt | 17 ++++++++++++++++- .../torvald/terrarum/ui/UIItemModuleInfoCell.kt | 11 ++++++----- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/assets/mods/basegame/metadata.properties b/assets/mods/basegame/metadata.properties index 4fcafbf1b..57e9cba8c 100644 --- a/assets/mods/basegame/metadata.properties +++ b/assets/mods/basegame/metadata.properties @@ -25,6 +25,9 @@ version=0.3.0 # Due to security reasons, loading an arbitrary JAR is not allowed. jar= +# Sha256sum of the External JAR, if any +jarhash= + # Modules that must be pre-installed, separate multiple by semicolon (;) # Dependency syntax: "module's identification name (aka folder name) spaces allowed versionnumber" # Version number: diff --git a/assets/mods/dwarventech/metadata.properties b/assets/mods/dwarventech/metadata.properties index a947a47ea..9942430ab 100644 --- a/assets/mods/dwarventech/metadata.properties +++ b/assets/mods/dwarventech/metadata.properties @@ -6,4 +6,5 @@ entrypoint=net.torvald.terrarum.modulecomputers.EntryPoint releasedate=2021-12-03 version=0.3.0 jar=ModuleComputers.jar +jarhash=b2d50cc10693885f060a5825a4fb2be2993b461bae2e58b4a4900fa169ec898f dependency=basegame 0.3.0 diff --git a/src/net/torvald/terrarum/ModMgr.kt b/src/net/torvald/terrarum/ModMgr.kt index 9f53215fa..dae8ab1d6 100644 --- a/src/net/torvald/terrarum/ModMgr.kt +++ b/src/net/torvald/terrarum/ModMgr.kt @@ -11,8 +11,10 @@ import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.MaterialCodex import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.serialise.Common import net.torvald.terrarum.utils.CSVFetcher import net.torvald.terrarum.utils.JsonFetcher +import org.apache.commons.codec.digest.DigestUtils import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVParser import org.apache.commons.csv.CSVRecord @@ -100,6 +102,8 @@ object ModMgr { errorLogs.add(ModuleErrorInfo(type, moduleName, cause)) } + private val digester = DigestUtils.getSha256Digest() + /** * Try to create an instance of a "titlescreen" from the current load order set. */ @@ -162,6 +166,7 @@ object ModMgr { val releaseDate = modMetadata.getProperty("releasedate") val version = modMetadata.getProperty("version") val jar = modMetadata.getProperty("jar") + val jarHash = modMetadata.getProperty("jarhash").uppercase() val dependency = modMetadata.getProperty("dependency").split(Regex(""";[ ]*""")).filter { it.isNotEmpty() }.toTypedArray() val isDir = FileSystems.getDefault().getPath("$modDir/$moduleName").toFile().isDirectory @@ -203,10 +208,20 @@ object ModMgr { if (jar.isNotBlank()) { val urls = arrayOf() + val jarFilePath = "${File(modDir).absolutePath}/$moduleName/$jar" val cl = JarFileLoader(urls) - cl.addFile("${File(modDir).absolutePath}/$moduleName/$jar") + cl.addFile(jarFilePath) moduleClassloader[moduleName] = cl + // check for hash + digester.reset() + val hash = digester.digest(File(jarFilePath).readBytes()).joinToString("","","") { it.toInt().and(255).toString(16).uppercase().padStart(2,'0') } + + if (jarHash != hash) { + println("Hash expected: $jarHash, got: $hash") + throw IllegalStateException("Module Jarfile hash mismatch") + } + // check for module-info.java val moduleInfoPath = cl.getResources("module-info.class").toList().filter { it.toString().contains("$moduleName/$jar!/module-info.class") && it.toString().endsWith("module-info.class")} if (moduleInfoPath.size == 0) { diff --git a/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt b/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt index 4c9e57797..cbb1c59c6 100644 --- a/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt +++ b/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt @@ -6,10 +6,7 @@ import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.utils.GdxRuntimeException -import net.torvald.terrarum.App -import net.torvald.terrarum.CommonResourcePool -import net.torvald.terrarum.ModMgr -import net.torvald.terrarum.blendNormal +import net.torvald.terrarum.* import net.torvald.terrarum.modulebasegame.ui.MODULEINFO_CELL_HEIGHT import net.torvald.terrarum.modulebasegame.ui.MODULEINFO_CELL_WIDTH @@ -27,6 +24,7 @@ class UIItemModuleInfoCell( private val modProp = ModMgr.moduleInfo[modName] ?: ModMgr.moduleInfoErrored[modName]!! + private val modErrors = ModMgr.errorLogs.filter { it.moduleName == modName } private val modErrored = (ModMgr.moduleInfo[modName] == null) private val modIcon = try { @@ -88,7 +86,10 @@ class UIItemModuleInfoCell( batch.shader = null batch.color = Color.WHITE App.fontGame.draw(batch, "$ccZero${modName.toUpperCase()}$ccNum $modVer", initialX + 86f + 3f, initialY + 2f) - App.fontGame.draw(batch, "$ccDesc$modDesc", initialX + 86f + 3f, initialY + 26f) + if (modErrored) + App.fontGame.draw(batch, "$emphRed${modErrors.first().cause?.message}", initialX + 86f + 3f, initialY + 26f) + else + App.fontGame.draw(batch, "$ccDesc$modDesc", initialX + 86f + 3f, initialY + 26f) App.fontGame.draw(batch, "$ccZero2$modAuthor$ccNum2 $modDate", initialX + 86f + 3f, initialY + 50f) if (modErrored) {