Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 484923e

Browse files
committed
update test files
1 parent be03e58 commit 484923e

3 files changed

Lines changed: 60 additions & 53 deletions

File tree

java/ql/test/experimental/query-tests/security/CWE-522-DecompressionBombs/src/main/java/com/Bombs/CommonsCompressHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void commonsCompressorInputStream(InputStream inputStream) throws
4242
new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(in)) {
4343
File f = new File("tmpfile");
4444
try (OutputStream o = Files.newOutputStream(f.toPath())) {
45-
IOUtils.copy(gzIn2, o);
45+
IOUtils.copy(gzIn2, o); // BAD
4646
}
4747
} catch (IOException e) {
4848
throw new RuntimeException(e);
@@ -63,7 +63,7 @@ static void commonsCompressArchiveInputStream(InputStream inputStream) throws Ar
6363
}
6464
File f = new File("tmpfile");
6565
try (OutputStream o = Files.newOutputStream(f.toPath())) {
66-
IOUtils.copy(zipInputStream, o);
66+
IOUtils.copy(zipInputStream, o); // BAD
6767
}
6868
}
6969
} catch (IOException e) {
@@ -83,7 +83,7 @@ static void commonsCompressArchiveInputStream2(InputStream inputStream) {
8383
File f = new File("tmpfile");
8484
try (OutputStream outputStream = new FileOutputStream(f)) {
8585
int readLen;
86-
while ((readLen = zipInputStream.read(readBuffer)) != -1) {
86+
while ((readLen = zipInputStream.read(readBuffer)) != -1) { // BAD
8787
outputStream.write(readBuffer, 0, readLen);
8888
}
8989
}
@@ -106,7 +106,7 @@ static void commonsCompressArchiveStreamFactory(InputStream inputStream)
106106
File f = new File("tmpfile");
107107
try (OutputStream outputStream = new FileOutputStream(f)) {
108108
int readLen;
109-
while ((readLen = zipInputStream.read(readBuffer)) != -1) {
109+
while ((readLen = zipInputStream.read(readBuffer)) != -1) { // BAD
110110
outputStream.write(readBuffer, 0, readLen);
111111
}
112112
}
@@ -121,7 +121,7 @@ static void commonsCompressCompressorStreamFactory(InputStream inputStream)
121121
int buffersize = 4096;
122122
final byte[] buffer = new byte[buffersize];
123123
int n = 0;
124-
while (-1 != (n = in.read(buffer))) {
124+
while (-1 != (n = in.read(buffer))) { // BAD
125125
out.write(buffer, 0, n);
126126
}
127127
out.close();

java/ql/test/experimental/query-tests/security/CWE-522-DecompressionBombs/src/main/java/com/Bombs/HelloServlet.java

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.commons.compress.archivers.ArchiveException;
44
import org.apache.commons.compress.compressors.CompressorException;
5+
import org.apache.commons.io.IOUtils;
56

67
import static com.Bombs.CommonsCompressHandler.*;
78
import static com.Bombs.SnappyHandler.*;
@@ -21,57 +22,63 @@
2122
import javax.servlet.http.Part;
2223

2324
@WebServlet(
24-
name = "helloServlet",
25-
urlPatterns = {"/hello-servlet"})
25+
name = "helloServlet",
26+
urlPatterns = {"/hello-servlet"})
2627
@MultipartConfig()
2728
public class HelloServlet extends HttpServlet {
2829

29-
public void init() {}
30-
31-
public void doPost(HttpServletRequest request, HttpServletResponse response)
32-
throws IOException, ServletException {
33-
response.setContentType("text/html");
34-
Part remoteFile = request.getPart("zipFile");
35-
// Zip
36-
ZipInputStreamSafe2(remoteFile.getInputStream());
37-
ZipInputStreamSafe(request.getPart("zipFile").getInputStream());
38-
ZipInputStreamUnsafe(remoteFile.getInputStream());
39-
GZipInputStreamUnsafe(request.getPart("zipFile").getInputStream());
40-
InflaterInputStreamUnsafe(request.getPart("zipFile").getInputStream());
41-
try {
42-
InflaterUnsafe(request.getParameter("data").getBytes(StandardCharsets.UTF_8));
43-
} catch (DataFormatException e) {
44-
throw new RuntimeException(e);
45-
}
46-
try {
47-
ZipFile1(request.getParameter("zipFileName"));
48-
} catch (DataFormatException e) {
49-
throw new RuntimeException(e);
30+
public void init() {
5031
}
5132

52-
// Zip4j
53-
zip4jZipInputStream(remoteFile.getInputStream());
54-
zip4jZipInputStreamSafe(remoteFile.getInputStream());
55-
// SnappyZip
56-
SnappyZipInputStream(remoteFile.getInputStream());
57-
// apache Commons
58-
commonsCompressArchiveInputStream2(remoteFile.getInputStream());
59-
commonsCompressorInputStream(remoteFile.getInputStream());
60-
try {
61-
commonsCompressArchiveInputStream(remoteFile.getInputStream());
62-
commonsCompressArchiveStreamFactory(remoteFile.getInputStream());
63-
} catch (ArchiveException e) {
64-
throw new RuntimeException(e);
65-
}
66-
try {
67-
commonsCompressCompressorStreamFactory(remoteFile.getInputStream());
68-
} catch (CompressorException e) {
69-
throw new RuntimeException(e);
70-
}
33+
public void doPost(HttpServletRequest request, HttpServletResponse response)
34+
throws IOException, ServletException {
35+
response.setContentType("text/html");
36+
Part remoteFile = request.getPart("zipFile");
37+
// Zip
38+
ZipInputStreamSafe2(remoteFile.getInputStream());
39+
ZipInputStreamSafe(request.getPart("zipFile").getInputStream());
40+
ZipInputStreamUnsafe(remoteFile.getInputStream());
41+
GZipInputStreamUnsafe(request.getPart("zipFile").getInputStream());
42+
InflaterInputStreamUnsafe(request.getPart("zipFile").getInputStream());
43+
try {
44+
InflaterUnsafe(request.getParameter("data").getBytes(StandardCharsets.UTF_8));
45+
} catch (DataFormatException e) {
46+
throw new RuntimeException(e);
47+
}
48+
try {
49+
ZipFile1(request.getParameter("zipFileName"));
50+
} catch (DataFormatException e) {
51+
throw new RuntimeException(e);
52+
}
7153

72-
PrintWriter out = response.getWriter();
73-
out.println("<html><body>end</body></html>");
74-
}
54+
// Zip4j
55+
zip4jZipInputStream(remoteFile.getInputStream());
56+
zip4jZipInputStreamSafe(remoteFile.getInputStream());
57+
// SnappyZip
58+
SnappyZipInputStream(remoteFile.getInputStream());
59+
// apache Commons
60+
commonsCompressArchiveInputStream2(remoteFile.getInputStream());
61+
commonsCompressorInputStream(remoteFile.getInputStream());
62+
try {
63+
commonsCompressArchiveInputStream(remoteFile.getInputStream());
64+
commonsCompressArchiveStreamFactory(remoteFile.getInputStream());
65+
} catch (ArchiveException e) {
66+
throw new RuntimeException(e);
67+
}
68+
try {
69+
commonsCompressCompressorStreamFactory(remoteFile.getInputStream());
70+
} catch (CompressorException e) {
71+
throw new RuntimeException(e);
72+
}
7573

76-
public void destroy() {}
74+
// FP
75+
String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
76+
byte[] inputStreamResult = request.getInputStream().readAllBytes();
77+
78+
PrintWriter out = response.getWriter();
79+
out.println("<html><body>end</body></html>");
80+
}
81+
82+
public void destroy() {
83+
}
7784
}

java/ql/test/experimental/query-tests/security/CWE-522-DecompressionBombs/src/main/java/com/Bombs/ZipHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ public static void ZipFile1(String zipFilePath) throws DataFormatException, IOEx
162162

163163
try (InputStream inputStream = zipFile.getInputStream(entry);
164164
FileOutputStream outputStream = new FileOutputStream(destPath); ) {
165-
int data = inputStream.read();
165+
int data = inputStream.read(); // BAD
166166
while (data != -1) {
167167
outputStream.write(data);
168-
data = inputStream.read();
168+
data = inputStream.read(); // BAD
169169
}
170170
}
171171
System.out.println("file : " + entry.getName() + " => " + destPath);

0 commit comments

Comments
 (0)