diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java index 492e340d9e8..4ba10283cc5 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java @@ -115,6 +115,42 @@ protected String renderContent(NoticeTemplate noticeTemplate, Alert alert) throw return template.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1"); } + protected String escapeJsonStr(String jsonStr){ + if (jsonStr == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + for (char c : jsonStr.toCharArray()) { + switch (c) { + case '"': + sb.append("\\\""); + break; + case '\\': + sb.append("\\\\"); + break; + case '\b': + sb.append("\\b"); + break; + case '\f': + sb.append("\\f"); + break; + case '\n': + sb.append("\\n"); + break; + case '\r': + sb.append("\\r"); + break; + case '\t': + sb.append("\\t"); + break; + default: + sb.append(c); + } + } + return sb.toString(); + } + @EventListener(SystemConfigChangeEvent.class) public void onEvent(SystemConfigChangeEvent event) { log.info("{} receive system config change event: {}.", this.getClass().getName(), event.getSource()); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java index b4db3b0773c..7f7b793cb25 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java @@ -47,6 +47,7 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a // fix null pointer exception filterInvalidTags(alert); + alert.setContent(escapeJsonStr(alert.getContent())); String webhookJson = renderContent(noticeTemplate, alert); webhookJson = webhookJson.replace(",\n }", "\n }");