forked from DTStack/Taier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipUtilTest.java
More file actions
141 lines (122 loc) · 5.27 KB
/
Copy pathZipUtilTest.java
File metadata and controls
141 lines (122 loc) · 5.27 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dtstack.taier.common.util;
import com.dtstack.taier.common.exception.TaierDefineException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtilTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void testUpzipFile() throws Exception {
File zipFile = temporaryFolder.newFile("normal.zip");
writeZip(zipFile, new ZipItem("conf/core-site.xml", "content"));
File targetDir = temporaryFolder.newFolder("normal");
List<File> files = ZipUtil.upzipFile(zipFile, targetDir.getAbsolutePath());
Assert.assertEquals(1, files.size());
File extractedFile = new File(targetDir, "conf/core-site.xml");
Assert.assertTrue(extractedFile.isFile());
Assert.assertEquals("content", new String(Files.readAllBytes(extractedFile.toPath()), StandardCharsets.UTF_8));
}
@Test
public void testRejectZipSlipEntry() throws Exception {
File zipFile = temporaryFolder.newFile("slip.zip");
writeZip(zipFile, new ZipItem("../evil.txt", "evil"));
File targetDir = temporaryFolder.newFolder("slip");
File escapedFile = new File(targetDir.getParentFile(), "evil.txt");
try {
ZipUtil.upzipFile(zipFile, targetDir.getAbsolutePath());
Assert.fail("Zip Slip entry should be rejected");
} catch (TaierDefineException e) {
Assert.assertTrue(e.getMessage().contains("outside of target dir"));
}
Assert.assertFalse(escapedFile.exists());
}
@Test
public void testRejectTooManyEntries() throws Exception {
File zipFile = temporaryFolder.newFile("too-many.zip");
writeZip(zipFile, buildZipItems(1001));
File targetDir = temporaryFolder.newFolder("too-many");
try {
ZipUtil.upzipFile(zipFile, targetDir.getAbsolutePath());
Assert.fail("Zip with too many entries should be rejected");
} catch (TaierDefineException e) {
Assert.assertTrue(e.getMessage().contains("entry count exceeds limit"));
}
}
@Test
public void testRejectRecursiveZipBomb() throws Exception {
File zipFile = temporaryFolder.newFile("nested.zip");
writeNestedZip(zipFile, 4);
File targetDir = temporaryFolder.newFolder("nested");
try {
ZipUtil.upzipFile(zipFile, targetDir.getAbsolutePath());
Assert.fail("Recursive zip should be rejected");
} catch (TaierDefineException e) {
Assert.assertTrue(e.getMessage().contains("recursion depth exceeds limit"));
}
}
private static ZipItem[] buildZipItems(int count) {
ZipItem[] items = new ZipItem[count];
for (int i = 0; i < count; i++) {
items[i] = new ZipItem("file-" + i + ".txt", "a");
}
return items;
}
private static void writeNestedZip(File zipFile, int depth) throws IOException {
if (depth == 0) {
writeZip(zipFile, new ZipItem("leaf.txt", "leaf"));
return;
}
File innerZip = File.createTempFile("inner", ".zip", zipFile.getParentFile());
writeNestedZip(innerZip, depth - 1);
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
zipOutputStream.putNextEntry(new ZipEntry("inner-" + depth + ".zip"));
Files.copy(innerZip.toPath(), zipOutputStream);
zipOutputStream.closeEntry();
}
Files.delete(innerZip.toPath());
}
private static void writeZip(File zipFile, ZipItem... items) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
for (ZipItem item : items) {
zipOutputStream.putNextEntry(new ZipEntry(item.name));
zipOutputStream.write(item.content.getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
}
}
}
private static class ZipItem {
private final String name;
private final String content;
private ZipItem(String name, String content) {
this.name = name;
this.content = content;
}
}
}