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

Fix MitM vulnerability #2132

Merged
merged 2 commits into from
Aug 25, 2021
Merged
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 @@ -123,14 +123,13 @@ public static String readFileToString(String filePath) throws IOException {
}
}

private static String validateFileName(String fileName, String targetDirectory) throws IOException {
File file = new File(fileName);
String canonicalPath = file.getCanonicalPath();
private static String validateFileName(String fileName, File destinationFolder) throws IOException {
String destinationFolderCanonicalPath = destinationFolder.getCanonicalPath();

File targetFile = new File(targetDirectory);
String targetCanonicalPath = targetFile.getCanonicalPath();
File file = new File(destinationFolderCanonicalPath, fileName);
String canonicalPath = file.getCanonicalPath();

if (!canonicalPath.startsWith(targetCanonicalPath)) {
if (!canonicalPath.startsWith(destinationFolderCanonicalPath)) {
throw new IllegalStateException("File is outside extraction target directory.");
}

Expand All @@ -151,12 +150,12 @@ public static void unzipFile(File zipFile, String destination) throws IOExceptio
if (destinationFolder.exists()) {
deleteFileOrFolderSilently(destinationFolder);
}

destinationFolder.mkdirs();

byte[] buffer = new byte[WRITE_BUFFER_SIZE];
while ((entry = zipStream.getNextEntry()) != null) {
String fileName = validateFileName(entry.getName(), ".");
String fileName = validateFileName(entry.getName(), destinationFolder);
File file = new File(destinationFolder, fileName);
if (entry.isDirectory()) {
file.mkdirs();
Expand Down