Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package io.johnsonlee.graphfold.engine.inputs.archive
import java.nio.file.Files
import java.nio.file.Path
import java.util.jar.JarFile
import java.util.jar.JarEntry
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

/** An explicitly selected archive; compiler outputs provide source evidence, never replacement graph bytes. */
data class ArchiveEntryInput(
Expand Down Expand Up @@ -52,7 +53,7 @@ private fun prepareFlatArchiveInput(
val restored = original.mapIndexed { index, artifact ->
selectedArchiveInventory(artifact, variantThreshold, output.resolve("inventory/$index.jar"))
}
val ordinary = restored.mapIndexed { index, path ->
val ordinary = original.mapIndexed { index, path ->
val archive = output.resolve("archives/$index.jar")
if (input.classpath[index].inventoryOnly) {
Files.createDirectories(archive.parent)
Expand All @@ -78,7 +79,7 @@ private fun prepareFlatArchiveInput(
.mapValues { (_, groups) -> groups.flatten().map { it.toFile().canonicalFile.toPath() }
.filter { it in ownedOutputs }.distinct() }.filterValues { it.isNotEmpty() }
val resources = restored.mapIndexed { index, artifact ->
packagedResources(artifact, output.resolve("resources/$index"))
packagedResources(artifact, output.resolve("resources/$index"), original = artifact == original[index])
}
val inventory = captureProductionInventory(classes, resources, restored,
input.classpath.indices.filter { input.classpath[it].application }.map { restored[it] })
Expand Down Expand Up @@ -115,9 +116,11 @@ private fun copyPackagedClasses(archive: Path, output: Path) {
}

/** Preserve each artifact's effective resource entries separately; conflicting properties remain unproved. */
private fun packagedResources(artifact: Path, output: Path): Path {
private fun packagedResources(artifact: Path, output: Path, original: Boolean): Path {
Files.createDirectories(output)
JarFile(artifact.toFile()).use { jar ->
// The inventory projection preserves signature evidence, but only original bytes can be authenticated.
val archive = if (original) JarFile(artifact.toFile()) else ZipFile(artifact.toFile())
archive.use { jar ->
jar.stream().use { entries ->
entries.filter { !it.isDirectory && !it.name.endsWith(".class") && !it.name.startsWith("META-INF/versions/") }
.forEach { entry -> copyPackagedResource(jar, entry, output) }
Expand All @@ -133,7 +136,7 @@ private fun ProductionInventory.withArchiveMetadata(input: ArchiveInput): Produc
resourceIndependentEntries = resourceIndependentEntries + inputs)
}

private fun copyPackagedResource(jar: JarFile, entry: JarEntry, output: Path) {
private fun copyPackagedResource(jar: ZipFile, entry: ZipEntry, output: Path) {
val target = output.resolve(entry.name).normalize()
checkInput(target.startsWith(output) && target != output && output.relativize(target).joinToString("/") == entry.name,
"Unsupported packaged resource path: ${entry.name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ internal fun archiveVariant(name: String): Pair<Int, String>? = VERSIONED_ENTRY.

private val VERSIONED_ENTRY = Regex("""META-INF/versions/([1-9][0-9]*)/(.+)""")

/** Inventory and graphs see the same build-selected MR contents, including effective resource entries. */
/**
* Inventory retains original metadata alongside build-selected contents. This is an evidence ZIP, not a signed JAR:
* consumers must use ZIP readers; graph normalization independently reads the original signed archive.
*/
internal fun selectedArchiveInventory(archive: Path, javaVersion: Int, output: Path): Path =
JarFile(archive.toFile()).use { jar ->
val multiRelease = jar.manifest?.mainAttributes?.getValue(Attributes.Name.MULTI_RELEASE)
Expand All @@ -43,3 +46,11 @@ internal fun selectedArchiveInventory(archive: Path, javaVersion: Int, output: P
private fun copyArchiveEntry(jar: JarFile, entry: JarEntry, target: JarOutputStream) {
jar.getInputStream(entry).use { it.copyTo(target) }
}

/** Signing blocks authenticate the original archive, never an analysis projection with rewritten entries. */
internal fun archiveSignature(name: String): Boolean {
val normalized = name.uppercase(java.util.Locale.ROOT)
if (!normalized.startsWith("META-INF/")) return false
val file = normalized.removePrefix("META-INF/")
return '/' !in file && (file.startsWith("SIG-") || file.substringAfterLast('.') in setOf("SF", "RSA", "DSA", "EC"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private fun materializeArchive(
if (!requiresArchiveProjection(archive, physical, entries.size, retained.size)) return@use dependency
validateArchiveProjection(archive, dependency)
writeInputArchive(target) { zip ->
retained.forEach { entry ->
retained.filterNot { archiveSignature(it.name) }.forEach { entry ->
val bytes = contents(entry)
writeEntry(zip, entry.name, if (entry.name == JarFile.MANIFEST_NAME) projectedManifest(bytes) else bytes)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ private fun materializeDirectory(
}
if (retained.size == entries.size) return dependency
writeInputArchive(target) { zip ->
retained.forEach { (name, file) ->
retained.filterKeys { !archiveSignature(it) }.forEach { (name, file) ->
val bytes = Files.readAllBytes(file)
writeEntry(zip, name, if (name == JarFile.MANIFEST_NAME) projectedManifest(bytes) else bytes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal fun normalizePackagedArchive(path: Path, output: Path): Path = JarFile(
val encoded = manifest?.let { value -> ByteArrayOutputStream().also { value.write(it) }.toByteArray() }
Files.createDirectories(output.parent)
ZipOutputStream(Files.newOutputStream(output)).use { zip ->
retained.forEach { entry -> copyPackagedEntry(jar, zip, entry, encoded) }
retained.filterNot { archiveSignature(it.name) }.forEach { entry -> copyPackagedEntry(jar, zip, entry, encoded) }
}
output
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package io.johnsonlee.graphfold.engine.inputs.archive

import java.nio.file.Files
import java.nio.file.Path
import java.security.MessageDigest
import java.util.jar.JarFile
import java.util.zip.ZipFile
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.ValueSource
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class SignedArchiveProjectionTest {
@TempDir lateinit var temporary: Path

@ParameterizedTest
@CsvSource("jar,8", "jar,17", "boot,8", "boot,17", "war,8", "war,17")
fun `signed MR packaged inputs retain evidence and select only the build JDK`(layout: String, javaVersion: Int) {
val library = signedArchive(multiRelease = true)
val original = Files.readAllBytes(library)
val archive = when (layout) {
"boot" -> projectionArchive(temporary.resolve("app.jar"), mapOf("BOOT-INF/lib/library.jar" to original))
"war" -> projectionArchive(temporary.resolve("app.war"), mapOf("WEB-INF/lib/library.jar" to original))
else -> library
}
val input = InputProject(Files.createDirectory(temporary.resolve("compiler")), emptyList(), emptyList())
val result = prepareArchiveInput(ArchiveInput("app", listOf(ArchiveEntryInput(archive))), input,
temporary.resolve("prepared"), variantThreshold = javaVersion)
val expected = bytecode(if (javaVersion < 17) 1 else 2)
val owned = result.classes.resolve("sample/Value.class")
val observed = if (Files.exists(owned)) Files.readAllBytes(owned) else result.dependencies.firstNotNullOf { path ->
JarFile(path.toFile()).use { jar -> jar.getJarEntry("sample/Value.class")?.let { jar.getInputStream(it).readBytes() } }
}
assertContentEquals(expected, observed)
result.dependencies.forEach(::readVerified)
assertContentEquals(original, Files.readAllBytes(library))
val resources = requireNotNull(result.production).entries
ZipFile(library.toFile()).use { jar ->
jar.entries().asSequence().filter { archiveSignature(it.name) || it.name == JarFile.MANIFEST_NAME }.forEach { entry ->
val hash = sha(jar.getInputStream(entry).use { it.readBytes() })
assertTrue(resources.any { (name, value) -> name.endsWith("/${entry.name}") && value == hash }, entry.name)
}
}
}

@Test fun `direct signed MR dependency projects without stale signing blocks`() {
val library = signedArchive(multiRelease = true)
val original = Files.readAllBytes(library)
val output = normalizeDependencyInputs(listOf(library), temporary.resolve("normalized"), variantThreshold = 17).single()
assertUnsignedSelected(output, bytecode(2))
assertContentEquals(original, Files.readAllBytes(library))
readVerified(library)
}

@Test fun `unchanged signed ordinary dependency retains the original signed archive`() {
val library = signedArchive(multiRelease = false)
assertEquals(library, selectedArchiveInventory(library, 17, temporary.resolve("inventory/view.jar")))
assertEquals(library, normalizeDependencyInputs(listOf(library), temporary.resolve("normalized"), variantThreshold = 17).single())
readVerified(library)
JarFile(library.toFile()).use { jar ->
val entry = jar.getJarEntry("sample/Value.class")
jar.getInputStream(entry).use { it.readBytes() }
assertTrue(entry.codeSigners.isNotEmpty())
}
}

@Test fun `manifest projection removes signing blocks but keeps original evidence`() {
val library = signedArchive(multiRelease = false, attributes = mapOf("Class-Path" to "other.jar"))
val original = Files.readAllBytes(library)
val output = normalizePackagedArchive(library, temporary.resolve("normalized/library.jar"))
assertUnsignedSelected(output, bytecode(1))
assertContentEquals(original, Files.readAllBytes(library))
readVerified(library)
}

@Test fun `projected exploded signed archive omits stale signing blocks`() {
val library = signedArchive(multiRelease = true)
val directory = Files.createDirectory(temporary.resolve("exploded"))
ZipFile(library.toFile()).use { jar ->
jar.entries().asSequence().filterNot { it.isDirectory }.forEach { entry ->
val target = directory.resolve(entry.name)
Files.createDirectories(target.parent)
jar.getInputStream(entry).use { Files.copy(it, target) }
}
}
val result = normalizeDependencyInputs(listOf(directory), temporary.resolve("normalized")).single()
assertUnsignedSelected(result, bytecode(1))
assertTrue(Files.exists(directory.resolve("META-INF/TEST.SF")))
}

@Test fun `tampered signed source is rejected before a projection can hide it`() {
val library = signedArchive(multiRelease = true)
val entries = ZipFile(library.toFile()).use { jar -> jar.entries().asSequence().associate { entry ->
entry.name to jar.getInputStream(entry).use { it.readBytes() }
} }.toMutableMap()
entries["META-INF/versions/17/sample/Value.class"] = bytecode(3)
val tampered = projectionArchive(temporary.resolve("tampered.jar"), entries)
assertFailsWith<SecurityException> {
selectedArchiveInventory(tampered, 17, temporary.resolve("inventory/view.jar"))
}
assertFailsWith<SecurityException> {
normalizeDependencyInputs(listOf(tampered), temporary.resolve("normalized"), variantThreshold = 17)
}
}

@ParameterizedTest
@ValueSource(strings = ["META-INF/KEY.SF", "meta-inf/key.rsa", "META-INF/KEY.DSA", "META-INF/KEY.EC", "META-INF/SIG-CUSTOM"])
fun `root signing blocks are omitted from graph projections`(name: String) {
assertTrue(archiveSignature(name))
}

@ParameterizedTest
@ValueSource(strings = ["OTHER/KEY.SF", "META-INF/services/KEY.SF", "META-INF/MANIFEST.MF", "META-INF/key.txt"])
fun `ordinary resources are not signing blocks`(name: String) {
assertFalse(archiveSignature(name))
}

private fun signedArchive(multiRelease: Boolean, attributes: Map<String, String> = emptyMap()): Path {
val entries = linkedMapOf("sample/Value.class" to bytecode(1), "settings.properties" to "value=1\n".toByteArray())
if (multiRelease) entries["META-INF/versions/17/sample/Value.class"] = bytecode(2)
val manifest = if (multiRelease) attributes + ("Multi-Release" to "true") else attributes
val archive = projectionArchive(temporary.resolve("library.jar"), entries, manifest)
val key = temporary.resolve("test.p12")
runTool("keytool", "-genkeypair", "-alias", "test", "-keyalg", "RSA", "-keysize", "2048", "-validity", "1",
"-dname", "CN=GraphFold test", "-storetype", "PKCS12", "-keystore", key.toString(), "-storepass", "testpass", "-noprompt")
runTool("jarsigner", "-keystore", key.toString(), "-storepass", "testpass", archive.toString(), "test")
readVerified(archive)
return archive
}

private fun runTool(tool: String, vararg arguments: String) {
val executable = Path.of(System.getProperty("java.home"), "bin", tool)
val process = ProcessBuilder(listOf(executable.toString()) + arguments).redirectErrorStream(true).start()
val output = process.inputStream.bufferedReader().use { it.readText() }
assertEquals(0, process.waitFor(), "$tool: $output")
}

private fun readVerified(path: Path) = JarFile(path.toFile()).use { jar ->
jar.entries().asSequence().filterNot { it.isDirectory }.forEach { jar.getInputStream(it).use { input -> input.readBytes() } }
}

private fun assertUnsignedSelected(path: Path, expected: ByteArray) = JarFile(path.toFile()).use { jar ->
assertFalse(jar.entries().asSequence().any { archiveSignature(it.name) })
assertContentEquals(expected, jar.getInputStream(jar.getJarEntry("sample/Value.class")).use { it.readBytes() })
readVerified(path)
}

private fun bytecode(value: Int): ByteArray = ClassWriter(0).apply {
visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "sample/Value", null, "java/lang/Object", null)
visitMethod(Opcodes.ACC_PUBLIC or Opcodes.ACC_STATIC, "value", "()I", null, null).apply {
visitCode(); visitLdcInsn(value); visitInsn(Opcodes.IRETURN); visitMaxs(1, 0); visitEnd()
}
visitEnd()
}.toByteArray()

private fun sha(bytes: ByteArray): String = MessageDigest.getInstance("SHA-256").digest(bytes).joinToString("") { "%02x".format(it) }
}
Loading