Skip to content

Commit

Permalink
Fix knot class loading allowlist not working (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma authored May 26, 2024
1 parent 94b5cc3 commit a46634f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,19 @@ Class<?> tryLoadClass(String name, boolean allowFromParent) throws ClassNotFound
}
}

if (!allowedPrefixes.isEmpty()) {
if (!allowedPrefixes.isEmpty() && url != null) {
String fileName = LoaderUtil.getClassFileName(name);
URL codeSource = null;

try {
codeSource = UrlUtil.getSource(fileName, url);
} catch (UrlConversionException e) {
Log.warn(LogCategory.GENERAL, "Failed to get the code source URL for " + url);
}

String[] prefixes;

if (url != null
&& (prefixes = allowedPrefixes.get(url.toString())) != null) {
if (codeSource != null && (prefixes = allowedPrefixes.get(codeSource.toString())) != null) {
assert prefixes.length > 0;
boolean found = false;

Expand All @@ -260,7 +268,7 @@ Class<?> tryLoadClass(String name, boolean allowFromParent) throws ClassNotFound
}

if (!found) {
throw new ClassNotFoundException("class "+name+" is currently restricted from being loaded");
throw new ClassNotFoundException("class " + name + " is currently restricted from being loaded");
}
}
}
Expand Down Expand Up @@ -350,7 +358,6 @@ Class<?> tryLoadClass(String name, boolean allowFromParent) throws ClassNotFound
return c;
}


private boolean shouldRerouteToParent(String name) {
return name.startsWith("org.slf4j.") || name.startsWith("org.apache.logging.log4j.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/quiltmc/loader/impl/util/UrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static URL asUrl(File file) throws MalformedURLException {
}

public static URL asUrl(Path path) throws MalformedURLException {
return path.toUri().toURL();
return LoaderUtil.normalizePath(path).toUri().toURL();
}

public static Path getCodeSource(URL url, String localPath) throws UrlConversionException {
Expand Down

0 comments on commit a46634f

Please sign in to comment.