Skip to content

Commit f20b1e9

Browse files
sgramponeBeta Bot
authored andcommitted
Revert branch 'genexuslabs:fix/gxcompress-abitrary-file-access' into beta
1 parent 3d68d56 commit f20b1e9

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,20 +636,26 @@ 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+
639648
private static void decompressTar(File archive, String directory) throws IOException {
640649
byte[] buffer = new byte[BUFFER_SIZE];
641-
final Path targetDir = Paths.get(directory).toAbsolutePath().normalize();
642650
try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
643651
TarArchiveEntry entry;
644652
while ((entry = tis.getNextEntry()) != null) {
645-
Path entryPath = targetDir.resolve(entry.getName()).normalize();
646-
if(!entryPath.startsWith(targetDir))
653+
if(isPathTraversal(directory, entry.getName()))
647654
{
648655
log.error(DIRECTORY_ATTACK + "{}", entry.getName());
649656
return;
650657
}else {
651-
652-
File newFile = entryPath.toFile();
658+
File newFile = new File(directory, entry.getName());
653659

654660
if (entry.isDirectory()) {
655661
if (!newFile.isDirectory() && !newFile.mkdirs()) {

0 commit comments

Comments
 (0)