Skip to content

Commit b3c4321

Browse files
notify/slack: reject webhook api_url_file at notification time
The chat.postMessage requirement of update_message and post_updates_to_thread is validated during config resolution, but an api_url_file cannot be checked there: its content is only read when a notification is sent, so that a rotated value is picked up without a reload. A file holding a hooks.slack.com URL therefore reached the notifier, which sent ts or thread_ts to an incoming webhook that returns no message identifiers, silently doing nothing useful. The resolved URL is now checked once the file has been read, with the same error as config validation. Signed-off-by: Sébastien GLON <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent c2e61ee commit b3c4321

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎notify/slack/slack.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ import (
3838
// https://api.slack.com/reference/messaging/attachments#legacy_fields - 1024, no units given, assuming runes or characters.
3939
const maxTitleLenRunes = 1024
4040

41+
// chatPostMessageURL is the only api_url that supports message updates and
42+
// threads, both of which need the message identifiers returned by the bot-token
43+
// Web API. Incoming webhooks return no identifiers.
44+
const chatPostMessageURL = "https://slack.com/api/chat.postMessage"
45+
4146
// New returns a new Slack notification handler.
4247
func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
4348
client, err := notify.NewClientWithTracing(*c.HTTPConfig, "slack", httpOpts...)
@@ -155,6 +160,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
155160
u = strings.TrimSpace(string(content))
156161
}
157162

163+
if err := requireBotAPIURL(n.conf, u); err != nil {
164+
return false, err
165+
}
166+
158167
if n.conf.Timeout > 0 {
159168
postCtx, cancel := context.WithTimeoutCause(ctx, n.conf.Timeout, fmt.Errorf("configured slack timeout reached (%s)", n.conf.Timeout))
160169
defer cancel()
@@ -231,6 +240,23 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
231240
return retry, nil
232241
}
233242

243+
// requireBotAPIURL rejects a resolved api_url that cannot support message
244+
// updates or threads. Config loading already performs this check for api_url
245+
// and app_token; api_url_file can only be checked here, because its content is
246+
// read at notification time.
247+
func requireBotAPIURL(conf *config.SlackConfig, u string) error {
248+
if !conf.UpdateMessage && !conf.PostUpdatesToThread {
249+
return nil
250+
}
251+
if u == chatPostMessageURL {
252+
return nil
253+
}
254+
if conf.UpdateMessage {
255+
return fmt.Errorf("update_message can only be used with bot tokens. api_url must be set to %s", chatPostMessageURL)
256+
}
257+
return fmt.Errorf("post_updates_to_thread can only be used with bot tokens. api_url must be set to %s", chatPostMessageURL)
258+
}
259+
234260
// repeatIntervalOnly reports whether the notification was triggered solely by
235261
// repeat_interval elapsing, meaning the state of the alert group is unchanged.
236262
func repeatIntervalOnly(ctx context.Context) bool {

‎notify/slack/slack_test.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,53 @@ func TestSlackPostUpdatesToThread(t *testing.T) {
639639
require.NotContains(t, captured[0].body, "thread_ts")
640640
})
641641
}
642+
643+
func TestSlackRejectsWebhookAPIURLFileAtNotifyTime(t *testing.T) {
644+
f, err := os.CreateTemp("", "slack_test")
645+
require.NoError(t, err, "creating temp file failed")
646+
t.Cleanup(func() { os.Remove(f.Name()) })
647+
_, err = f.WriteString("https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX\n")
648+
require.NoError(t, err, "writing to temp file failed")
649+
650+
for _, tc := range []struct {
651+
name string
652+
conf config.SlackConfig
653+
err string
654+
}{
655+
{
656+
name: "update_message",
657+
conf: config.SlackConfig{UpdateMessage: true},
658+
err: "update_message can only be used with bot tokens. api_url must be set to https://slack.com/api/chat.postMessage",
659+
},
660+
{
661+
name: "post_updates_to_thread",
662+
conf: config.SlackConfig{PostUpdatesToThread: true},
663+
err: "post_updates_to_thread can only be used with bot tokens. api_url must be set to https://slack.com/api/chat.postMessage",
664+
},
665+
} {
666+
t.Run(tc.name, func(t *testing.T) {
667+
conf := tc.conf
668+
conf.APIURLFile = f.Name()
669+
conf.Channel = "#test-channel"
670+
conf.HTTPConfig = &commoncfg.HTTPClientConfig{}
671+
672+
tmpl, err := template.FromGlobs([]string{})
673+
require.NoError(t, err)
674+
tmpl.ExternalURL, err = url.Parse("http://am")
675+
require.NoError(t, err)
676+
677+
notifier, err := New(&conf, tmpl, slog.New(slog.DiscardHandler))
678+
require.NoError(t, err)
679+
680+
notifier.postJSONFunc = func(ctx context.Context, client *http.Client, reqURL string, body io.Reader) (*http.Response, error) {
681+
t.Fatal("no request must be sent to a webhook URL")
682+
return nil, nil
683+
}
684+
685+
ctx := notify.WithNflogStore(notify.WithGroupKey(context.Background(), "test-group-key"), nflog.NewStore(nil))
686+
retry, err := notifier.Notify(ctx)
687+
require.False(t, retry)
688+
require.EqualError(t, err, tc.err)
689+
})
690+
}
691+
}

0 commit comments

Comments
 (0)