Skip to content

Commit 9063ef4

Browse files
committed
Try with other methods
1 parent b62f16c commit 9063ef4

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

gxcompress/src/main/java/com/genexus/compression/GXCompressor.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -636,26 +636,20 @@ private static void decompress7z(File archive, String directory) throws IOExcept
636636
}
637637
}
638638

639-
private static boolean isPathTraversal(String dir, String fName) {
640-
try {
641-
Path path = Paths.get(dir).resolve(fName);
642-
return !path.toAbsolutePath().equals(path.toRealPath());
643-
}catch (Exception e){
644-
return true;
645-
}
646-
}
647-
648639
private static void decompressTar(File archive, String directory) throws IOException {
649640
byte[] buffer = new byte[BUFFER_SIZE];
641+
final Path targetDir = Paths.get(directory).toAbsolutePath().normalize();
650642
try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
651643
TarArchiveEntry entry;
652644
while ((entry = tis.getNextEntry()) != null) {
653-
if(isPathTraversal(directory, entry.getName()))
645+
Path entryPath = targetDir.resolve(entry.getName()).normalize();
646+
if(!entryPath.startsWith(targetDir))
654647
{
655648
log.error(DIRECTORY_ATTACK + "{}", entry.getName());
656649
return;
657650
}else {
658-
File newFile = new File(directory, entry.getName());
651+
652+
File newFile = entryPath.toFile();
659653

660654
if (entry.isDirectory()) {
661655
if (!newFile.isDirectory() && !newFile.mkdirs()) {

0 commit comments

Comments
 (0)