|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.alibaba.cloud.ai.dataagent.controller; |
| 17 | + |
| 18 | +import com.alibaba.cloud.ai.dataagent.entity.Agent; |
| 19 | +import com.alibaba.cloud.ai.dataagent.entity.Datasource; |
| 20 | +import com.alibaba.cloud.ai.dataagent.properties.FileStorageProperties; |
| 21 | +import com.alibaba.cloud.ai.dataagent.service.file.FileStorageService; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.io.TempDir; |
| 25 | +import org.springframework.core.io.Resource; |
| 26 | +import org.springframework.http.ResponseEntity; |
| 27 | +import org.springframework.http.codec.multipart.FilePart; |
| 28 | +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
| 29 | +import org.springframework.web.multipart.MultipartFile; |
| 30 | +import reactor.core.publisher.Mono; |
| 31 | + |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 40 | + |
| 41 | +class FileAndSerializationSecurityTest { |
| 42 | + |
| 43 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 44 | + |
| 45 | + @TempDir |
| 46 | + Path tempDir; |
| 47 | + |
| 48 | + @Test |
| 49 | + void agentSerializationShouldHideApiKey() throws Exception { |
| 50 | + Agent agent = Agent.builder().id(1L).name("demo").apiKey("sk-test-secret").apiKeyEnabled(1).build(); |
| 51 | + |
| 52 | + String json = objectMapper.writeValueAsString(agent); |
| 53 | + |
| 54 | + assertFalse(json.contains("\"apiKey\":")); |
| 55 | + assertFalse(json.contains("sk-test-secret")); |
| 56 | + assertTrue(json.contains("apiKeyEnabled")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void datasourceSerializationShouldHideCredentials() throws Exception { |
| 61 | + Datasource datasource = Datasource.builder() |
| 62 | + .id(1) |
| 63 | + .name("mysql") |
| 64 | + .username("root") |
| 65 | + .password("secret") |
| 66 | + .connectionUrl("jdbc:mysql://127.0.0.1:3306/test") |
| 67 | + .build(); |
| 68 | + |
| 69 | + String json = objectMapper.writeValueAsString(datasource); |
| 70 | + |
| 71 | + assertTrue(json.contains("username")); |
| 72 | + assertFalse(json.contains("\"password\":")); |
| 73 | + assertFalse(json.contains("\"connectionUrl\":")); |
| 74 | + assertFalse(json.contains("jdbc:mysql://127.0.0.1:3306/test")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void getFileShouldReturnFileWithinBasePath() throws Exception { |
| 79 | + Path file = tempDir.resolve("avatars").resolve("ok.txt"); |
| 80 | + Files.createDirectories(file.getParent()); |
| 81 | + byte[] expected = "safe-content".getBytes(StandardCharsets.UTF_8); |
| 82 | + Files.write(file, expected); |
| 83 | + |
| 84 | + FileUploadController controller = new FileUploadController(buildFileStorageProperties(), |
| 85 | + new NoopFileStorageService()); |
| 86 | + MockServerHttpRequest request = MockServerHttpRequest.get("/api/upload/uploads/avatars/ok.txt").build(); |
| 87 | + |
| 88 | + ResponseEntity<byte[]> response = controller.getFile(request); |
| 89 | + |
| 90 | + assertEquals(200, response.getStatusCode().value()); |
| 91 | + assertArrayEquals(expected, response.getBody()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void getFileShouldBlockPathTraversal() throws Exception { |
| 96 | + Path parentSecret = tempDir.getParent().resolve("secret.txt"); |
| 97 | + Files.write(parentSecret, "top-secret".getBytes(StandardCharsets.UTF_8)); |
| 98 | + |
| 99 | + FileUploadController controller = new FileUploadController(buildFileStorageProperties(), |
| 100 | + new NoopFileStorageService()); |
| 101 | + MockServerHttpRequest request = MockServerHttpRequest.get("/api/upload/uploads/../secret.txt").build(); |
| 102 | + |
| 103 | + ResponseEntity<byte[]> response = controller.getFile(request); |
| 104 | + |
| 105 | + assertEquals(403, response.getStatusCode().value()); |
| 106 | + } |
| 107 | + |
| 108 | + private FileStorageProperties buildFileStorageProperties() { |
| 109 | + FileStorageProperties properties = new FileStorageProperties(); |
| 110 | + properties.setPath(tempDir.toString()); |
| 111 | + properties.setUrlPrefix("/uploads"); |
| 112 | + return properties; |
| 113 | + } |
| 114 | + |
| 115 | + private static final class NoopFileStorageService implements FileStorageService { |
| 116 | + |
| 117 | + @Override |
| 118 | + public Mono<String> storeFile(FilePart filePart, String subPath) { |
| 119 | + throw new UnsupportedOperationException(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String storeFile(MultipartFile file, String subPath) { |
| 124 | + throw new UnsupportedOperationException(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean deleteFile(String filePath) { |
| 129 | + throw new UnsupportedOperationException(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String getFileUrl(String filePath) { |
| 134 | + throw new UnsupportedOperationException(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public Resource getFileResource(String filePath) { |
| 139 | + throw new UnsupportedOperationException(); |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments