-
Notifications
You must be signed in to change notification settings - Fork 725
Expand file tree
/
Copy pathZipEntryCheck.java
More file actions
161 lines (128 loc) · 5.45 KB
/
ZipEntryCheck.java
File metadata and controls
161 lines (128 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package checks.security;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarFile;
public class ZipEntryCheck {
ZipFile zipFile;
Enumeration<? extends ZipEntry> entries = zipFile.entries(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
public void noncompliant() throws Exception {
File f = new File("/home/eric/testzipbomb/zbsm.zip");
ZipFile zipFile = new ZipFile(f);
Enumeration<? extends ZipEntry> entries = zipFile.entries(); // Noncompliant
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
if (ze.getSize() > 555) { // Noncompliant
// die
}
}
}
public ZipEntry noncompliant2(ZipInputStream zis) throws Exception {
return zis.getNextEntry(); // Noncompliant
}
public JarEntry noncompliant3(JarFile file) {
return file.entries().nextElement(); // Noncompliant
// ^^^^^^^
}
public String compliant() throws java.lang.Exception {
File f = new File("/home/eric/testzipbomb/zbsm.zip");
ZipFile zipFile = new ZipFile(f);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
int THRESHOLD_ENTRIES = 10000;
int THRESHOLD_SIZE = 1000000000; // 1 GB
double THRESHOLD_RATIO = 10;
int totalSizeArchive = 0;
int totalEntryArchive = 0;
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
InputStream in = new BufferedInputStream(zipFile.getInputStream(ze)); // Compliant
OutputStream out = new BufferedOutputStream(new FileOutputStream("./output_onlyfortesting.txt"));
totalEntryArchive++;
int nBytes = -1;
byte[] buffer = new byte[2048];
int totalSizeEntry = 0;
while ((nBytes = in.read(buffer)) > 0) {
out.write(buffer, 0, nBytes);
totalSizeEntry += nBytes;
totalSizeArchive += nBytes;
double compressionRatio = totalSizeEntry / ze.getCompressedSize();
if (compressionRatio > THRESHOLD_RATIO) {
// ratio between compressed and uncompressed data is highly suspicious, looks like a Zip Bomb Attack
break;
}
}
if (totalSizeArchive > THRESHOLD_SIZE) {
// the uncompressed data size is too much for the application resource capacity
break;
}
if (totalEntryArchive > THRESHOLD_ENTRIES) {
// too much entries in this archive, can lead to inodes exhaustion of the system
break;
}
}
return "thymeleaf/welcome";
}
}
class TarUtilities {
private TarUtilities() {
/* This utility class should not be instantiated */
}
public static List<TarArchiveEntry> getAllEntries(TarFile file) {
return file.getEntries(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^^^^
}
public static Optional<TarArchiveEntry> getNext(TarArchiveInputStream stream) throws IOException {
return Optional.of(stream.getNextEntry()); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^^^^^^
}
public static long getEntrySize(TarArchiveEntry entry) {
return entry.getSize(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^
}
}
class SevenZUtilities {
private SevenZUtilities() {
/* This utility class should not be instantiated */
}
public static Iterable<SevenZArchiveEntry> getAllEntries(SevenZFile file) {
return file.getEntries(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^^^^
}
public static long getEntrySize(SevenZArchiveEntry entry) {
return entry.getSize(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^
}
}
class ApacheCommonsZipUtilities {
private ApacheCommonsZipUtilities() {
/* This utility class should not be instantiated */
}
public static Enumeration<org.apache.commons.compress.archivers.zip.ZipArchiveEntry> getAllEntries(org.apache.commons.compress.archivers.zip.ZipFile file) {
return file.getEntries(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^^^^
}
public static Optional<org.apache.commons.compress.archivers.zip.ZipArchiveEntry> getNext(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream stream) throws IOException {
return Optional.of(stream.getNextEntry()); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^^^^^^
}
public static long getEntrySize(org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry) {
return entry.getSize(); // Noncompliant {{Make sure that expanding this archive file is safe here.}}
// ^^^^^^^
}
}