|
1 | | -/*--------------------------------------------------------------------------------------------- |
2 | | - * Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved. |
3 | | - * This file is a part of the ModelEngine Project. |
4 | | - * Licensed under the MIT License. See License.txt in the project root for license information. |
5 | | - *--------------------------------------------------------------------------------------------*/ |
| 1 | +/* |
| 2 | + * Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 3 | + * This file is a part of the ModelEngine Project. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + */ |
6 | 6 |
|
7 | 7 | package modelengine.fitframework.util.support; |
8 | 8 |
|
@@ -195,20 +195,134 @@ void givenMax1ByteSecurityThenThrowException() { |
195 | 195 | @Test |
196 | 196 | @DisplayName("给定压缩包中存在遍历路径文件,解压失败。") |
197 | 197 | public void givenPathTraversalThenCatchException() throws IOException { |
198 | | - File testZipFile = new File("test-archive.zip"); |
199 | | - File targetDir = new File("target-dir"); |
200 | | - try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(testZipFile.toPath()))) { |
201 | | - ZipEntry entry = new ZipEntry("../unauthorized-file.txt"); |
202 | | - zos.putNextEntry(entry); |
203 | | - zos.write("Malicious content".getBytes(StandardCharsets.UTF_8)); |
204 | | - zos.closeEntry(); |
| 198 | + File testZipFile = new File("src/test/resources/zip-slip-test/test-archive.zip"); |
| 199 | + File targetDir = new File("src/test/resources/zip-slip-test/target-dir"); |
| 200 | + try { |
| 201 | + // 确保父目录存在 |
| 202 | + FileUtils.ensureDirectory(testZipFile.getParentFile()); |
| 203 | + // 动态创建包含恶意路径的ZIP文件 |
| 204 | + try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(testZipFile.toPath()))) { |
| 205 | + ZipEntry entry = new ZipEntry("../unauthorized-file.txt"); |
| 206 | + zos.putNextEntry(entry); |
| 207 | + zos.write("Malicious content".getBytes(StandardCharsets.UTF_8)); |
| 208 | + zos.closeEntry(); |
| 209 | + } |
| 210 | + |
| 211 | + Unzip unzip = |
| 212 | + FileUtils.unzip(testZipFile).secure(new Unzip.Security(100, 1024, false)).target(targetDir); |
| 213 | + SecurityException securityException = catchThrowableOfType(SecurityException.class, unzip::start); |
| 214 | + assertThat(securityException.getMessage()).startsWith("Detected a potential path traversal attack. "); |
| 215 | + } finally { |
| 216 | + FileUtils.delete(testZipFile); |
| 217 | + FileUtils.delete(targetDir); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + @DisplayName("Given zip entry with multiple parent path traversals then throw SecurityException") |
| 223 | + public void givenMultipleParentPathTraversalsThenThrowSecurityException() throws IOException { |
| 224 | + File testZipFile = new File("src/test/resources/zip-slip-test/test-archive-multi.zip"); |
| 225 | + File targetDir = new File("src/test/resources/zip-slip-test/target-dir-multi"); |
| 226 | + try { |
| 227 | + // 确保父目录存在 |
| 228 | + FileUtils.ensureDirectory(testZipFile.getParentFile()); |
| 229 | + // 动态创建包含多级路径遍历的ZIP文件 |
| 230 | + try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(testZipFile.toPath()))) { |
| 231 | + ZipEntry entry = new ZipEntry("../../../../../../etc/passwd"); |
| 232 | + zos.putNextEntry(entry); |
| 233 | + zos.write("Malicious content".getBytes(StandardCharsets.UTF_8)); |
| 234 | + zos.closeEntry(); |
| 235 | + } |
| 236 | + |
| 237 | + Unzip unzip = |
| 238 | + FileUtils.unzip(testZipFile).secure(new Unzip.Security(100, 1024, false)).target(targetDir); |
| 239 | + SecurityException securityException = catchThrowableOfType(SecurityException.class, unzip::start); |
| 240 | + assertThat(securityException.getMessage()).contains("Detected a potential path traversal attack"); |
| 241 | + } finally { |
| 242 | + FileUtils.delete(testZipFile); |
| 243 | + FileUtils.delete(targetDir); |
205 | 244 | } |
| 245 | + } |
206 | 246 |
|
207 | | - Unzip unzip = FileUtils.unzip(testZipFile).secure(new Unzip.Security(100, 1024, false)).target(targetDir); |
208 | | - SecurityException securityException = catchThrowableOfType(SecurityException.class, unzip::start); |
209 | | - assertThat(securityException.getMessage()).startsWith("Detected a potential path traversal attack. "); |
210 | | - FileUtils.delete(testZipFile); |
211 | | - FileUtils.delete(targetDir); |
| 247 | + @Test |
| 248 | + @DisplayName("Given zip entry with absolute path then throw SecurityException") |
| 249 | + public void givenAbsolutePathEntryThenThrowSecurityException() throws IOException { |
| 250 | + File testZipFile = new File("src/test/resources/zip-slip-test/test-archive-absolute.zip"); |
| 251 | + File targetDir = new File("src/test/resources/zip-slip-test/target-dir-absolute"); |
| 252 | + try { |
| 253 | + // 确保父目录存在 |
| 254 | + FileUtils.ensureDirectory(testZipFile.getParentFile()); |
| 255 | + // 动态创建包含绝对路径的ZIP文件 |
| 256 | + try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(testZipFile.toPath()))) { |
| 257 | + ZipEntry entry = new ZipEntry("/tmp/unauthorized-file.txt"); |
| 258 | + zos.putNextEntry(entry); |
| 259 | + zos.write("Malicious content".getBytes(StandardCharsets.UTF_8)); |
| 260 | + zos.closeEntry(); |
| 261 | + } |
| 262 | + |
| 263 | + Unzip unzip = |
| 264 | + FileUtils.unzip(testZipFile).secure(new Unzip.Security(100, 1024, false)).target(targetDir); |
| 265 | + SecurityException securityException = catchThrowableOfType(SecurityException.class, unzip::start); |
| 266 | + assertThat(securityException.getMessage()).contains("Detected a potential path traversal attack"); |
| 267 | + } finally { |
| 268 | + FileUtils.delete(testZipFile); |
| 269 | + FileUtils.delete(targetDir); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + @DisplayName("Given zip entry with path traversal in middle then throw SecurityException") |
| 275 | + public void givenPathTraversalInMiddleThenThrowSecurityException() throws IOException { |
| 276 | + File testZipFile = new File("src/test/resources/zip-slip-test/test-archive-middle.zip"); |
| 277 | + File targetDir = new File("src/test/resources/zip-slip-test/target-dir-middle"); |
| 278 | + try { |
| 279 | + // 确保父目录存在 |
| 280 | + FileUtils.ensureDirectory(testZipFile.getParentFile()); |
| 281 | + // 动态创建包含中间路径遍历的ZIP文件 |
| 282 | + try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(testZipFile.toPath()))) { |
| 283 | + ZipEntry entry = new ZipEntry("subdir/../../../unauthorized.txt"); |
| 284 | + zos.putNextEntry(entry); |
| 285 | + zos.write("Malicious content".getBytes(StandardCharsets.UTF_8)); |
| 286 | + zos.closeEntry(); |
| 287 | + } |
| 288 | + |
| 289 | + Unzip unzip = |
| 290 | + FileUtils.unzip(testZipFile).secure(new Unzip.Security(100, 1024, false)).target(targetDir); |
| 291 | + SecurityException securityException = catchThrowableOfType(SecurityException.class, unzip::start); |
| 292 | + assertThat(securityException.getMessage()).contains("Detected a potential path traversal attack"); |
| 293 | + } finally { |
| 294 | + FileUtils.delete(testZipFile); |
| 295 | + FileUtils.delete(targetDir); |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + @Test |
| 300 | + @DisplayName("Given zip entry with safe nested path then unzip successfully") |
| 301 | + public void givenSafeNestedPathThenUnzipSuccessfully() throws IOException { |
| 302 | + File testZipFile = new File("src/test/resources/zip-slip-test/test-archive-safe.zip"); |
| 303 | + File targetDir = new File("src/test/resources/zip-slip-test/target-dir-safe"); |
| 304 | + try { |
| 305 | + // 确保父目录存在 |
| 306 | + FileUtils.ensureDirectory(testZipFile.getParentFile()); |
| 307 | + // 动态创建包含安全嵌套路径的ZIP文件 |
| 308 | + try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(testZipFile.toPath()))) { |
| 309 | + ZipEntry entry = new ZipEntry("subdir/nested/safe-file.txt"); |
| 310 | + zos.putNextEntry(entry); |
| 311 | + zos.write("Safe content".getBytes(StandardCharsets.UTF_8)); |
| 312 | + zos.closeEntry(); |
| 313 | + } |
| 314 | + |
| 315 | + Unzip unzip = |
| 316 | + FileUtils.unzip(testZipFile).secure(new Unzip.Security(100, 1024, false)).target(targetDir); |
| 317 | + assertThatNoException().isThrownBy(unzip::start); |
| 318 | + |
| 319 | + File safeFile = new File(targetDir, "subdir/nested/safe-file.txt"); |
| 320 | + assertThat(safeFile).exists(); |
| 321 | + assertThat(Files.readString(safeFile.toPath())).isEqualTo("Safe content"); |
| 322 | + } finally { |
| 323 | + FileUtils.delete(testZipFile); |
| 324 | + FileUtils.delete(targetDir); |
| 325 | + } |
212 | 326 | } |
213 | 327 |
|
214 | 328 | @Test |
|
0 commit comments