diff --git a/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImpl.java b/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImpl.java index 73546670..183b717e 100644 --- a/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImpl.java +++ b/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImpl.java @@ -25,6 +25,7 @@ import com.xiaowang.jayucloud.salon.core.modules.system.vo.SecureIntegrationConf import com.xiaowang.jayucloud.salon.core.modules.user.entity.UserDO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -33,6 +34,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.core.env.Environment; import java.io.InputStream; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.ArrayList; @@ -41,6 +43,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Slf4j @Service @@ -51,6 +55,11 @@ public class IntegrationProbeServiceImpl implements IntegrationProbeService { private static final String STORAGE = "OBJECT_STORAGE"; private static final String SMS = "SMS"; private static final String SUBSCRIBE = "SUBSCRIBE_MESSAGE"; + private static final Pattern URL_IN_MESSAGE = Pattern.compile("https?://[^\\s\"'<>]+"); + private static final Pattern URL_HOST = Pattern.compile("^(https?://[^/?#]+)"); + private static final Pattern SENSITIVE_PARAM = Pattern.compile( + "(?i)(signature|token|access[-_]?key(?:[-_]?id|[-_]?secret)?|secret[-_]?key)=([^&\\s\"']+)"); + private static final int FAILURE_REASON_MAX_LENGTH = 120; private final IntegrationProbeResultMapper resultMapper; private final ObjectMapper objectMapper; @@ -386,17 +395,26 @@ public class IntegrationProbeServiceImpl implements IntegrationProbeService { } enabled++; boolean cleanupFailed = false; + String stage = "上传"; try { provider.uploadStream(new java.io.ByteArrayInputStream(bytes), bytes.length, "text/plain", objectName); + stage = "服务端读取"; try (InputStream input = provider.downloadFile(objectName)) { boolean same = java.util.Arrays.equals(bytes, input.readAllBytes()); + stage = "生成签名地址"; String signed = provider.getPresignedUrl(objectName, 5); - byte[] signedBytes = StringUtils.hasText(signed) ? restTemplate.getForObject(signed, byte[].class) : null; + stage = "签名访问"; + // 传 URI 而不是字符串:字符串重载会经 DefaultUriBuilderFactory 的 URI_COMPONENT 展开, + // 把签名参数里的 %2B/%2F/%3D 二次编码成 %252B/%252F/%253D,导致对象存储验签失败。 + byte[] signedBytes = StringUtils.hasText(signed) + ? restTemplate.exchange(URI.create(signed), HttpMethod.GET, null, byte[].class).getBody() + : null; + stage = "内容比对"; boolean signedRead = java.util.Arrays.equals(bytes, signedBytes); checks.add(new IntegrationProbeCheckVO(provider.providerCode(), same && signedRead ? "PASS" : "FAIL", same && signedRead ? "上传、读取和签名访问成功" : "对象内容或签名读取不一致")); } } catch (Exception exception) { - checks.add(new IntegrationProbeCheckVO(provider.providerCode(), "FAIL", "上传或读取失败")); + checks.add(new IntegrationProbeCheckVO(provider.providerCode(), "FAIL", storageFailureDetail(stage, exception))); } finally { try { provider.deleteFile(objectName); } catch (Exception ignored) { cleanupFailed = true; log.warn("清理对象存储测试文件失败,provider={}", provider.providerCode()); } @@ -410,6 +428,36 @@ public class IntegrationProbeServiceImpl implements IntegrationProbeService { } } + private String storageFailureDetail(String stage, Exception exception) { + return stage + "失败(" + exception.getClass().getSimpleName() + ":" + describeStorageFailure(exception) + ")"; + } + + private String describeStorageFailure(Exception exception) { + String message = exception.getMessage(); + if (!StringUtils.hasText(message)) { + return "无错误消息"; + } + Matcher urlMatcher = URL_IN_MESSAGE.matcher(message); + StringBuffer masked = new StringBuffer(); + while (urlMatcher.find()) { + urlMatcher.appendReplacement(masked, Matcher.quoteReplacement(maskUrl(urlMatcher.group()))); + } + urlMatcher.appendTail(masked); + String sanitized = SENSITIVE_PARAM.matcher(masked.toString()).replaceAll("$1=***"); + sanitized = sanitized.replaceAll("\\s+", " ").trim(); + return sanitized.length() > FAILURE_REASON_MAX_LENGTH + ? sanitized.substring(0, FAILURE_REASON_MAX_LENGTH) + "…" + : sanitized; + } + + private String maskUrl(String url) { + Matcher hostMatcher = URL_HOST.matcher(url); + if (hostMatcher.find()) { + return hostMatcher.group(1) + "/…"; + } + return "(地址已隐藏)"; + } + private IntegrationProbeResultVO probeSms(UserDO currentUser, String operation, String mode, String phone) { List checks = new ArrayList<>(); List configs = messageHubPushConfigService.listConfigs(currentUser); diff --git a/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImplTest.java b/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImplTest.java index a820059c..9e285666 100644 --- a/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImplTest.java +++ b/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/system/service/impl/IntegrationProbeServiceImplTest.java @@ -18,15 +18,24 @@ import com.xiaowang.jayucloud.salon.core.modules.user.entity.UserDO; import org.junit.jupiter.api.Test; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; +import java.io.ByteArrayInputStream; +import java.net.URI; +import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.List; import java.util.Map; import org.mockito.ArgumentCaptor; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; @@ -159,6 +168,68 @@ class IntegrationProbeServiceImplTest { assertEquals("微信审核请求参数格式无效,请核对接口字段", result.reason()); } + @Test + void signedReadPassesRawPresignedUrlAsUriWithoutReencoding() throws Exception { + String signed = "https://jayucloud-salon-sit.oss-cn-chengdu.aliyuncs.com/integration-probe/probe.txt" + + "?Expires=1762600000&OSSAccessKeyId=LTAI-test&Signature=abc%2Bdef%2Fghi%3D"; + byte[] payload = "salon-integration-probe".getBytes(StandardCharsets.UTF_8); + when(storageProvider.providerCode()).thenReturn("ALIYUN_OSS"); + when(storageProvider.isEnabled()).thenReturn(true); + when(storageProvider.downloadFile(anyString())).thenReturn(new ByteArrayInputStream(payload)); + when(storageProvider.getPresignedUrl(anyString(), any())).thenReturn(signed); + when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(), eq(byte[].class))) + .thenReturn(new ResponseEntity<>(payload, HttpStatus.OK)); + when(results.selectOne(any())).thenReturn(null); + + var result = service.probe(admin(), request("OBJECT_STORAGE", "ROUND_TRIP", "DIAGNOSTIC", false)); + + ArgumentCaptor uriCaptor = ArgumentCaptor.forClass(URI.class); + verify(restTemplate).exchange(uriCaptor.capture(), eq(HttpMethod.GET), any(), eq(byte[].class)); + assertEquals(signed, uriCaptor.getValue().toString(), "签名地址必须以 URI 原样传入,不能再被 URI_COMPONENT 编码"); + assertTrue(uriCaptor.getValue().toString().contains("%2B")); + verify(restTemplate, never()).getForObject(anyString(), eq(byte[].class)); + assertEquals("PASS", result.getStatus()); + } + + @Test + void storageFailureDetailCarriesStageAndExceptionType() throws Exception { + when(storageProvider.providerCode()).thenReturn("MINIO"); + when(storageProvider.isEnabled()).thenReturn(true); + when(storageProvider.downloadFile(anyString())).thenThrow(new UnknownHostException("sit.minio.yunail.art")); + + var result = service.probe(admin(), request("OBJECT_STORAGE", "ROUND_TRIP", "DIAGNOSTIC", false)); + + String detail = result.getChecks().get(0).getDetail(); + assertEquals("FAIL", result.getStatus()); + assertTrue(detail.contains("服务端读取失败"), detail); + assertTrue(detail.contains("UnknownHostException"), detail); + assertTrue(detail.contains("sit.minio.yunail.art"), detail); + } + + @Test + void storageFailureDetailMasksSignedUrlInsteadOfLeakingSignature() throws Exception { + String signed = "https://jayucloud-salon-sit.oss-cn-chengdu.aliyuncs.com/integration-probe/probe.txt" + + "?Expires=1762600000&OSSAccessKeyId=LTAI-test&Signature=abc%2Bdef%3D"; + byte[] payload = "salon-integration-probe".getBytes(StandardCharsets.UTF_8); + when(storageProvider.providerCode()).thenReturn("ALIYUN_OSS"); + when(storageProvider.isEnabled()).thenReturn(true); + when(storageProvider.downloadFile(anyString())).thenReturn(new ByteArrayInputStream(payload)); + when(storageProvider.getPresignedUrl(anyString(), any())).thenReturn(signed); + when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(), eq(byte[].class))) + .thenThrow(new IllegalStateException("403 SignatureDoesNotMatch for URL: " + signed)); + when(results.selectOne(any())).thenReturn(null); + + var result = service.probe(admin(), request("OBJECT_STORAGE", "ROUND_TRIP", "DIAGNOSTIC", false)); + + String detail = result.getChecks().get(0).getDetail(); + assertTrue(detail.contains("签名访问失败"), detail); + assertTrue(detail.contains("IllegalStateException"), detail); + assertTrue(detail.contains("jayucloud-salon-sit.oss-cn-chengdu.aliyuncs.com"), detail); + assertFalse(detail.contains("Signature="), detail); + assertFalse(detail.contains("OSSAccessKeyId"), detail); + assertFalse(detail.contains("%2B"), detail); + } + private UserDO admin() { UserDO user = new UserDO(); user.setId(1L); diff --git a/JC-Salon-Frontend/src/views/admin/SystemSettingsView.vue b/JC-Salon-Frontend/src/views/admin/SystemSettingsView.vue index 726f376b..835d954d 100644 --- a/JC-Salon-Frontend/src/views/admin/SystemSettingsView.vue +++ b/JC-Salon-Frontend/src/views/admin/SystemSettingsView.vue @@ -122,9 +122,9 @@

配置保存后再测试;点击状态芯片可展开检查项明细,真实发送会产生外部副作用。

+ 刷新 {{ probeFailCount }} 项失败 {{ probeWarnCount }} 项需关注 - 刷新最近结果
@@ -163,7 +163,7 @@
{{ check.name }} {{ probeStatusLabel(check.status) }} - {{ check.detail }} + {{ check.detail }}