Skip to content

Commit

Permalink
Set filtered jar's manifest time to epoch
Browse files Browse the repository at this point in the history
(cherry picked from commit a7aa354)
  • Loading branch information
Malandril authored and aloubyansky committed Sep 17, 2024
1 parent fb18562 commit c543035
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,13 @@ static void filterJarFile(Path resolvedDep, Path targetPath, Set<String> transfo
} else {
manifest = new Manifest();
}
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(targetPath), manifest)) {
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(targetPath))) {
JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME);
// Set manifest time to epoch to always make the same jar
manifestEntry.setTime(0);
out.putNextEntry(manifestEntry);
manifest.write(out);
out.closeEntry();
Enumeration<JarEntry> entries = in.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
Expand All @@ -1306,6 +1312,8 @@ static void filterJarFile(Path resolvedDep, Path targetPath, Set<String> transfo
while ((r = inStream.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
} finally {
out.closeEntry();
}
} else {
log.debugf("Removed %s from %s", entryName, resolvedDep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ void should_unsign_jar_when_filtered(@TempDir Path tempDir) throws Exception {
}
}

@Test
void manifestTimeShouldAlwaysBeSetToEpoch(@TempDir Path tempDir) throws Exception {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "myarchive.jar")
.addClasses(Integer.class)
.addManifest();
Path initialJar = tempDir.resolve("initial.jar");
Path filteredJar = tempDir.resolve("filtered.jar");
archive.as(ZipExporter.class).exportTo(new File(initialJar.toUri()), true);
JarResultBuildStep.filterJarFile(initialJar, filteredJar, Set.of("java/lang/Integer.class"));
try (JarFile jarFile = new JarFile(filteredJar.toFile())) {
assertThat(jarFile.stream())
.filteredOn(jarEntry -> jarEntry.getName().equals(JarFile.MANIFEST_NAME))
.isNotEmpty()
.allMatch(jarEntry -> jarEntry.getTime() == 0);
// Check that the manifest is still has attributes
Manifest manifest = jarFile.getManifest();
assertThat(manifest.getMainAttributes()).isNotEmpty();
}
}

private static KeyStore.PrivateKeyEntry createPrivateKeyEntry()
throws NoSuchAlgorithmException, CertificateException, OperatorCreationException, CertIOException {
KeyPairGenerator ky = KeyPairGenerator.getInstance("RSA");
Expand Down

0 comments on commit c543035

Please sign in to comment.