fix: 落实官方规范评审修复(生成 API 客户端 / SSRF 防护 / 凭据迁 Secret)
对应 docs/operation-audit-halo-compliance.md 评审的两项不符合与第 5 节修复建议: 1. API 客户端生成(清单第 2 条):build.gradle 配置 haloPlugin.openApi 管线; 查询/导出/测试端点补 tag、参数声明与类型化 response(生成契约输入); bootstrap OpenAPI 规范 JSON(与 Springdoc 声明一一对手工对齐)驱动 devtools generateApiClient 产出 TS 客户端(ui/src/api/generated,openapi-generator 7.7.0); UI 经 ui/src/api 封装层复用宿主 axiosInstance,替换手写类型与裸 fetch; @halo-dev/api-client 由死依赖转为 axiosInstance 实际来源。 2. 出站安全与凭据(清单第 4 条):新增 OutboundUrlGuard——URI 解析 + scheme 白名单 + 禁止用户名/密码形态 + 全量 DNS 解析并拒绝环回/私网/链路本地/组播/未指定/ 云元数据(169.254.169.254、100.100.100.200、fd00:ec2::254)/CGNAT/ULA/Teredo/ 6to4/文档与保留段;PinnedAddressResolverGroup 钉定校验地址建连(DNS rebinding 防护,TLS SNI/Host 仍取原主机名);重定向显式关闭、三层超时 10 秒。 Webhook 地址凭据迁移 Halo Secret(settings.yaml 改 secret 输入存引用,服务端经 ReactiveExtensionClient 解析键 webhookUrl,fail closed);日志只记主机名, 不再记录可能内嵌 access_token 的完整地址。 测试:后端 244 用例(新增守卫地址段枚举/Secret 解析 fail closed/建连钉定证明等 25 例)+ UI vitest 54 用例 + vue-tsc + ./gradlew build 全部通过。
This commit is contained in:
+75
-6
@@ -1,6 +1,7 @@
|
||||
package run.halo.operationaudit.alert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -8,18 +9,34 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.app.extension.Secret;
|
||||
import run.halo.app.plugin.ReactiveSettingFetcher;
|
||||
|
||||
/**
|
||||
* 告警设置 TTL 缓存测试(与审计设置同款模式):命中缓存、失败/缺省回退默认(告警关闭)。
|
||||
* 告警设置 TTL 缓存测试(与审计设置同款模式):命中缓存、失败/缺省回退默认(告警关闭);
|
||||
* Webhook 地址 Secret 解析:未配置引用 / Secret 不存在 / 缺字段 / 空白值均 fail closed
|
||||
* (错误信号,绝不发送),字段值去除首尾空白。
|
||||
*/
|
||||
class AlertSettingProviderTest {
|
||||
|
||||
private final ReactiveSettingFetcher fetcher = mock(ReactiveSettingFetcher.class);
|
||||
private final AlertSettingProvider provider = new AlertSettingProvider(fetcher);
|
||||
private final ReactiveExtensionClient client = mock(ReactiveExtensionClient.class);
|
||||
private final AlertSettingProvider provider = new AlertSettingProvider(fetcher, client);
|
||||
|
||||
private static AlertSettings settings(String secretName) {
|
||||
return new AlertSettings(true, secretName, 5, 5);
|
||||
}
|
||||
|
||||
private static Secret secret(String url) {
|
||||
var secret = new Secret();
|
||||
secret.setStringData(Map.of(AlertSettingProvider.WEBHOOK_URL_FIELD, url));
|
||||
return secret;
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCacheWithinTtl() {
|
||||
@@ -27,15 +44,15 @@ class AlertSettingProviderTest {
|
||||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||||
.thenAnswer(inv -> {
|
||||
fetchCount.incrementAndGet();
|
||||
return Mono.just(new AlertSettings(true, "https://hooks.example.com/x", 3, 2));
|
||||
return Mono.just(new AlertSettings(true, "webhook-secret", 3, 2));
|
||||
});
|
||||
|
||||
var first = provider.get().block();
|
||||
var second = provider.get().block();
|
||||
|
||||
assertThat(first).extracting(AlertSettings::enabled, AlertSettings::webhookUrl,
|
||||
assertThat(first).extracting(AlertSettings::enabled, AlertSettings::webhookSecretName,
|
||||
AlertSettings::thresholdCount, AlertSettings::windowMinutes)
|
||||
.containsExactly(true, "https://hooks.example.com/x", 3, 2);
|
||||
.containsExactly(true, "webhook-secret", 3, 2);
|
||||
assertThat(second).isSameAs(first);
|
||||
assertThat(fetchCount.get()).isEqualTo(1);
|
||||
}
|
||||
@@ -49,7 +66,7 @@ class AlertSettingProviderTest {
|
||||
|
||||
assertThat(settings).isEqualTo(AlertSettings.defaults());
|
||||
assertThat(settings.enabledOrDefault()).isFalse();
|
||||
assertThat(settings.webhookUrlOrDefault()).isNull();
|
||||
assertThat(settings.webhookSecretNameOrDefault()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -73,4 +90,56 @@ class AlertSettingProviderTest {
|
||||
provider.get().block();
|
||||
verify(fetcher, times(2)).fetch(any(), any());
|
||||
}
|
||||
|
||||
// ===== Webhook 地址 Secret 解析(fail closed) =====
|
||||
|
||||
@Test
|
||||
void shouldResolveWebhookUrlFromSecret() {
|
||||
when(client.fetch(eq(Secret.class), eq("webhook-secret")))
|
||||
.thenReturn(Mono.just(secret(" https://hooks.example.com/x ")));
|
||||
|
||||
assertThat(provider.webhookUrl(settings("webhook-secret")).block())
|
||||
.isEqualTo("https://hooks.example.com/x");
|
||||
// 便捷方法:先读设置再解析
|
||||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||||
.thenReturn(Mono.just(settings("webhook-secret")));
|
||||
assertThat(provider.webhookUrl().block()).isEqualTo("https://hooks.example.com/x");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailClosedWhenSecretRefMissing() {
|
||||
assertThatThrownBy(() -> provider.webhookUrl(settings(" ")).block())
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("未配置");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailClosedWhenSecretMissing() {
|
||||
when(client.fetch(eq(Secret.class), eq("gone"))).thenReturn(Mono.empty());
|
||||
|
||||
assertThatThrownBy(() -> provider.webhookUrl(settings("gone")).block())
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("不存在");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailClosedWhenSecretFieldMissing() {
|
||||
var secret = new Secret();
|
||||
secret.setStringData(Map.of("other", "value"));
|
||||
when(client.fetch(eq(Secret.class), eq("webhook-secret"))).thenReturn(Mono.just(secret));
|
||||
|
||||
assertThatThrownBy(() -> provider.webhookUrl(settings("webhook-secret")).block())
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining(AlertSettingProvider.WEBHOOK_URL_FIELD);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailClosedWhenSecretFieldBlank() {
|
||||
when(client.fetch(eq(Secret.class), eq("webhook-secret")))
|
||||
.thenReturn(Mono.just(secret(" ")));
|
||||
|
||||
assertThatThrownBy(() -> provider.webhookUrl(settings("webhook-secret")).block())
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("缺少");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user