Skip to content

Commit

Permalink
Remove deprecated usage of SecureJar methods.
Browse files Browse the repository at this point in the history
Add logging to testing endpoint.
  • Loading branch information
shartte committed Jan 13, 2024
1 parent 8d4bef4 commit 97dd066
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ repositories {
}

dependencies {
compileOnly('org.jetbrains:annotations:24.0.1')
implementation('cpw.mods:securejarhandler:2.1.24')
}

java {
toolchain.languageVersion = JavaLanguageVersion.of(16)
toolchain.languageVersion = JavaLanguageVersion.of(17)
withSourcesJar()
}

Expand Down
32 changes: 26 additions & 6 deletions src/main/java/cpw/mods/bootstraplauncher/BootstrapLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,38 @@

import cpw.mods.cl.JarModuleFinder;
import cpw.mods.cl.ModuleClassLoader;
import cpw.mods.jarhandling.JarContentsBuilder;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.niofs.union.UnionPathFilter;
import org.jetbrains.annotations.VisibleForTesting;

import java.io.File;
import java.io.IOException;
import java.lang.module.ModuleFinder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Consumer;

public class BootstrapLauncher {
private static final boolean DEBUG = System.getProperties().containsKey("bsl.debug");

public static void testMain(String... args) {
@VisibleForTesting
public static void unitTestingMain(String... args) {
System.err.println("*".repeat(80));
System.err.println("Starting in unit testing mode. Misconfiguration may mask bugs that would occur in normal operation.");
System.err.println("*".repeat(80));
run(false, args);
}

Expand Down Expand Up @@ -103,8 +119,12 @@ private static void run(boolean classloaderIsolation, String... args) {
var paths = e.getValue();
if (paths.size() == 1 && Files.notExists(paths.get(0))) return;
var pathsArray = paths.toArray(Path[]::new);
var jar = SecureJar.from(new PackageTracker(Set.copyOf(previousPackages), pathsArray), pathsArray);
var packages = jar.getPackages();
var jarContents = new JarContentsBuilder()
.paths(pathsArray)
.pathFilter(new PackageTracker(Set.copyOf(previousPackages), pathsArray))
.build();
var jar = SecureJar.from(jarContents);
var packages = jar.moduleDataProvider().descriptor().packages();

if (DEBUG) {
System.out.println("bsl: the following paths are merged together in module " + name);
Expand Down Expand Up @@ -171,7 +191,7 @@ private static Map<String, String> getMergeFilenameMap() {
return filenameMap;
}

private record PackageTracker(Set<String> packages, Path... paths) implements BiPredicate<String, String> {
private record PackageTracker(Set<String> packages, Path... paths) implements UnionPathFilter {
@Override
public boolean test(final String path, final String basePath) {
// This method returns true if the given path is allowed within the JAR (filters out 'bad' paths)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
uses java.util.function.Consumer;
requires java.base;
requires cpw.mods.securejarhandler;
requires static org.jetbrains.annotations;

exports cpw.mods.bootstraplauncher;
}

0 comments on commit 97dd066

Please sign in to comment.