Skip to content

Commit 75c88ba

Browse files
FIX (webhook): Escape webhook characters
1 parent ff1b653 commit 75c88ba

File tree

1 file changed

+16
-2
lines changed
  • backend/internal/features/notifiers/models/webhook

1 file changed

+16
-2
lines changed

backend/internal/features/notifiers/models/webhook/model.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ func (t *WebhookNotifier) sendPOST(webhookURL, heading, message string, logger *
206206
func (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

Comments
 (0)