Skip to content

Commit

Permalink
fix: Prevent error when using quotation marks in BuildMessage (#75)
Browse files Browse the repository at this point in the history
* fix: Prevent error when using quotation marks in BuildMessage

When a PR title or commit summary contains quotation marks, they were
not escaped when injected into the JSON attachment. This caused
a message like

    unable to unmarshal webhook message: invalid character 't' after object key:value pair

when used inside of a template containing this test:

```json
{
  "text": "*Status*: FAILURE\n*Repo*: {{ .RepositoryName }} | *Branch*: {{ .BuildBranch }} | *Author*: {{ .BuildAuthor }}\n*Build*: {{ .BuildLink }}\n*Message*: {{ .BuildMessage }}"
}
```

This change safens this use case. Other typical fields _shouldn't_ have
quotation marks in them, but it might be prudent down the line to
cleanse _all_ fields as they are injected into the JSON or provide
a method usable inside of the template that escapes correctly.

* fix: Use ReplaceAll instead of Replace with -1

* fix: Address cuddling comment
  • Loading branch information
colindean authored Jul 17, 2024
1 parent 23a64ab commit 379b0ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/vela-slack/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions cmd/vela-slack/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 379b0ba

Please sign in to comment.