mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2024-12-31 00:08:08 +01:00
(CodeQL) Fixed finding: "Arbitrary file access during archive extraction ("Zip Slip")
" (#2344) (CodeQL) Fixed finding: "Arbitrary file access during archive extraction ("Zip Slip") " Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
This commit is contained in:
parent
212e521238
commit
d832a90de0
@ -105,7 +105,7 @@ public class FileToPdf {
|
|||||||
new ByteArrayInputStream(Files.readAllBytes(zipFilePath)))) {
|
new ByteArrayInputStream(Files.readAllBytes(zipFilePath)))) {
|
||||||
ZipEntry entry = zipIn.getNextEntry();
|
ZipEntry entry = zipIn.getNextEntry();
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
Path filePath = tempUnzippedDir.resolve(entry.getName());
|
Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName()));
|
||||||
if (!entry.isDirectory()) {
|
if (!entry.isDirectory()) {
|
||||||
Files.createDirectories(filePath.getParent());
|
Files.createDirectories(filePath.getParent());
|
||||||
if (entry.getName().toLowerCase().endsWith(".html")
|
if (entry.getName().toLowerCase().endsWith(".html")
|
||||||
@ -175,7 +175,7 @@ public class FileToPdf {
|
|||||||
ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) {
|
ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) {
|
||||||
ZipEntry entry = zipIn.getNextEntry();
|
ZipEntry entry = zipIn.getNextEntry();
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
Path filePath = tempDirectory.resolve(entry.getName());
|
Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName()));
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
Files.createDirectories(filePath); // Explicitly create the directory structure
|
Files.createDirectories(filePath); // Explicitly create the directory structure
|
||||||
} else {
|
} else {
|
||||||
@ -241,4 +241,14 @@ public class FileToPdf {
|
|||||||
Files.deleteIfExists(tempOutputFile);
|
Files.deleteIfExists(tempOutputFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String sanitizeZipFilename(String entryName) {
|
||||||
|
if (entryName == null || entryName.trim().isEmpty()) {
|
||||||
|
return entryName;
|
||||||
|
}
|
||||||
|
while (entryName.contains("../") || entryName.contains("..\\")) {
|
||||||
|
entryName = entryName.replace("../", "").replace("..\\", "");
|
||||||
|
}
|
||||||
|
return entryName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user