mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-11-11 13:50:44 +00:00
fix: harden every shipped config and isolate archive storage tests
This commit is contained in:
2
.github/workflows/maven.yml
vendored
2
.github/workflows/maven.yml
vendored
@@ -39,7 +39,7 @@ jobs:
|
|||||||
if: runner.os == 'Linux'
|
if: runner.os == 'Linux'
|
||||||
run: >-
|
run: >-
|
||||||
mvn -B -pl server
|
mvn -B -pl server
|
||||||
-Dtest=FtpCommandSecurityTests,UploadPathSecurityTests,PreviewSourceSecurityTests,PreviewParameterSecurityTests,ArchiveDirectorySecurityTests,ActuatorExposureSecurityTests,ArchiveTraversalSecurityTests,FileControllerPathSecurityTests,FileControllerDeleteSecurityTests
|
-Dtest=FtpCommandSecurityTests,UploadPathSecurityTests,PreviewSourceSecurityTests,PreviewParameterSecurityTests,ArchiveDirectorySecurityTests,ActuatorExposureSecurityTests,ActuatorOverrideExposureSecurityTests,ArchiveTraversalSecurityTests,FileControllerPathSecurityTests,FileControllerDeleteSecurityTests
|
||||||
test
|
test
|
||||||
|
|
||||||
- name: Validate Windows deployment
|
- name: Validate Windows deployment
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ spring.freemarker.request-context-attribute = request
|
|||||||
spring.freemarker.suffix = .ftl
|
spring.freemarker.suffix = .ftl
|
||||||
|
|
||||||
# Spring Boot Actuator监控端点配置
|
# Spring Boot Actuator监控端点配置
|
||||||
management.endpoints.web.exposure.include = health,info,metrics
|
management.endpoints.web.exposure.include = health
|
||||||
management.endpoint.health.show-details = always
|
management.endpoint.health.show-details = never
|
||||||
management.health.defaults.enabled = true
|
management.health.defaults.enabled = true
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -35,13 +35,13 @@ import java.util.List;
|
|||||||
@Component
|
@Component
|
||||||
public class CompressFileReader {
|
public class CompressFileReader {
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private static final String fileDir = ConfigConstants.getFileDir();
|
|
||||||
|
|
||||||
public CompressFileReader(FileHandlerService fileHandlerService) {
|
public CompressFileReader(FileHandlerService fileHandlerService) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String unRar(String filePath, String filePassword, String fileName, FileAttribute fileAttribute) throws Exception {
|
public String unRar(String filePath, String filePassword, String fileName, FileAttribute fileAttribute) throws Exception {
|
||||||
|
String fileDir = ConfigConstants.getFileDir();
|
||||||
List<String> imgUrls = new ArrayList<>();
|
List<String> imgUrls = new ArrayList<>();
|
||||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||||
String packagePath = "_";
|
String packagePath = "_";
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package cn.keking.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = ActuatorExposureSecurityTests.ManagementApp.class,
|
||||||
|
properties = "spring.config.name=test",
|
||||||
|
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class ActuatorOverrideExposureSecurityTests extends ActuatorExposureSecurityTests {
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.io.TempDir;
|
|||||||
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
@@ -17,18 +18,23 @@ class ArchiveTraversalSecurityTests {
|
|||||||
|
|
||||||
@Test void existingExtractorRejectsZipSlipBeforeWritingOutsideArchive() throws Exception {
|
@Test void existingExtractorRejectsZipSlipBeforeWritingOutsideArchive() throws Exception {
|
||||||
String previous = ConfigConstants.getFileDir();
|
String previous = ConfigConstants.getFileDir();
|
||||||
ConfigConstants.setFileDirValue(temp.toString());
|
|
||||||
try {
|
try {
|
||||||
Path archive = temp.resolve("malicious.zip");
|
CompressFileReader reader = new CompressFileReader(null);
|
||||||
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(archive))) {
|
for (String directory : List.of("first", "second")) {
|
||||||
zip.putNextEntry(new ZipEntry("../outside-marker.txt"));
|
Path root = Files.createDirectory(temp.resolve(directory));
|
||||||
zip.write("BENIGN-CANARY".getBytes());
|
ConfigConstants.setFileDirValue(root.toString());
|
||||||
zip.closeEntry();
|
Path archive = root.resolve("malicious.zip");
|
||||||
|
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(archive))) {
|
||||||
|
zip.putNextEntry(new ZipEntry("../outside-marker.txt"));
|
||||||
|
zip.write("BENIGN-CANARY".getBytes());
|
||||||
|
zip.closeEntry();
|
||||||
|
}
|
||||||
|
Exception error = assertThrows(Exception.class, () -> reader
|
||||||
|
.unRar(archive.toString(), "", "malicious.zip", new FileAttribute()));
|
||||||
|
assertTrue(error.getMessage().contains("Unsafe path detected"), error.toString());
|
||||||
|
assertTrue(Files.isDirectory(root.resolve("malicious.zip_")));
|
||||||
|
assertFalse(Files.exists(root.resolve("outside-marker.txt")));
|
||||||
}
|
}
|
||||||
Exception error = assertThrows(Exception.class, () -> new CompressFileReader(null)
|
|
||||||
.unRar(archive.toString(), "", "malicious.zip", new FileAttribute()));
|
|
||||||
assertTrue(error.getMessage().contains("Unsafe path detected"), error.toString());
|
|
||||||
assertFalse(Files.exists(temp.resolve("outside-marker.txt")));
|
|
||||||
} finally {
|
} finally {
|
||||||
ConfigConstants.setFileDirValue(previous);
|
ConfigConstants.setFileDirValue(previous);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user