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

Vulnerability fix (powered by Mobb Autofixer) #14

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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;

/**
* MD5 hash generator. More information about this class is available from <a target="_top" href=
Expand Down Expand Up @@ -46,6 +47,7 @@ public static void main(String[] args) {
} else {
for (String element : args) {
try {
ensurePathIsRelative(element);
System.out.println(MD5.getHashString(new File(element)) + " " + element);
} catch (IOException x) {
System.err.println(x.getMessage());
Expand All @@ -54,6 +56,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");
}
}

/**
* Gets this hash sum as an array of 16 bytes.
*
Expand Down