Skip to content

Commit 0e4c087

Browse files
improve support for bytecode patching signed jars
1 parent df43f83 commit 0e4c087

File tree

1 file changed

+12
-4
lines changed
  • build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/dependencies/patches

1 file changed

+12
-4
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/dependencies/patches/Utils.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public String toString() {
6060
}
6161
}
6262

63+
public static void patchJar(File inputJar, File outputJar, Collection<PatcherInfo> patchers) {
64+
patchJar(inputJar, outputJar, patchers, true);
65+
}
66+
6367
/**
6468
* Patches the classes in the input JAR file, using the collection of patchers. Each patcher specifies a target class (its jar entry
6569
* name) and the SHA256 digest on the class bytes.
@@ -69,8 +73,10 @@ public String toString() {
6973
* @param inputFile the JAR file to patch
7074
* @param outputFile the output (patched) JAR file
7175
* @param patchers list of patcher info (classes to patch (jar entry name + optional SHA256 digest) and ASM visitor to transform them)
76+
* @param preserveManifest whether to include the manifest file from the input JAR; set this to false when patching a signed JAR,
77+
* otherwise the patched classes will fail to load at runtime due to mismatched signatures
7278
*/
73-
public static void patchJar(File inputFile, File outputFile, Collection<PatcherInfo> patchers) {
79+
public static void patchJar(File inputFile, File outputFile, Collection<PatcherInfo> patchers, boolean preserveManifest) {
7480
var classPatchers = patchers.stream().collect(Collectors.toMap(PatcherInfo::jarEntryName, Function.identity()));
7581
var mismatchedClasses = new ArrayList<MismatchInfo>();
7682
try (JarFile jarFile = new JarFile(inputFile); JarOutputStream jos = new JarOutputStream(new FileOutputStream(outputFile))) {
@@ -101,9 +107,11 @@ public static void patchJar(File inputFile, File outputFile, Collection<PatcherI
101107
);
102108
}
103109
} else {
104-
// Read the entry's data and write it to the new JAR
105-
try (InputStream is = jarFile.getInputStream(entry)) {
106-
is.transferTo(jos);
110+
if (preserveManifest || entryName.endsWith("MANIFEST.MF") == false) {
111+
// Read the entry's data and write it to the new JAR
112+
try (InputStream is = jarFile.getInputStream(entry)) {
113+
is.transferTo(jos);
114+
}
107115
}
108116
}
109117
jos.closeEntry();

0 commit comments

Comments
 (0)