@@ -206,8 +206,8 @@ func (t *WebhookNotifier) sendPOST(webhookURL, heading, message string, logger *
206206func (t * WebhookNotifier ) buildRequestBody (heading , message string ) []byte {
207207 if t .BodyTemplate != nil && * t .BodyTemplate != "" {
208208 result := * t .BodyTemplate
209- result = strings .ReplaceAll (result , "{{heading}}" , heading )
210- result = strings .ReplaceAll (result , "{{message}}" , message )
209+ result = strings .ReplaceAll (result , "{{heading}}" , escapeJSONString ( heading ) )
210+ result = strings .ReplaceAll (result , "{{message}}" , escapeJSONString ( message ) )
211211 return []byte (result )
212212 }
213213
@@ -227,3 +227,17 @@ func (t *WebhookNotifier) applyHeaders(req *http.Request) {
227227 }
228228 }
229229}
230+
231+ func escapeJSONString (s string ) string {
232+ b , err := json .Marshal (s )
233+ if err != nil || len (b ) < 2 {
234+ escaped := strings .ReplaceAll (s , `\` , `\\` )
235+ escaped = strings .ReplaceAll (escaped , `"` , `\"` )
236+ escaped = strings .ReplaceAll (escaped , "\n " , `\n` )
237+ escaped = strings .ReplaceAll (escaped , "\r " , `\r` )
238+ escaped = strings .ReplaceAll (escaped , "\t " , `\t` )
239+ return escaped
240+ }
241+
242+ return string (b [1 : len (b )- 1 ])
243+ }
0 commit comments