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:
+31
-21
@@ -1,5 +1,7 @@
|
||||
package run.halo.operationaudit.alert;
|
||||
|
||||
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@@ -13,6 +15,7 @@ import reactor.core.publisher.Mono;
|
||||
import run.halo.app.core.extension.endpoint.CustomEndpoint;
|
||||
import run.halo.app.extension.GroupVersion;
|
||||
import run.halo.operationaudit.audit.AuditLog;
|
||||
import run.halo.operationaudit.audit.AuditQueryEndpoint;
|
||||
|
||||
/**
|
||||
* 告警测试端点(功能评审 P0-2):验证 Webhook 配置是否可用——以当前配置地址推送一条
|
||||
@@ -24,9 +27,12 @@ import run.halo.operationaudit.audit.AuditLog;
|
||||
* (auditlogs get/list)未授权该资源,仅超级管理员等管理角色可调用——与
|
||||
* "验证告警配置"的管理动作语义一致。
|
||||
*
|
||||
* <p>Webhook 地址未配置或非法(非 http(s))返回 400;推送失败不抛出——以 200 +
|
||||
* {@code success=false} 返回原因摘要(fire-and-forget 语义不变:真实告警路径仍异步
|
||||
* 不等待)。推送结果同时反馈到运行日志,任何异常不影响主流程。
|
||||
* <p>Webhook 地址经 {@link AlertSettingProvider#webhookUrl()} 从 Halo {@code Secret}
|
||||
* 解析(Setting 只存 Secret 引用,凭据不出 Secret);未配置引用、Secret 不存在或缺键
|
||||
* 返回 400;地址校验(SSRF 防护,见 {@link OutboundUrlGuard})与推送失败不抛出——
|
||||
* 以 200 + {@code success=false} 返回原因摘要(fire-and-forget 语义不变:真实告警路径
|
||||
* 仍异步不等待)。推送结果同时反馈到运行日志(只记主机名,不记可能含凭据的完整地址),
|
||||
* 任何异常不影响主流程。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -46,33 +52,37 @@ public class AlertTestEndpoint implements CustomEndpoint {
|
||||
return org.springdoc.webflux.core.fn.SpringdocRouteBuilder.route()
|
||||
.POST("alerts/-/test", this::test, builder -> builder
|
||||
.operationId("TestAlertWebhook")
|
||||
.tag(AuditQueryEndpoint.TAG)
|
||||
.description("向当前配置的 Webhook 地址推送一条测试告警(type=test)并返回"
|
||||
+ "推送结果:success 为是否送达(接收方 2xx),message 为状态/原因摘要;"
|
||||
+ "Webhook 未配置或非法时返回 400")
|
||||
.response(responseBuilder().responseCode("200")
|
||||
.description("推送结果")
|
||||
.implementation(AlertSendResult.class))
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> test(ServerRequest request) {
|
||||
return settingProvider.get()
|
||||
.flatMap(settings -> {
|
||||
var url = settings.webhookUrlOrDefault();
|
||||
if (url == null) {
|
||||
return ServerResponse.badRequest().bodyValue(Map.of("message",
|
||||
"Webhook 地址未配置或非法(需以 http(s) 开头),请先保存设置"));
|
||||
return settingProvider.webhookUrl()
|
||||
.flatMap(url -> sender.sendForResult(url, buildTestPayload(Instant.now()))
|
||||
.doOnNext(result -> {
|
||||
if (result.success()) {
|
||||
log.info("Test alert sent to {}: {}",
|
||||
OutboundUrlGuard.urlHost(url), result.message());
|
||||
} else {
|
||||
log.warn("Failed to send test alert to {}: {}",
|
||||
OutboundUrlGuard.urlHost(url), result.message());
|
||||
}
|
||||
})
|
||||
.flatMap(result -> ServerResponse.ok().bodyValue(result)))
|
||||
// 未配置引用 / Secret 不存在 / 缺键:设置问题归一为 400(消息不含 Secret 内容)
|
||||
.onErrorResume(e -> {
|
||||
if (e instanceof IllegalStateException ise) {
|
||||
return ServerResponse.badRequest()
|
||||
.bodyValue(Map.of("message", String.valueOf(ise.getMessage())));
|
||||
}
|
||||
return sender.sendForResult(url, buildTestPayload(Instant.now()))
|
||||
.doOnNext(result -> {
|
||||
if (result.success()) {
|
||||
log.info("Test alert sent to {}: {}", url, result.message());
|
||||
} else {
|
||||
log.warn("Failed to send test alert to {}: {}", url,
|
||||
result.message());
|
||||
}
|
||||
})
|
||||
.flatMap(result -> ServerResponse.ok().bodyValue(Map.of(
|
||||
"success", result.success(),
|
||||
"message", result.message())));
|
||||
return Mono.error(e);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user