hash checking for module's jarfile

This commit is contained in:
minjaesong
2022-03-08 11:59:28 +09:00
parent 96e0444be1
commit 88c71b6c06
4 changed files with 26 additions and 6 deletions

View File

@@ -25,6 +25,9 @@ version=0.3.0
# Due to security reasons, loading an arbitrary JAR is not allowed. # Due to security reasons, loading an arbitrary JAR is not allowed.
jar= jar=
# Sha256sum of the External JAR, if any
jarhash=
# Modules that must be pre-installed, separate multiple by semicolon (;) # Modules that must be pre-installed, separate multiple by semicolon (;)
# Dependency syntax: "module's identification name (aka folder name) spaces allowed versionnumber" # Dependency syntax: "module's identification name (aka folder name) spaces allowed versionnumber"
# Version number: # Version number:

View File

@@ -6,4 +6,5 @@ entrypoint=net.torvald.terrarum.modulecomputers.EntryPoint
releasedate=2021-12-03 releasedate=2021-12-03
version=0.3.0 version=0.3.0
jar=ModuleComputers.jar jar=ModuleComputers.jar
jarhash=b2d50cc10693885f060a5825a4fb2be2993b461bae2e58b4a4900fa169ec898f
dependency=basegame 0.3.0 dependency=basegame 0.3.0

View File

@@ -11,8 +11,10 @@ import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.itemproperties.MaterialCodex import net.torvald.terrarum.itemproperties.MaterialCodex
import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.utils.CSVFetcher import net.torvald.terrarum.utils.CSVFetcher
import net.torvald.terrarum.utils.JsonFetcher import net.torvald.terrarum.utils.JsonFetcher
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord import org.apache.commons.csv.CSVRecord
@@ -100,6 +102,8 @@ object ModMgr {
errorLogs.add(ModuleErrorInfo(type, moduleName, cause)) 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. * 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 releaseDate = modMetadata.getProperty("releasedate")
val version = modMetadata.getProperty("version") val version = modMetadata.getProperty("version")
val jar = modMetadata.getProperty("jar") val jar = modMetadata.getProperty("jar")
val jarHash = modMetadata.getProperty("jarhash").uppercase()
val dependency = modMetadata.getProperty("dependency").split(Regex(""";[ ]*""")).filter { it.isNotEmpty() }.toTypedArray() val dependency = modMetadata.getProperty("dependency").split(Regex(""";[ ]*""")).filter { it.isNotEmpty() }.toTypedArray()
val isDir = FileSystems.getDefault().getPath("$modDir/$moduleName").toFile().isDirectory val isDir = FileSystems.getDefault().getPath("$modDir/$moduleName").toFile().isDirectory
@@ -203,10 +208,20 @@ object ModMgr {
if (jar.isNotBlank()) { if (jar.isNotBlank()) {
val urls = arrayOf<URL>() val urls = arrayOf<URL>()
val jarFilePath = "${File(modDir).absolutePath}/$moduleName/$jar"
val cl = JarFileLoader(urls) val cl = JarFileLoader(urls)
cl.addFile("${File(modDir).absolutePath}/$moduleName/$jar") cl.addFile(jarFilePath)
moduleClassloader[moduleName] = cl 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 // 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")} 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) { if (moduleInfoPath.size == 0) {

View File

@@ -6,10 +6,7 @@ import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.utils.GdxRuntimeException import com.badlogic.gdx.utils.GdxRuntimeException
import net.torvald.terrarum.App import net.torvald.terrarum.*
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.blendNormal
import net.torvald.terrarum.modulebasegame.ui.MODULEINFO_CELL_HEIGHT import net.torvald.terrarum.modulebasegame.ui.MODULEINFO_CELL_HEIGHT
import net.torvald.terrarum.modulebasegame.ui.MODULEINFO_CELL_WIDTH 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 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 modErrored = (ModMgr.moduleInfo[modName] == null)
private val modIcon = try { private val modIcon = try {
@@ -88,7 +86,10 @@ class UIItemModuleInfoCell(
batch.shader = null batch.shader = null
batch.color = Color.WHITE batch.color = Color.WHITE
App.fontGame.draw(batch, "$ccZero${modName.toUpperCase()}$ccNum $modVer", initialX + 86f + 3f, initialY + 2f) 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) App.fontGame.draw(batch, "$ccZero2$modAuthor$ccNum2 $modDate", initialX + 86f + 3f, initialY + 50f)
if (modErrored) { if (modErrored) {