Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

humanize the alert management error when recipient input is wrong (#77852)

Nicola Momettoed3aa72e65a0
diff --git a/src/metabase/channel/impl/http.clj b/src/metabase/channel/impl/http.clj
index 9920ef11aea..23a8ba28425 100644
--- a/src/metabase/channel/impl/http.clj
+++ b/src/metabase/channel/impl/http.clj
@@ -1,6 +1,7 @@
(ns metabase.channel.impl.http
(:require
[clj-http.client :as http]
+ [clojure.string :as str]
[java-time.api :as t]
[metabase.channel.core :as channel]
[metabase.channel.render.core :as channel.render]
@@ -36,12 +37,24 @@
[:type [:= :channel/http]]
[:details HTTPDetails]])
+(defn- check-url!
+ [url]
+ (when (str/blank? url)
+ (throw (ex-info (tru "No URL is configured for this webhook.") {:status-code 400})))
+ (when-not (try
+ (u/valid-host? (channel.settings/http-channel-host-strategy) url)
+ (catch Exception e
+ (throw (ex-info (tru "Invalid webhook URL: {0}" (ex-message e))
+ {:status-code 400
+ :url url}
+ e))))
+ (throw (ex-info (tru "URLs referring to hosts that supply internal hosting metadata are prohibited.")
+ {:status-code 400}))))
+
(mu/defmethod channel/send! :channel/http
[{{:keys [url method auth-method auth-info]} :details} :- HTTPChannel
request]
- (when-not (u/valid-host? (channel.settings/http-channel-host-strategy) url)
- (throw (ex-info (tru "URLs referring to hosts that supply internal hosting metadata are prohibited.")
- {:status-code 400})))
+ (check-url! url)
(let [req (merge
{:accept :json
:content-type :json
diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.clj
index 74bc3455ad7..09ee8020c1e 100644
--- a/src/metabase/notification/send.clj
+++ b/src/metabase/notification/send.clj
@@ -10,6 +10,7 @@
[metabase.notification.settings :as notification.settings]
[metabase.task-history.core :as task-history]
[metabase.util :as u]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.retry :as retry]
@@ -76,6 +77,10 @@
:notification_id notification-id
:notification_type payload-type
:recipient_ids (map :id (:recipients handler))}}
+ (when (and (:channel_id handler) (nil? (:channel handler)))
+ (throw (ex-info (tru "The channel this notification is set to send to no longer exists or is inactive.")
+ {:channel-id (:channel_id handler)
+ :channel-type (:channel_type handler)})))
(retry/with-retry (assoc retry-config
:retry-on Exception
:abort-if (fn [_ ex]
diff --git a/test/metabase/channel/impl/http_test.clj b/test/metabase/channel/impl/http_test.clj
index 3a60a8d8d0c..aa99757228d 100644
--- a/test/metabase/channel/impl/http_test.clj
+++ b/test/metabase/channel/impl/http_test.clj
@@ -13,6 +13,7 @@
[metabase.server.middleware.json :as mw.json]
[metabase.test :as mt]
[metabase.util.i18n :refer [deferred-tru]]
+ [metabase.util.malli :as mu]
[ring.adapter.jetty :as jetty]
[ring.middleware.params :refer [wrap-params]]
[toucan2.core :as t2])
@@ -319,6 +320,23 @@
:page 1}})
(first @requests)))))))
+(deftest send!-humanized-invalid-url-test
+ (mu/disable-enforcement
+ (testing "a missing webhook URL throws a human-readable error rather than an NPE (#76802)"
+ (doseq [channel [{:type :channel/http}
+ {:type :channel/http :details {}}
+ {:type :channel/http :details {:url "" :auth-method "none"}}]]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"No URL is configured for this webhook"
+ (channel/send! channel nil)))))
+ (testing "an unparseable webhook URL throws a human-readable error (#76802)"
+ (mt/with-temporary-setting-values [http-channel-host-strategy :external-only]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"Invalid webhook URL"
+ (channel/send! {:type :channel/http
+ :details {:url "not-a-url" :auth-method "none"}}
+ nil)))))))
+
(deftest alert-http-channel-e2e-test
(mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
(let [received-message (atom nil)
diff --git a/test/metabase/notification/send_test.clj b/test/metabase/notification/send_test.clj
index 8e7378591e3..b75ecdbcfb2 100644
--- a/test/metabase/notification/send_test.clj
+++ b/test/metabase/notification/send_test.clj
@@ -195,6 +195,28 @@
[:message :string]]])}}
(latest-task-history-entry "channel-send"))))))))))
+(deftest notification-send-inactive-channel-humanized-error-test
+ (notification.tu/with-notification-testing-setup!
+ (mt/with-temp [:model/Channel chn notification.tu/default-can-connect-channel]
+ (let [n (models.notification/create-notification!
+ {:payload_type :notification/testing}
+ nil
+ [{:channel_type notification.tu/test-channel-type
+ :channel_id (:id chn)
+ :recipients [{:type :notification-recipient/user :user_id (mt/user->id :crowberto)}]}])]
+ (testing "sending to a deactivated channel fails with a human-readable error, without retries (#76802)"
+ (t2/update! :model/Channel (:id chn) {:active false})
+ (let [send-count (atom 0)]
+ (with-redefs [channel/send! (fn [& _] (swap! send-count inc))]
+ (#'notification.send/send-notification-sync! n))
+ (testing "channel/send! is never invoked"
+ (is (zero? @send-count))))
+ (is (=? {:task "channel-send"
+ :status :failed
+ :task_details {:message #"The channel this notification is set to send to no longer exists or is inactive\."
+ :attempted_retries 0}}
+ (latest-task-history-entry "channel-send"))))))))
+
(def ^:private fake-email-notification
{:subject "test-message"
:recipients ["[email protected]"]