@@ -85,7 +85,7 @@ public static Path unzip(Path zip, Path toDir, Predicate<ZipEntry> filter) throw
8585 if (filter .test (entry )) {
8686 var target = toDir .resolve (entry .getName ());
8787
88- verifyInsideTargetDirectory (entry , target , targetDirNormalizedPath );
88+ verifyInsideTargetDirectory (entry . getName () , target , targetDirNormalizedPath );
8989
9090 if (entry .isDirectory ()) {
9191 throwExceptionIfDirectoryIsNotCreatable (target );
@@ -100,10 +100,10 @@ public static Path unzip(Path zip, Path toDir, Predicate<ZipEntry> filter) throw
100100 }
101101 }
102102
103- private static void verifyInsideTargetDirectory (ZipEntry entry , Path entryPath , Path targetDirNormalizedPath ) {
103+ private static void verifyInsideTargetDirectory (String entryName , Path entryPath , Path targetDirNormalizedPath ) {
104104 if (!entryPath .normalize ().startsWith (targetDirNormalizedPath )) {
105105 // vulnerability - trying to create a file outside the target directory
106- throw new IllegalStateException ("Unzipping an entry outside the target directory is not allowed: " + entry . getName () );
106+ throw new IllegalStateException ("Extracting an entry outside the target directory is not allowed: " + entryName );
107107 }
108108 }
109109
@@ -123,6 +123,7 @@ private static void copy(ZipFile zipFile, ZipEntry entry, Path to) throws IOExce
123123 }
124124
125125 public static void extractTarGz (Path compressedFile , Path targetDir ) throws IOException {
126+ Path targetDirNormalizedPath = targetDir .normalize ();
126127 try (InputStream fis = Files .newInputStream (compressedFile );
127128 InputStream bis = new BufferedInputStream (fis );
128129 InputStream gzis = new GzipCompressorInputStream (bis );
@@ -132,18 +133,21 @@ public static void extractTarGz(Path compressedFile, Path targetDir) throws IOEx
132133 if (!tarArchiveInputStream .canReadEntryData (targzEntry )) {
133134 continue ;
134135 }
135- var entry = targetDir .resolve (targzEntry .getName ());
136+ var target = targetDir .resolve (targzEntry .getName ());
137+
138+ verifyInsideTargetDirectory (targzEntry .getName (), target , targetDirNormalizedPath );
139+
136140 if (targzEntry .isDirectory ()) {
137- Files .createDirectories (entry );
141+ Files .createDirectories (target );
138142 } else {
139- if (!Files .isDirectory (entry .getParent ())) {
140- Files .createDirectories (entry .getParent ());
143+ if (!Files .isDirectory (target .getParent ())) {
144+ Files .createDirectories (target .getParent ());
141145 }
142- Files .copy (tarArchiveInputStream , entry , StandardCopyOption .REPLACE_EXISTING );
146+ Files .copy (tarArchiveInputStream , target , StandardCopyOption .REPLACE_EXISTING );
143147 int mode = targzEntry .getMode ();
144148 if (mode != 0 && !IS_OS_WINDOWS ) {
145149 Set <PosixFilePermission > permissions = fromFileMode (mode );
146- Files .setPosixFilePermissions (entry , permissions );
150+ Files .setPosixFilePermissions (target , permissions );
147151 }
148152 }
149153 }
0 commit comments