diff --git a/cmd/vela-slack/plugin.go b/cmd/vela-slack/plugin.go index 03f0243..d923485 100644 --- a/cmd/vela-slack/plugin.go +++ b/cmd/vela-slack/plugin.go @@ -91,7 +91,7 @@ func (p *Plugin) Exec() error { // clean up newlines that could invalidate JSON // BuildMessage is the only field that can have newlines; // typically when the commit contains a title and body message - p.Env.BuildMessage = strings.Replace(p.Env.BuildMessage, "\n", "\\n", -1) + p.Env.BuildMessage = cleanBuildMessage(p.Env.BuildMessage) // create message struct file Slack msg := slack.WebhookMessage{ @@ -187,6 +187,14 @@ func (p *Plugin) Exec() error { return nil } +func cleanBuildMessage(buildMessage string) string { + subject := buildMessage + subject = strings.ReplaceAll(subject, "\n", "\\n") + subject = strings.ReplaceAll(subject, "\"", "\\\"") + + return subject +} + // Validate function to validate plugin configuration. func (p *Plugin) Validate() error { logrus.Debug("validating plugin configuration") diff --git a/cmd/vela-slack/plugin_test.go b/cmd/vela-slack/plugin_test.go index 3bd588f..883890e 100644 --- a/cmd/vela-slack/plugin_test.go +++ b/cmd/vela-slack/plugin_test.go @@ -328,6 +328,29 @@ Newlines`, } } +func TestSlack_Plugin_Exec_Quote(t *testing.T) { + // setup types + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "ok") + })) + defer ts.Close() + + p := &Plugin{ + Webhook: ts.URL, + Env: &Env{ + BuildMessage: `This message has "quotes"`, + }, + Path: "testdata/slack_attachment.json", + WebhookMsg: &slack.WebhookMessage{}, + Remote: false, + } + + err := p.Exec() + if err != nil { + t.Errorf("Exec returned err: %v", err) + } +} + func TestSlack_Plugin_Exec_Newline_Embedded(t *testing.T) { // setup types ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {