Skip to content

Commit c2d4984

Browse files
Add check for DoS attack and clear error messages
1 parent 7a6d936 commit c2d4984

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public class GXCompressor implements IGXCompressor {
3434
private static final String GENERIC_ERROR = "An error occurred during the compression/decompression process: ";
3535
private static final String NO_FILES_ADDED = "No files have been added for compression.";
3636
private static final String FILE_NOT_EXISTS = "File does not exist: ";
37-
private static final String FOLDER_NOT_EXISTS = "The specified folder does not exist: ";
38-
private static final String UNSUPPORTED_FORMAT = "Unsupported compression/decompression format: ";
37+
private static final String UNSUPPORTED_FORMAT = " is an unsupported format. Supported formats are zip, 7z, tar, gz and jar.";
3938
private static final String EMPTY_FILE = "The selected file is empty: ";
39+
private static final String DIRECTORY_ATTACK = "Potential directory traversal attack detected: ";
40+
private static final String MAX_FILESIZE_EXCEEDED = "The files selected for compression exceed the maximum permitted file size of ";
4041

4142
private static void storageMessages(String error, GXBaseCollection<SdtMessages_Message> messages) {
4243
try {
@@ -48,15 +49,15 @@ private static void storageMessages(String error, GXBaseCollection<SdtMessages_M
4849
} catch (Exception e) {
4950
log.error("Failed to store the following error message: {}", error, e);
5051
}
51-
5252
}
5353

54-
public static Boolean compress(ArrayList<String> files, String path, GXBaseCollection<SdtMessages_Message>[] messages) {
54+
public static Boolean compress(ArrayList<String> files, String path, long maxCombinedFileSize, GXBaseCollection<SdtMessages_Message>[] messages) {
5555
if (files.isEmpty()){
5656
log.error(NO_FILES_ADDED);
5757
storageMessages(NO_FILES_ADDED, messages[0]);
5858
return false;
5959
}
60+
long totalSize = 0;
6061
File[] toCompress = new File[files.size()];
6162
int index = 0;
6263
for (String filePath : files) {
@@ -71,8 +72,16 @@ public static Boolean compress(ArrayList<String> files, String path, GXBaseColle
7172
if (normalizedPath.contains(File.separator + ".." + File.separator) ||
7273
normalizedPath.endsWith(File.separator + "..") ||
7374
normalizedPath.startsWith(".." + File.separator)) {
74-
log.warn("Potential directory traversal attack detected: {}", filePath);
75-
continue;
75+
log.error(DIRECTORY_ATTACK + "{}", filePath);
76+
storageMessages(DIRECTORY_ATTACK + filePath, messages[0]);
77+
return false;
78+
}
79+
long fileSize = file.length();
80+
totalSize += fileSize;
81+
if (totalSize > maxCombinedFileSize) {
82+
log.error(MAX_FILESIZE_EXCEEDED + "{}", maxCombinedFileSize);
83+
storageMessages(MAX_FILESIZE_EXCEEDED + maxCombinedFileSize, messages[0]);
84+
return false;
7685
}
7786
toCompress[index++] = file;
7887
} catch (IOException e) {
@@ -98,13 +107,11 @@ public static Boolean compress(ArrayList<String> files, String path, GXBaseColle
98107
compressToJar(toCompress, path);
99108
break;
100109
default:
110+
log.error("{}" + UNSUPPORTED_FORMAT, format);
111+
storageMessages(format + UNSUPPORTED_FORMAT, messages[0]);
101112
return false;
102113
}
103114
return true;
104-
} catch (IllegalArgumentException iae) {
105-
log.error("{}{}. Supported compression formats are zip, 7z, tar, gz and jar", UNSUPPORTED_FORMAT, format, iae);
106-
storageMessages(UNSUPPORTED_FORMAT + format, messages[0]);
107-
return false;
108115
} catch (Exception e) {
109116
log.error(GENERIC_ERROR, e);
110117
storageMessages(e.getMessage(), messages[0]);
@@ -147,8 +154,8 @@ public static Boolean decompress(String file, String path, GXBaseCollection<SdtM
147154
decompressJar(toCompress, path);
148155
break;
149156
default:
150-
log.error("{}{}. Supported decompression formats are zip, 7z, tar, gz, jar", UNSUPPORTED_FORMAT, extension);
151-
storageMessages( UNSUPPORTED_FORMAT + extension, messages[0]);
157+
log.error("{}" + UNSUPPORTED_FORMAT, extension);
158+
storageMessages(extension + UNSUPPORTED_FORMAT, messages[0]);
152159
return false;
153160
}
154161
return true;

0 commit comments

Comments
 (0)