@@ -35,7 +35,10 @@ public class GXCompressor implements IGXCompressor {
35
35
private static final String UNSUPPORTED_FORMAT = " is an unsupported format. Supported formats are zip, 7z, tar, gz and jar." ;
36
36
private static final String EMPTY_FILE = "The selected file is empty: " ;
37
37
private static final String DIRECTORY_ATTACK = "Potential directory traversal attack detected: " ;
38
- private static final String MAX_FILESIZE_EXCEEDED = "The files selected for compression exceed the maximum permitted file size of " ;
38
+ private static final String MAX_FILESIZE_EXCEEDED = "The file(s) selected for (de)compression exceed the maximum permitted file size of " ;
39
+ private static final String TOO_MANY_FILES = "Too many files have been added for (de)compression. Maximum allowed is " ;
40
+ private static final String ZIP_SLIP_DETECTED = "Zip slip or path traversal attack detected in archive: " ;
41
+ private static final int MAX_FILES_ALLOWED = 1000 ;
39
42
40
43
private static void storageMessages (String error , GXBaseCollection <SdtMessages_Message > messages ) {
41
44
try {
@@ -55,6 +58,12 @@ public static Boolean compress(ArrayList<String> files, String path, long maxCom
55
58
storageMessages (NO_FILES_ADDED , messages [0 ]);
56
59
return false ;
57
60
}
61
+ if (maxCombinedFileSize > -1 && files .size () > MAX_FILES_ALLOWED ){
62
+ log .error (TOO_MANY_FILES + MAX_FILES_ALLOWED );
63
+ storageMessages (TOO_MANY_FILES + MAX_FILES_ALLOWED , messages [0 ]);
64
+ files .clear ();
65
+ return false ;
66
+ }
58
67
long totalSize = 0 ;
59
68
File [] toCompress = new File [files .size ()];
60
69
int index = 0 ;
@@ -67,18 +76,18 @@ public static Boolean compress(ArrayList<String> files, String path, long maxCom
67
76
storageMessages (FILE_NOT_EXISTS + filePath , messages [0 ]);
68
77
continue ;
69
78
}
70
- if (normalizedPath .contains (File .separator + ".." + File .separator ) ||
71
- normalizedPath .endsWith (File .separator + ".." ) ||
72
- normalizedPath .startsWith (".." + File .separator )) {
79
+ if (!normalizedPath .equals (file .getAbsolutePath ())) {
73
80
log .error (DIRECTORY_ATTACK + "{}" , filePath );
74
81
storageMessages (DIRECTORY_ATTACK + filePath , messages [0 ]);
75
82
return false ;
76
83
}
77
84
long fileSize = file .length ();
78
85
totalSize += fileSize ;
79
- if (totalSize > maxCombinedFileSize && maxCombinedFileSize > - 1 ) {
80
- log .error (MAX_FILESIZE_EXCEEDED + "{}" , maxCombinedFileSize );
86
+ if (maxCombinedFileSize > - 1 && totalSize > maxCombinedFileSize ) {
87
+ log .error (MAX_FILESIZE_EXCEEDED + maxCombinedFileSize );
81
88
storageMessages (MAX_FILESIZE_EXCEEDED + maxCombinedFileSize , messages [0 ]);
89
+ toCompress = null ;
90
+ files .clear ();
82
91
return false ;
83
92
}
84
93
toCompress [index ++] = file ;
@@ -133,6 +142,29 @@ public static Boolean decompress(String file, String path, GXBaseCollection<SdtM
133
142
storageMessages (EMPTY_FILE + file , messages [0 ]);
134
143
return false ;
135
144
}
145
+ try {
146
+ int fileCount = CompressionUtils .countArchiveEntries (toCompress );
147
+ if (fileCount > MAX_FILES_ALLOWED ) {
148
+ log .error (TOO_MANY_FILES + fileCount );
149
+ storageMessages (TOO_MANY_FILES + fileCount , messages [0 ]);
150
+ return false ;
151
+ }
152
+ } catch (Exception e ) {
153
+ log .error ("Error counting archive entries for file: {}" , file , e );
154
+ storageMessages ("Error counting archive entries for file: " + file , messages [0 ]);
155
+ return false ;
156
+ }
157
+ try {
158
+ if (!CompressionUtils .isArchiveSafe (toCompress , path )) {
159
+ log .error (ZIP_SLIP_DETECTED + file );
160
+ storageMessages (ZIP_SLIP_DETECTED + file , messages [0 ]);
161
+ return false ;
162
+ }
163
+ } catch (Exception e ) {
164
+ log .error ("Error checking archive safety for file: {}" , file , e );
165
+ storageMessages ("Error checking archive safety for file: " + file , messages [0 ]);
166
+ return false ;
167
+ }
136
168
String extension = getExtension (toCompress .getName ());
137
169
try {
138
170
switch (extension .toLowerCase ()) {
@@ -636,4 +668,4 @@ private static void decompressJar(File archive, String directory) throws IOExcep
636
668
}
637
669
}
638
670
}
639
- }
671
+ }
0 commit comments