Skip to content

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

Better human validation of notification schemas (#77922)

Nicola Mometto37d96787a1f6
diff --git a/src/metabase/channel/models/channel.clj b/src/metabase/channel/models/channel.clj
index 52ecb6013b5..82d7e0f58f6 100644
--- a/src/metabase/channel/models/channel.clj
+++ b/src/metabase/channel/models/channel.clj
@@ -126,7 +126,7 @@
[:channel/email
[:map
[:details ::ChannelTemplateEmailDetails]]]
- [::mc/default :any]]])
+ [::mc/default [:map]]]])
(mr/def ::ChannelTemplateEmailDetailsUserProvided
"Email template details schema for API-provided templates. Only handlebars-text is allowed;
@@ -146,7 +146,7 @@
[:channel/email
[:map
[:details ::ChannelTemplateEmailDetailsUserProvided]]]
- [::mc/default :any]]])
+ [::mc/default [:map]]]])
(defn- check-valid-channel-template
[channel-template]
diff --git a/src/metabase/notification/models.clj b/src/metabase/notification/models.clj
index 330a31f8127..66fd0cdabde 100644
--- a/src/metabase/notification/models.clj
+++ b/src/metabase/notification/models.clj
@@ -576,6 +576,8 @@
(t2/insert! :model/NotificationSubscription (map #(assoc % :notification_id notification-id) subscriptions)))
(doseq [{:keys [recipients template] :as handler} handlers+recipients]
;; assert can either template_id exists, then template but be nil, and vice versa
+ (when (and template (not (map? template)))
+ (throw (ex-info "Channel template must be a map" {:status-code 400})))
(let [template-id (if template
(t2/insert-returning-pk! :model/ChannelTemplate template)
(:template_id handler))
diff --git a/test/metabase/notification/api/notification_test.clj b/test/metabase/notification/api/notification_test.clj
index 888809b214f..d874e896531 100644
--- a/test/metabase/notification/api/notification_test.clj
+++ b/test/metabase/notification/api/notification_test.clj
@@ -307,6 +307,38 @@
(fn [[handler]]
[(assoc handler :template resource-template)])))))))))
+(deftest api-validates-template-shape-test
+ (let [bad-template "not-a-template"]
+ (testing "POST /api/notification rejects a non-map template"
+ (mt/with-model-cleanup [:model/Notification :model/ChannelTemplate]
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (mt/user-http-request :rasta :post 400 "notification"
+ {:payload_type "notification/card"
+ :payload {:card_id card-id}
+ :handlers [{:channel_type "channel/email"
+ :template bad-template
+ :recipients [{:type "notification-recipient/user"
+ :user_id (mt/user->id :rasta)}]}]}))))
+ (testing "POST /api/notification/send rejects a non-map template"
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (mt/user-http-request :rasta :post 400 "notification/send"
+ {:payload_type "notification/card"
+ :payload {:card_id card-id
+ :send_condition "has_result"}
+ :handlers [{:channel_type "channel/email"
+ :template bad-template
+ :recipients [{:type "notification-recipient/user"
+ :user_id (mt/user->id :rasta)}]}]})))
+ (testing "PUT /api/notification/:id rejects a non-map template"
+ (notification.tu/with-card-notification
+ [notification {:handlers [{:channel_type "channel/email"
+ :recipients [{:type :notification-recipient/user
+ :user_id (mt/user->id :crowberto)}]}]}]
+ (mt/user-http-request :crowberto :put 400 (format "notification/%d" (:id notification))
+ (update notification :handlers
+ (fn [[handler]]
+ [(assoc handler :template bad-template)])))))))
+
(defn- update-cron-subscription
[{:keys [subscriptions] :as notification} new-schedule ui-display-type]
(assert (= 1 (count subscriptions)))