Skip to content

Commit e749b19

Browse files
authored
Add reflective check for ManifestEntryVerifier's different constructors (#92)
1 parent 6a8cac8 commit e749b19

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/main/java/cpw/mods/modlauncher/SecureJarHandler.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
import sun.security.util.ManifestEntryVerifier;
2525

2626
import javax.annotation.Nullable;
27+
28+
import java.lang.reflect.Constructor;
2729
import java.lang.reflect.Field;
2830
import java.lang.reflect.InvocationTargetException;
2931
import java.lang.reflect.Method;
3032
import java.net.URL;
3133
import java.security.*;
3234
import java.util.HashMap;
3335
import java.util.Map;
36+
import java.util.function.Function;
3437
import java.util.jar.JarEntry;
38+
import java.util.jar.JarFile;
3539
import java.util.jar.Manifest;
3640

3741
import static cpw.mods.modlauncher.LogMarkers.CLASSLOADING;
@@ -41,6 +45,7 @@ public class SecureJarHandler {
4145
private static final Method BEGIN_ENTRY = LamdbaExceptionUtils.uncheck(()->JVCLASS.getMethod("beginEntry", JarEntry.class, ManifestEntryVerifier.class));
4246
private static final Method UPDATE = LamdbaExceptionUtils.uncheck(()->JVCLASS.getMethod("update", int.class, byte[].class, int.class, int.class, ManifestEntryVerifier.class));
4347
private static final Field JV;
48+
private static final Function<Manifest, ManifestEntryVerifier> MEV_FACTORY;
4449
static {
4550
Field jv;
4651
try {
@@ -52,7 +57,23 @@ public class SecureJarHandler {
5257
LogManager.getLogger().warn("LEGACY JDK DETECTED, SECURED JAR HANDLING DISABLED");
5358
jv = null;
5459
}
60+
61+
Function<Manifest, ManifestEntryVerifier> mevFactory;
62+
try {
63+
Constructor<ManifestEntryVerifier> mevConstructor = ManifestEntryVerifier.class.getConstructor(Manifest.class, String.class);
64+
mevFactory = LamdbaExceptionUtils.rethrowFunction((manifest) -> mevConstructor.newInstance(manifest, JarFile.MANIFEST_NAME));
65+
} catch (NoSuchMethodException e) {
66+
try {
67+
Constructor<ManifestEntryVerifier> mevConstructor = ManifestEntryVerifier.class.getConstructor(Manifest.class);
68+
mevFactory = LamdbaExceptionUtils.rethrowFunction((manifest) -> mevConstructor.newInstance(manifest));
69+
} catch (NoSuchMethodException e2) {
70+
mevFactory = null;
71+
jv = null;
72+
}
73+
}
74+
5575
JV = jv;
76+
MEV_FACTORY = mevFactory;
5677
}
5778

5879

@@ -63,7 +84,7 @@ public static CodeSource createCodeSource(final String name, @Nullable final URL
6384
if (manifest == null) return null;
6485
if (url == null) return null;
6586
JarEntry je = new JarEntry(name);
66-
ManifestEntryVerifier mev = new ManifestEntryVerifier(manifest);
87+
ManifestEntryVerifier mev = MEV_FACTORY.apply(manifest);
6788
Object obj = LamdbaExceptionUtils.uncheck(()->JV.get(manifest));
6889
if (obj == null) {
6990
// we don't have a fully fledged manifest with security info, for some reason (likely loaded by default JAR code, rather than our stuff)

0 commit comments

Comments
 (0)