Skip to content

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

Check ChannelTemplate write permission on notification handler templates (#80469)

Nicola Mometto79c18e5f890e
diff --git a/src/metabase/notification/api/notification.clj b/src/metabase/notification/api/notification.clj
index fcb56df60d9..ca89752eeb7 100644
--- a/src/metabase/notification/api/notification.clj
+++ b/src/metabase/notification/api/notification.clj
@@ -71,6 +71,23 @@
(when (some :channel handlers)
(api/check-403 (mi/can-write? :model/Channel))))
+(defn- handler-touches-template?
+ [{:keys [template template_id]}]
+ (or (map? template) (some? template_id)))
+
+(defn- check-handler-templates!
+ "Validate the templates carried by a notification request's `handlers`: reject internal-only
+ handlebars-resource templates (400), and require the same permission as writing a `ChannelTemplate`
+ directly (403) for any template the request creates, overwrites, or deletes. `existing-handlers`
+ (nil on create) hold templates an update may overwrite or delete, so a template on either side
+ gates the write."
+ ([handlers] (check-handler-templates! handlers nil))
+ ([handlers existing-handlers]
+ (check-no-resource-templates! handlers)
+ (when (or (some handler-touches-template? handlers)
+ (some handler-touches-template? existing-handlers))
+ (api/check-403 (mi/can-write? :model/ChannelTemplate)))))
+
(defn get-notification
"Get a notification by id."
[id]
@@ -214,7 +231,7 @@
(api.macros/defendpoint :post "/" :- ::models.notification/FullyHydratedNotification
"Create a new notification, return the created notification."
[_route _query body :- ::CreateNotificationParams request]
- (check-no-resource-templates! (:handlers body))
+ (check-handler-templates! (:handlers body))
(create-notification!
(-> body
(update :payload_type keyword)
@@ -284,9 +301,9 @@
[{:keys [id]} :- [:map [:id ms/PositiveInt]]
_query
body :- ::NotificationApiUpdateInput]
- (check-no-resource-templates! (:handlers body))
(let [existing-notification (get-notification id)]
(api/update-check existing-notification body)
+ (check-handler-templates! (:handlers body) (:handlers existing-notification))
(let [body (body-with-authoritative-ids body existing-notification)]
(models.notification/update-notification! existing-notification body)
(u/prog1 (get-notification id)
diff --git a/test/metabase/notification/api/notification_test.clj b/test/metabase/notification/api/notification_test.clj
index 0755a936efa..68780cceb90 100644
--- a/test/metabase/notification/api/notification_test.clj
+++ b/test/metabase/notification/api/notification_test.clj
@@ -737,6 +737,79 @@
(change-notification-creator (mt/user->id :rasta))
(move-card-collection (mt/user->id :rasta))))))))))))
+(deftest create-notification-template-permission-test
+ (testing "POST /api/notification - attaching a ChannelTemplate requires the same permission as writing one directly"
+ (mt/with-premium-features #{}
+ (mt/with-model-cleanup [:model/Notification :model/ChannelTemplate]
+ (binding [collection/*allow-deleting-personal-collections* true]
+ (mt/with-user-in-groups [_group {:name "template-perms create"}
+ user [_group]]
+ (mt/with-temp
+ [:model/Collection {collection-id :id} {:personal_owner_id (:id user)}
+ :model/Card {card-id :id} {:collection_id collection-id}
+ :model/ChannelTemplate {system-template-id :id} notification.tu/channel-template-email-with-handlebars-body]
+ (let [inline-template (-> notification.tu/channel-template-email-with-handlebars-body
+ (update :channel_type u/qualified-name)
+ (update-in [:details :type] u/qualified-name))
+ create! (fn [user-or-id expected-status handler]
+ (mt/user-http-request user-or-id :post expected-status "notification"
+ {:payload_type "notification/card"
+ :payload {:card_id card-id}
+ :handlers [(merge {:channel_type :channel/email
+ :recipients [{:type :notification-recipient/user
+ :user_id (:id user)}]}
+ handler)]}))]
+ (testing "a handler with no template is unaffected - a card-viewer can still create"
+ (create! user 200 {}))
+ (testing "referencing an existing (system) template by id"
+ (testing "a non-admin who can view the card cannot"
+ (create! user 403 {:template_id system-template-id}))
+ (testing "an admin can"
+ (create! :crowberto 200 {:template_id system-template-id})))
+ (testing "attaching an inline template"
+ (testing "a non-admin who can view the card cannot"
+ (create! user 403 {:template inline-template}))
+ (testing "an admin can"
+ (create! :crowberto 200 {:template inline-template})))))))))))
+
+(deftest update-notification-template-permission-test
+ (testing "PUT /api/notification/:id - overwriting or deleting a bound ChannelTemplate requires template write permission"
+ (mt/with-premium-features #{}
+ (mt/with-temp [:model/ChannelTemplate {system-template-id :id} notification.tu/channel-template-email-with-handlebars-body]
+ (notification.tu/with-card-notification
+ [notification {:card {:collection_id (t2/select-one-pk :model/Collection :personal_owner_id (mt/user->id :rasta))}
+ :notification {:creator_id (mt/user->id :rasta)}
+ :handlers [{:channel_type :channel/email
+ :template_id system-template-id
+ :recipients [{:type :notification-recipient/user
+ :user_id (mt/user->id :rasta)}]}]}]
+ (let [notification-id (:id notification)
+ ;; the fully hydrated notification is the canonical PUT body
+ body (mt/user-http-request :crowberto :get 200 (format "notification/%d" notification-id))
+ put! (fn [user-or-id expected-status handlers-fn]
+ (mt/user-http-request user-or-id :put expected-status (format "notification/%d" notification-id)
+ ;; drop creator_id so echoing it back doesn't read as a (forbidden) reassignment
+ (-> body (dissoc :creator_id) (update :handlers handlers-fn))))
+ overwrite-with (fn [name']
+ (fn [[handler]]
+ [(assoc handler :template {:id system-template-id
+ :channel_type :channel/email
+ :name name'
+ :details {:type :email/handlebars-text
+ :subject "updated subject"
+ :body "<h1>updated body</h1>"}})]))
+ original (t2/select-one :model/ChannelTemplate system-template-id)]
+ (testing "the owner (non-admin) cannot overwrite the referenced template - the row is untouched"
+ (put! :rasta 403 (overwrite-with "renamed"))
+ (is (=? (select-keys original [:name :details])
+ (select-keys (t2/select-one :model/ChannelTemplate system-template-id) [:name :details]))))
+ (testing "the owner (non-admin) cannot delete the referenced template by omitting it"
+ (put! :rasta 403 (fn [[handler]] [(dissoc handler :template :template_id)]))
+ (is (true? (t2/exists? :model/ChannelTemplate system-template-id))))
+ (testing "an admin can overwrite the template"
+ (put! :crowberto 200 (overwrite-with "admin-renamed"))
+ (is (= "admin-renamed" (t2/select-one-fn :name :model/ChannelTemplate system-template-id))))))))))
+
(deftest send-saved-notification-permissions-test
(mt/with-temp [:model/User {third-user-id :id} {:is_superuser false}]
(notification.tu/with-card-notification