From e27b6e6150f8d6cd9f5ef6539bb3154da4a28544 Mon Sep 17 00:00:00 2001 From: TheRealNoob Date: Sun, 8 Nov 2026 14:33:03 -0600 Subject: [PATCH 1/3] config: tolerate redacted api_url in Slack bot-token validation SecretURL.UnmarshalYAML deserialises the redaction token "" into &url.URL{}, whose String() is "". The update_message and post_updates_to_thread validations compare api_url against the required chat.postMessage endpoint, rejecting the empty string even though the real (hidden) value may be correct. Skip validation when api_url is the empty URL produced by redaction. A genuinely empty api_url cannot reach this check because ParseURL rejects strings without an http/https scheme. Signed-off-by: TheRealNoob --- config/config_test.go | 35 +++++++++++++++++++++++++++++++++++ notify/slack/config.go | 5 +++++ 2 files changed, 40 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index 28f08982a5..b3b1c0bece 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1398,6 +1398,41 @@ func TestSlackPostUpdatesToThreadWebhookURL(t *testing.T) { require.EqualError(t, err, "post_updates_to_thread can only be used with bot tokens. api_url must be set to https://slack.com/api/chat.postMessage") } +func TestSlackRedactedConfigUpdateMessage(t *testing.T) { + // A redacted config (e.g. from /api/v2/status) has api_url: . + // SecretURL.UnmarshalYAML deserialises this into an empty *url.URL. + // The update_message validation must not reject it. + in := ` +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts + api_url: + update_message: true +` + conf, err := Load(in) + require.NoError(t, err) + require.True(t, conf.Receivers[0].SlackConfigs[0].UpdateMessage) +} + +func TestSlackRedactedConfigPostUpdatesToThread(t *testing.T) { + in := ` +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts + api_url: + post_updates_to_thread: true +` + conf, err := Load(in) + require.NoError(t, err) + require.True(t, conf.Receivers[0].SlackConfigs[0].PostUpdatesToThread) +} + func TestSlackGlobalAppToken(t *testing.T) { conf, err := LoadFile("testdata/conf.slack-default-app-token.yml") if err != nil { diff --git a/notify/slack/config.go b/notify/slack/config.go index 000911efc8..cdbadd4275 100644 --- a/notify/slack/config.go +++ b/notify/slack/config.go @@ -230,6 +230,11 @@ func validateBotTokenAPIURL(option string, enabled bool, apiURL *amcommoncfg.Sec if !enabled { return nil } + // A redacted api_url (e.g. from /api/v2/status) deserialises as + // &url.URL{}, whose String() is "". The real value is unknown. + if apiURL != nil && apiURL.String() == "" { + return nil + } if apiURL == nil && apiURLFile != "" { return errors.New(option + " can't be used with api_url_file") } From d2f9086755bb7fbccbe100da893e15f9c5c24766 Mon Sep 17 00:00:00 2001 From: TheRealNoob Date: Wed, 11 Nov 2026 23:10:57 -0600 Subject: [PATCH 2/3] config: tolerate redacted global slack_api_url When the global slack_api_url is redacted (e.g. from /api/v2/status), SecretURL.UnmarshalYAML sets it to &url.URL{} whose String() is "". The mutual exclusivity check with slack_app_token compared this empty string against SlackAppURL, producing a false positive error. Skip the transition check when SlackAPIURL is nil or redacted. This also avoids a latent nil-dereference when only slack_api_url_file is set alongside slack_app_token. Signed-off-by: TheRealNoob --- config/config.go | 10 +++++++--- config/config_test.go | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 3f14c4450f..4becc99287 100644 --- a/config/config.go +++ b/config/config.go @@ -341,12 +341,16 @@ func (c *Config) UnmarshalYAML(unmarshal func(any) error) error { return errors.New("at most one of slack_api_url & slack_api_url_file must be configured") } - if (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) && (c.Global.SlackAPIURL != nil || len(c.Global.SlackAPIURLFile) > 0) { + // A redacted SlackAPIURL (e.g. from /api/v2/status) deserialises as + // &url.URL{}, whose String() is "". The real value is unknown. + if c.Global.SlackAPIURL != nil && c.Global.SlackAPIURL.String() != "" { // Support transition from workaround suggested in https://github.com/prometheus/alertmanager/issues/2513, // where users might set `slack_api_url` at the top level and then have `http_config` with individual // bearer tokens in the receivers. - if c.Global.SlackAPIURL.String() != c.Global.SlackAppURL.String() { - return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") + if (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) && (c.Global.SlackAPIURL != nil || len(c.Global.SlackAPIURLFile) > 0) { + if c.Global.SlackAPIURL.String() != c.Global.SlackAppURL.String() { + return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") + } } } diff --git a/config/config_test.go b/config/config_test.go index b3b1c0bece..40e3bee692 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1433,6 +1433,24 @@ receivers: require.True(t, conf.Receivers[0].SlackConfigs[0].PostUpdatesToThread) } +func TestSlackRedactedGlobalAPIURL(t *testing.T) { + // A redacted global slack_api_url (e.g. from /api/v2/status) + // must not cause a validation error. + in := ` +global: + slack_api_url: + slack_app_token: +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts +` + _, err := Load(in) + require.NoError(t, err) +} + func TestSlackGlobalAppToken(t *testing.T) { conf, err := LoadFile("testdata/conf.slack-default-app-token.yml") if err != nil { From b14c00d2a1d55452804acf4c40fca89a08652efc Mon Sep 17 00:00:00 2001 From: TheRealNoob Date: Mon, 23 Nov 2026 21:30:21 -0600 Subject: [PATCH 3/3] config: reject global slack_api_url_file with slack_app_token A global slack_api_url_file combined with slack_app_token or slack_app_token_file used to panic in UnmarshalYAML, because the transition check called String() on a nil SlackAPIURL. The previous commit avoided the panic but let the combination load silently. The file contents can't be compared with slack_app_url at load time, so the transition exception can't apply. Reject the combination with the existing "at most one of" error, and drop the now-unreachable slack_api_url_file clause from the transition check. Also add a test that reloads a redacted Config.String() with update_message and post_updates_to_thread enabled. Signed-off-by: TheRealNoob --- config/config.go | 25 +++++++++++++++---------- config/config_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 4becc99287..dd0cbd2d2a 100644 --- a/config/config.go +++ b/config/config.go @@ -341,16 +341,21 @@ func (c *Config) UnmarshalYAML(unmarshal func(any) error) error { return errors.New("at most one of slack_api_url & slack_api_url_file must be configured") } - // A redacted SlackAPIURL (e.g. from /api/v2/status) deserialises as - // &url.URL{}, whose String() is "". The real value is unknown. - if c.Global.SlackAPIURL != nil && c.Global.SlackAPIURL.String() != "" { - // Support transition from workaround suggested in https://github.com/prometheus/alertmanager/issues/2513, - // where users might set `slack_api_url` at the top level and then have `http_config` with individual - // bearer tokens in the receivers. - if (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) && (c.Global.SlackAPIURL != nil || len(c.Global.SlackAPIURLFile) > 0) { - if c.Global.SlackAPIURL.String() != c.Global.SlackAppURL.String() { - return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") - } + // slack_api_url_file can't be compared with slack_app_url at load time, + // so the transition check below can't apply. Reject the combination. + if len(c.Global.SlackAPIURLFile) > 0 && (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) { + return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") + } + + // Support transition from workaround suggested in https://github.com/prometheus/alertmanager/issues/2513, + // where users might set `slack_api_url` at the top level and then have `http_config` with individual + // bearer tokens in the receivers. + // + // We test SlackAPIURL.String() value because a redacted URL (e.g. from /api/v2/status) + // deserialises as &url.URL{}, whose String() is "". The real value is unknown, so we can't test its contents. + if (c.Global.SlackAppToken != "" || len(c.Global.SlackAppTokenFile) > 0) && c.Global.SlackAPIURL != nil && c.Global.SlackAPIURL.String() != "" { + if c.Global.SlackAPIURL.String() != c.Global.SlackAppURL.String() { + return errors.New("at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") } } diff --git a/config/config_test.go b/config/config_test.go index 40e3bee692..32ba8a69a9 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1451,6 +1451,46 @@ receivers: require.NoError(t, err) } +func TestSlackRedactedConfigReload(t *testing.T) { + // Config.String() redacts api_url, as served by /api/v2/status. + // Loading that output must not fail the bot-token validation. + in := ` +global: + slack_api_url: https://slack.com/api/chat.postMessage +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts + update_message: true + post_updates_to_thread: true +` + conf, err := Load(in) + require.NoError(t, err) + + _, err = Load(conf.String()) + require.NoError(t, err) +} + +func TestSlackGlobalAPIURLFileWithAppToken(t *testing.T) { + // slack_api_url_file can't be compared with slack_app_url at load time, + // so it can't be combined with slack_app_token. + in := ` +global: + slack_app_token: xoxb-token + slack_api_url_file: /etc/alertmanager/slack-url +route: + receiver: foo +receivers: + - name: foo + slack_configs: + - channel: alerts +` + _, err := Load(in) + require.EqualError(t, err, "at most one of slack_app_token/slack_app_token_file & slack_api_url/slack_api_url_file must be configured") +} + func TestSlackGlobalAppToken(t *testing.T) { conf, err := LoadFile("testdata/conf.slack-default-app-token.yml") if err != nil {