2019-10-16 07:49:58 +02:00
|
|
|
/*
|
2020-04-11 12:33:05 +02:00
|
|
|
* Copyright (C) 2010 Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
* Copyright (C) 2010 Connor Tumbleson <connor.tumbleson@gmail.com>
|
2012-09-20 03:19:12 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
2021-08-24 15:31:41 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2012-09-20 03:19:12 +02:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package brut.util;
|
|
|
|
|
2017-07-05 17:43:30 +02:00
|
|
|
import brut.common.BrutException;
|
|
|
|
import brut.common.InvalidUnknownFileException;
|
|
|
|
import brut.common.RootUnknownFileException;
|
|
|
|
import brut.common.TraversalUnknownFileException;
|
2012-09-20 03:19:12 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
2021-03-07 21:06:45 +01:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.zip.CRC32;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
public class BrutIO {
|
|
|
|
public static void copyAndClose(InputStream in, OutputStream out)
|
|
|
|
throws IOException {
|
|
|
|
try {
|
|
|
|
IOUtils.copy(in, out);
|
|
|
|
} finally {
|
2015-03-30 07:30:52 +02:00
|
|
|
IOUtils.closeQuietly(in);
|
|
|
|
IOUtils.closeQuietly(out);
|
2012-09-20 03:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long recursiveModifiedTime(File[] files) {
|
|
|
|
long modified = 0;
|
2021-08-26 19:00:37 +02:00
|
|
|
for (File file : files) {
|
|
|
|
long submodified = recursiveModifiedTime(file);
|
2012-09-20 03:19:12 +02:00
|
|
|
if (submodified > modified) {
|
|
|
|
modified = submodified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long recursiveModifiedTime(File file) {
|
|
|
|
long modified = file.lastModified();
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
File[] subfiles = file.listFiles();
|
2021-08-26 19:00:37 +02:00
|
|
|
for (File subfile : subfiles) {
|
|
|
|
long submodified = recursiveModifiedTime(subfile);
|
2012-09-20 03:19:12 +02:00
|
|
|
if (submodified > modified) {
|
|
|
|
modified = submodified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
2015-03-25 17:56:55 +01:00
|
|
|
|
2015-03-30 07:30:52 +02:00
|
|
|
public static CRC32 calculateCrc(InputStream input) throws IOException {
|
2015-03-25 17:56:55 +01:00
|
|
|
CRC32 crc = new CRC32();
|
2015-03-30 07:30:52 +02:00
|
|
|
int bytesRead;
|
|
|
|
byte[] buffer = new byte[8192];
|
2015-03-25 17:56:55 +01:00
|
|
|
while((bytesRead = input.read(buffer)) != -1) {
|
|
|
|
crc.update(buffer, 0, bytesRead);
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
2024-01-02 12:11:03 +01:00
|
|
|
public static String sanitizeFilepath(final File directory, final String entry) throws IOException, BrutException {
|
|
|
|
if (entry.isEmpty()) {
|
2020-12-10 13:57:00 +01:00
|
|
|
throw new InvalidUnknownFileException("Invalid Unknown File");
|
2017-07-05 17:43:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new File(entry).isAbsolute()) {
|
2020-12-10 13:57:00 +01:00
|
|
|
throw new RootUnknownFileException("Absolute Unknown Files is not allowed");
|
2017-07-05 17:43:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
final String canonicalDirPath = directory.getCanonicalPath() + File.separator;
|
|
|
|
final String canonicalEntryPath = new File(directory, entry).getCanonicalPath();
|
|
|
|
|
|
|
|
if (!canonicalEntryPath.startsWith(canonicalDirPath)) {
|
2020-12-10 13:57:00 +01:00
|
|
|
throw new TraversalUnknownFileException("Directory Traversal is not allowed");
|
2017-07-05 17:43:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://stackoverflow.com/q/2375903/455008
|
|
|
|
return canonicalEntryPath.substring(canonicalDirPath.length());
|
|
|
|
}
|
|
|
|
|
2024-01-02 12:11:03 +01:00
|
|
|
public static boolean detectPossibleDirectoryTraversal(String entry) {
|
2024-01-17 12:11:22 +01:00
|
|
|
return entry.contains("../") || entry.contains("/..") || entry.contains("..\\") || entry.contains("\\..");
|
2024-01-02 12:11:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String adaptSeparatorToUnix(String path) {
|
2018-04-05 22:24:58 +02:00
|
|
|
char separator = File.separatorChar;
|
|
|
|
|
|
|
|
if (separator != '/') {
|
|
|
|
return path.replace(separator, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:12:20 +02:00
|
|
|
public static void copy(File inputFile, ZipOutputStream outputFile) throws IOException {
|
2023-07-30 00:56:27 +02:00
|
|
|
try (FileInputStream fis = new FileInputStream(inputFile)) {
|
2015-04-16 15:12:20 +02:00
|
|
|
IOUtils.copy(fis, outputFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void copy(ZipFile inputFile, ZipOutputStream outputFile, ZipEntry entry) throws IOException {
|
2023-07-30 00:56:27 +02:00
|
|
|
try (InputStream is = inputFile.getInputStream(entry)) {
|
2015-04-16 15:12:20 +02:00
|
|
|
IOUtils.copy(is, outputFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
}
|