Skip to content
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

Path Traversal vulnerability fix (powered by Mobb) #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class MavenWrapperDownloader {

public static void main(String args[]) {
System.out.println("- Downloader started");
ensurePathIsRelative(args[0]);
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());

Expand Down Expand Up @@ -94,6 +95,37 @@ public static void main(String args[]) {
}
}

private static void ensurePathIsRelative(String path) {
ensurePathIsRelative(new File(path));
}


private static void ensurePathIsRelative(URI uri) {
ensurePathIsRelative(new File(uri));
}


private static void ensurePathIsRelative(File file) {
// Based on https://stackoverflow.com/questions/2375903/whats-the-best-way-to-defend-against-a-path-traversal-attack/34658355#34658355
String canonicalPath;
String absolutePath;

if (file.isAbsolute()) {
throw new RuntimeException("Potential directory traversal attempt - absolute path not allowed");
}

try {
canonicalPath = file.getCanonicalPath();
absolutePath = file.getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("Potential directory traversal attempt", e);
}

if (!canonicalPath.startsWith(absolutePath) || !canonicalPath.equals(absolutePath)) {
throw new RuntimeException("Potential directory traversal attempt");
}
}

private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
Expand Down
Loading