Skip to content

Commit

Permalink
[bugfix] webhook template json escape (#2532)
Browse files Browse the repository at this point in the history
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
pwallk and tomsun28 authored Aug 16, 2024
1 parent 361e573 commit c8560e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }");

Expand Down

0 comments on commit c8560e6

Please sign in to comment.