对应 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 全部通过。
93 lines
4.4 KiB
Java
93 lines
4.4 KiB
Java
package run.halo.operationaudit.alert;
|
||
|
||
import java.time.Duration;
|
||
import java.util.Map;
|
||
import lombok.RequiredArgsConstructor;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.http.MediaType;
|
||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||
import org.springframework.stereotype.Component;
|
||
import org.springframework.web.reactive.function.client.WebClient;
|
||
import reactor.core.publisher.Mono;
|
||
import reactor.netty.http.client.HttpClient;
|
||
|
||
/**
|
||
* 告警 Webhook 推送器:向配置地址异步 POST JSON 负载。
|
||
*
|
||
* <p>fire-and-forget(产品设计原则 4):超时 10 秒后放弃,失败(网络错误、非 2xx 响应、
|
||
* 地址非法)仅记 warn 日志,不重试、不抛出,绝不影响登录与审计主流程。
|
||
*
|
||
* <p>负载为通用 JSON 结构(type/title/content/timestamp/data),不内置钉钉、企业微信、
|
||
* 飞书等平台的专属消息格式适配,需要时可在中转服务侧转换。
|
||
*
|
||
* <p>功能评审 P0-2:{@link #sendForResult(String, Map)}——返回推送结果
|
||
* (送达与否 + 原因摘要)供测试告警端点同步反馈到设置页;登录失败告警路径
|
||
* 仍走 {@link #send(String, Map)}(fire-and-forget 语义不变)。
|
||
*
|
||
* <p>出站安全(官方《敏感数据与出站请求》指南):发送前经 {@link OutboundUrlGuard}
|
||
* 完成 SSRF 校验(scheme/主机名/用户信息/端口 + 全量 DNS 解析危险地址段拒绝),
|
||
* 建连钉定校验通过的地址(DNS rebinding 防护);重定向显式关闭;连接/响应/整体
|
||
* 三层超时(10 秒);响应体不落内存(toBodilessEntity)。校验失败归一为
|
||
* {@link AlertSendResult#failure(String)},永不抛出。
|
||
*/
|
||
@Slf4j
|
||
@Component
|
||
@RequiredArgsConstructor
|
||
public class AlertWebhookSender {
|
||
|
||
/** 发送超时:告警为旁路功能,超时即放弃(不重试)。 */
|
||
private static final Duration TIMEOUT = Duration.ofSeconds(10);
|
||
|
||
private final OutboundUrlGuard guard;
|
||
|
||
/** 异步推送 JSON 负载;任何失败仅记 warn 日志(只记主机名,不记可能含凭据的地址)。 */
|
||
public void send(String url, Map<String, Object> payload) {
|
||
sendForResult(url, payload).subscribe(result -> {
|
||
if (!result.success()) {
|
||
log.warn("Failed to send alert webhook to {}: {}",
|
||
OutboundUrlGuard.urlHost(url), result.message());
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 推送 JSON 负载并返回结果(功能评审 P0-2):成功为接收方 2xx;超时、网络错误、
|
||
* 非 2xx、地址校验失败等任何失败均归一为失败结果,绝不抛出、绝不重试。
|
||
*
|
||
* <p>返回的 Mono 永不为空、永不报错,超时上限与 fire-and-forget 路径一致
|
||
* (10 秒)——测试告警端点同步等待该结果反馈到设置页。
|
||
*/
|
||
public Mono<AlertSendResult> sendForResult(String url, Map<String, Object> payload) {
|
||
return guard.validate(url)
|
||
.flatMap(target -> sendToTarget(target, payload))
|
||
.onErrorResume(OutboundUrlGuard.InvalidUrlException.class,
|
||
e -> Mono.just(AlertSendResult.failure(e.getMessage())));
|
||
}
|
||
|
||
/**
|
||
* 向已完成 SSRF 校验并钉定地址的目标推送(包内可见以便测试直接构造回环目标,
|
||
* 绕过守卫测试 HTTP 机制本身)。
|
||
*/
|
||
Mono<AlertSendResult> sendToTarget(OutboundUrlGuard.ValidatedTarget target,
|
||
Map<String, Object> payload) {
|
||
var httpClient = HttpClient.create()
|
||
// 建连钉定校验通过的地址:TLS SNI/Host 头仍取原始主机名,证书校验不受影响
|
||
.resolver(new PinnedAddressResolverGroup(target.toSocketAddress()))
|
||
// Webhook 无重定向语义:显式关闭,杜绝跳转绕过目标校验
|
||
.followRedirect(false)
|
||
.responseTimeout(TIMEOUT);
|
||
return Mono.defer(() -> WebClient.builder()
|
||
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
||
.build()
|
||
.post()
|
||
.uri(target.uri())
|
||
.contentType(MediaType.APPLICATION_JSON)
|
||
.bodyValue(payload)
|
||
.retrieve()
|
||
.toBodilessEntity())
|
||
.timeout(TIMEOUT)
|
||
.map(response -> AlertSendResult.ok(response.getStatusCode().value()))
|
||
.onErrorResume(e -> Mono.just(AlertSendResult.failure(e)));
|
||
}
|
||
}
|