Skip to content

Commit

Permalink
Fix Union URL issue when in powerful classloaders.
Browse files Browse the repository at this point in the history
Cleanup code to better separate api.
Deprecate concept of passing in a default manifest.
  • Loading branch information
LexManos committed Nov 3, 2023
1 parent e22e708 commit c279804
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 195 deletions.
13 changes: 12 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ changelog {
fromTag '0.9'
}

jar {
tasks.named('jar', Jar).configure {
manifest {
attributes([
'Specification-Title': 'SecureModules',
Expand All @@ -50,6 +50,17 @@ jar {
}
}

tasks.register('writeManifest') {
doLast {
jar.manifest.writeTo(file('src/main/resources/META-INF/MANIFEST.MF'))
}
}

eclipse {
synchronizationTasks writeManifest
autoBuildTasks writeManifest
}

license {
header = file("LICENSE-header.txt")
}
Expand Down
16 changes: 8 additions & 8 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ dependencyResolutionManagement {
library('asm-tree', 'org.ow2.asm', 'asm-tree' ).versionRef('asm')
library('asm-commons', 'org.ow2.asm', 'asm-commons').versionRef('asm')
bundle('asm', ['asm', 'asm-tree', 'asm-commons'])
version('junit', '5.10.0')
library('junit-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit')
library('junit-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')
library('junit-platform-launcher', 'org.junit.platform:junit-platform-launcher:1.10.0')
bundle('junit-runtime', ['junit-engine', 'junit-platform-launcher'])
library('unsafe', 'net.minecraftforge:unsafe:0.9.2')

version('junit', '5.10.0')
library('junit-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit')
library('junit-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')
library('junit-platform-launcher', 'org.junit.platform:junit-platform-launcher:1.10.0')
bundle('junit-runtime', ['junit-engine', 'junit-platform-launcher'])

library('unsafe', 'net.minecraftforge:unsafe:0.9.2')
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/cpw/mods/cl/UnionURLStreamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

package cpw.mods.cl;

import cpw.mods.niofs.union.UnionPath;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.function.Function;

Expand All @@ -25,16 +25,19 @@ public String protocol() {

@Override
public Function<URL, InputStream> inputStreamFunction() {
return u-> {
return u -> {
try {
if (Paths.get(u.toURI()) instanceof UnionPath upath) {
return upath.buildInputStream();
} else {
throw new IllegalArgumentException("Invalid Path "+u.toURI()+" at UnionURLStreamHandler");
}
} catch (URISyntaxException e) {
throw new RuntimeException(e);
var path = Paths.get(u.toURI());
return Files.newInputStream(path);
} catch (URISyntaxException | IOException e) {
return sneak(e);
}
};
}


@SuppressWarnings("unchecked")
private static <E extends Throwable, R> R sneak(Exception exception) throws E {
throw (E)exception;
}
}
27 changes: 10 additions & 17 deletions src/main/java/cpw/mods/jarhandling/SecureJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

import cpw.mods.jarhandling.impl.Jar;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.module.ModuleDescriptor;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.CodeSigner;
import java.util.List;
Expand Down Expand Up @@ -60,17 +57,21 @@ static SecureJar from(BiPredicate<String, String> filter, final Path... paths) {
}

static SecureJar from(Function<SecureJar, JarMetadata> metadataSupplier, final Path... paths) {
return from(Manifest::new, metadataSupplier, paths);
return from(metadataSupplier, null, paths);
}

static SecureJar from(Function<SecureJar, JarMetadata> metadataSupplier, BiPredicate<String, String> filter, final Path... paths) {
return from(Manifest::new, metadataSupplier, filter, paths);
return new Jar(metadataSupplier, filter, paths);
}

/** Supplying a manifest is stupid. */
@Deprecated(forRemoval = true, since = "2.2")
static SecureJar from(Supplier<Manifest> defaultManifest, Function<SecureJar, JarMetadata> metadataSupplier, final Path... paths) {
return from(defaultManifest, metadataSupplier, null, paths);
}

/** Supplying a manifest is stupid. */
@Deprecated(forRemoval = true, since = "2.2")
static SecureJar from(Supplier<Manifest> defaultManifest, Function<SecureJar, JarMetadata> metadataSupplier, BiPredicate<String, String> filter, final Path... paths) {
return new Jar(defaultManifest, metadataSupplier, filter, paths);
}
Expand All @@ -85,19 +86,11 @@ static SecureJar from(Supplier<Manifest> defaultManifest, Function<SecureJar, Ja

Path getRootPath();

// TODO: [SM] Make this record into an interface for API
record Provider(String serviceName, List<String> providers) {
public static Provider fromPath(final Path path, final BiPredicate<String, String> pkgFilter) {
final var sname = path.getFileName().toString();
try {
var entries = Files.readAllLines(path).stream()
.map(String::trim)
.filter(l->l.length() > 0 && !l.startsWith("#"))
.filter(p-> pkgFilter == null || pkgFilter.test(p.replace('.','/'), ""))
.toList();
return new Provider(sname, entries);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
@Deprecated
public static Provider fromPath(Path path, BiPredicate<String, String> filter) {
return Jar.getProvider(path, filter);
}
}

Expand Down
Loading

0 comments on commit c279804

Please sign in to comment.