22
22
23
23
public class CompressionUtils {
24
24
25
+ private static final int BUFFER_SIZE = 8192 ;
26
+
25
27
/**
26
28
* Counts the number of entries in an archive file.
27
29
*
@@ -39,7 +41,7 @@ public static int countArchiveEntries(File archiveFile) throws IOException {
39
41
return zipFile .size ();
40
42
}
41
43
case "7z" :
42
- try (SevenZFile sevenZFile = getSevenZFile (archiveFile . getAbsolutePath () )) {
44
+ try (SevenZFile sevenZFile = getSevenZFile (archiveFile )) {
43
45
while (sevenZFile .getNextEntry () != null ) {
44
46
count ++;
45
47
}
@@ -81,21 +83,17 @@ public static boolean isArchiveSafe(File archiveFile, String targetDir) throws I
81
83
Enumeration <? extends ZipEntry > entries = zipFile .entries ();
82
84
while (entries .hasMoreElements ()) {
83
85
ZipEntry entry = entries .nextElement ();
84
- File destinationFile = new File (targetPath , entry .getName ()).getCanonicalFile ();
85
- if (!destinationFile .getPath ().startsWith (targetPath .getPath () + File .separator ) &&
86
- !destinationFile .getPath ().equals (targetPath .getPath ())) {
86
+ if (!isEntryPathSafe (targetPath , entry .getName ())) {
87
87
return false ;
88
88
}
89
89
}
90
90
}
91
91
return true ;
92
92
case "7z" :
93
- try (SevenZFile sevenZFile = getSevenZFile (archiveFile . getAbsolutePath () )) {
93
+ try (SevenZFile sevenZFile = getSevenZFile (archiveFile )) {
94
94
SevenZArchiveEntry entry ;
95
95
while ((entry = sevenZFile .getNextEntry ()) != null ) {
96
- File destinationFile = new File (targetPath , entry .getName ()).getCanonicalFile ();
97
- if (!destinationFile .getPath ().startsWith (targetPath .getPath () + File .separator ) &&
98
- !destinationFile .getPath ().equals (targetPath .getPath ())) {
96
+ if (!isEntryPathSafe (targetPath , entry .getName ())) {
99
97
return false ;
100
98
}
101
99
}
@@ -105,9 +103,7 @@ public static boolean isArchiveSafe(File archiveFile, String targetDir) throws I
105
103
try (TarArchiveInputStream tarStream = new TarArchiveInputStream (Files .newInputStream (archiveFile .toPath ()))) {
106
104
TarArchiveEntry entry ;
107
105
while ((entry = tarStream .getNextEntry ()) != null ) {
108
- File destinationFile = new File (targetPath , entry .getName ()).getCanonicalFile ();
109
- if (!destinationFile .getPath ().startsWith (targetPath .getPath () + File .separator ) &&
110
- !destinationFile .getPath ().equals (targetPath .getPath ())) {
106
+ if (!isEntryPathSafe (targetPath , entry .getName ())) {
111
107
return false ;
112
108
}
113
109
}
@@ -117,19 +113,15 @@ public static boolean isArchiveSafe(File archiveFile, String targetDir) throws I
117
113
String fileName = archiveFile .getName ();
118
114
if (fileName .endsWith (".gz" ) && fileName .length () > 3 ) {
119
115
String extractedName = fileName .substring (0 , fileName .length () - 3 );
120
- File destinationFile = new File (targetPath , extractedName ).getCanonicalFile ();
121
- return destinationFile .getPath ().startsWith (targetPath .getPath () + File .separator ) ||
122
- destinationFile .getPath ().equals (targetPath .getPath ());
116
+ return isEntryPathSafe (targetPath , extractedName );
123
117
}
124
118
return true ;
125
119
case "jar" :
126
120
try (JarFile jarFile = new JarFile (archiveFile )) {
127
121
Enumeration <JarEntry > entries = jarFile .entries ();
128
122
while (entries .hasMoreElements ()) {
129
123
JarEntry entry = entries .nextElement ();
130
- File destinationFile = new File (targetPath , entry .getName ()).getCanonicalFile ();
131
- if (!destinationFile .getPath ().startsWith (targetPath .getPath () + File .separator ) &&
132
- !destinationFile .getPath ().equals (targetPath .getPath ())) {
124
+ if (!isEntryPathSafe (targetPath , entry .getName ())) {
133
125
return false ;
134
126
}
135
127
}
@@ -164,7 +156,7 @@ public static long getMaxFileSize(File archiveFile) throws IOException {
164
156
}
165
157
break ;
166
158
case "7z" :
167
- try (SevenZFile sevenZFile = getSevenZFile (archiveFile . getAbsolutePath () )) {
159
+ try (SevenZFile sevenZFile = getSevenZFile (archiveFile )) {
168
160
SevenZArchiveEntry entry ;
169
161
while ((entry = sevenZFile .getNextEntry ()) != null ) {
170
162
if (!entry .isDirectory () && entry .getSize () > maxSize ) {
@@ -185,7 +177,7 @@ public static long getMaxFileSize(File archiveFile) throws IOException {
185
177
break ;
186
178
case "gz" :
187
179
try (GZIPInputStream gzStream = new GZIPInputStream (Files .newInputStream (archiveFile .toPath ()))) {
188
- byte [] buffer = new byte [8192 ];
180
+ byte [] buffer = new byte [BUFFER_SIZE ];
189
181
long size = 0 ;
190
182
int n ;
191
183
while ((n = gzStream .read (buffer )) != -1 ) {
@@ -241,7 +233,7 @@ public static long estimateDecompressedSize(File archiveFile) throws IOException
241
233
}
242
234
break ;
243
235
case "7z" :
244
- try (SevenZFile sevenZFile = getSevenZFile (archiveFile . getAbsolutePath () )) {
236
+ try (SevenZFile sevenZFile = getSevenZFile (archiveFile )) {
245
237
SevenZArchiveEntry entry ;
246
238
while ((entry = sevenZFile .getNextEntry ()) != null ) {
247
239
if (!entry .isDirectory ()) {
@@ -304,7 +296,12 @@ public static long estimateDecompressedSize(File archiveFile) throws IOException
304
296
return totalSize ;
305
297
}
306
298
307
- private static SevenZFile getSevenZFile (final String specialPath ) throws IOException {
308
- return SevenZFile .builder ().setFile (getFile (specialPath )).get ();
299
+ private static SevenZFile getSevenZFile (File archiveFile ) throws IOException {
300
+ return SevenZFile .builder ().setFile (archiveFile ).get ();
301
+ }
302
+
303
+ private static boolean isEntryPathSafe (File targetPath , String entryName ) throws IOException {
304
+ File destinationFile = new File (targetPath , entryName ).getCanonicalFile ();
305
+ return destinationFile .getPath ().startsWith (targetPath .getPath () + File .separator ) || destinationFile .getPath ().equals (targetPath .getPath ());
309
306
}
310
307
}
0 commit comments