-
Notifications
You must be signed in to change notification settings - Fork 25.3k
improve support for bytecode patching signed jars #128613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e4c087
improve support for bytecode patching signed jars
richard-dennehy 1093646
Update docs/changelog/128613.yaml
richard-dennehy 58ca8d8
use equals rather than endsWith
richard-dennehy b7ea4e6
keep main manifest attributes when unsigning jar
richard-dennehy ee4def7
ensure unsigned Manifest file stream is closed
richard-dennehy f0baf92
Merge branch 'main' into signed-jar-patcher
richard-dennehy d6fb825
remove only signature-related attributes from manifest
richard-dennehy a973835
[CI] Auto commit changes from spotless
elasticsearchmachine a4d9849
Merge remote-tracking branch 'upstream/main' into signed-jar-patcher
jfreden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,11 @@ | |
import java.util.HexFormat; | ||
import java.util.Locale; | ||
import java.util.function.Function; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
import java.util.jar.JarOutputStream; | ||
import java.util.jar.Manifest; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES; | ||
|
@@ -60,6 +62,10 @@ public String toString() { | |
} | ||
} | ||
|
||
public static void patchJar(File inputJar, File outputJar, Collection<PatcherInfo> patchers) { | ||
patchJar(inputJar, outputJar, patchers, false); | ||
} | ||
|
||
/** | ||
* Patches the classes in the input JAR file, using the collection of patchers. Each patcher specifies a target class (its jar entry | ||
* name) and the SHA256 digest on the class bytes. | ||
|
@@ -69,8 +75,11 @@ public String toString() { | |
* @param inputFile the JAR file to patch | ||
* @param outputFile the output (patched) JAR file | ||
* @param patchers list of patcher info (classes to patch (jar entry name + optional SHA256 digest) and ASM visitor to transform them) | ||
* @param unsignJar whether to remove class signatures from the JAR Manifest; set this to true when patching a signed JAR, | ||
* otherwise the patched classes will fail to load at runtime due to mismatched signatures. | ||
* @see <a href="https://docs.oracle.com/javase/tutorial/deployment/jar/intro.html">Understanding Signing and Verification</a> | ||
*/ | ||
public static void patchJar(File inputFile, File outputFile, Collection<PatcherInfo> patchers) { | ||
public static void patchJar(File inputFile, File outputFile, Collection<PatcherInfo> patchers, boolean unsignJar) { | ||
var classPatchers = patchers.stream().collect(Collectors.toMap(PatcherInfo::jarEntryName, Function.identity())); | ||
var mismatchedClasses = new ArrayList<MismatchInfo>(); | ||
try (JarFile jarFile = new JarFile(inputFile); JarOutputStream jos = new JarOutputStream(new FileOutputStream(outputFile))) { | ||
|
@@ -101,9 +110,23 @@ public static void patchJar(File inputFile, File outputFile, Collection<PatcherI | |
); | ||
} | ||
} else { | ||
// Read the entry's data and write it to the new JAR | ||
try (InputStream is = jarFile.getInputStream(entry)) { | ||
is.transferTo(jos); | ||
if (unsignJar && entryName.equals("META-INF/MANIFEST.MF")) { | ||
var manifest = new Manifest(is); | ||
for (var manifestEntry : manifest.getEntries().entrySet()) { | ||
var nonSignatureAttributes = new Attributes(); | ||
for (var attribute : manifestEntry.getValue().entrySet()) { | ||
if (attribute.getKey().toString().endsWith("Digest") == false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in practice, the digest keys appear to either be https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signature_Validation |
||
nonSignatureAttributes.put(attribute.getKey(), attribute.getValue()); | ||
} | ||
} | ||
manifestEntry.setValue(nonSignatureAttributes); | ||
} | ||
manifest.write(jos); | ||
} else if (unsignJar == false || entryName.matches("META-INF/.*\\.SF") == false) { | ||
// Read the entry's data and write it to the new JAR | ||
is.transferTo(jos); | ||
} | ||
} | ||
} | ||
jos.closeEntry(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 128613 | ||
summary: Improve support for bytecode patching signed jars | ||
area: Infra/Core | ||
type: enhancement | ||
issues: [] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
unsignJar
should be the default behaviour since if there is a signature it will always be invalid after patching the jar. It also shouldn't impact existing usages of this method since they appear to not use signed jars (they work after patching).