Skip to content

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

Require channel write permission to send to a channel by id (#82608)

applebyf58c520891e8
diff --git a/src/metabase/notification/api/notification.clj b/src/metabase/notification/api/notification.clj
index d91d39f7952..65beabcc972 100644
--- a/src/metabase/notification/api/notification.clj
+++ b/src/metabase/notification/api/notification.clj
@@ -61,9 +61,9 @@
(throw (ex-info "invalid template" {:status-code 400})))))
(defn- check-inline-channels!
- "Validate that an inline `:channel` handler requires the same permission as creating one."
+ "Validate that an inline `:channel` or `:channel_id` handler requires the same permission as creating one."
[handlers]
- (when (some :channel handlers)
+ (when (some #(or (:channel %) (:channel_id %)) handlers)
(api/check-403 (mi/can-write? :model/Channel))))
(defn- handler-touches-template?
diff --git a/test/metabase/notification/api/notification_test.clj b/test/metabase/notification/api/notification_test.clj
index 4acec0bdf5f..1f5036834bd 100644
--- a/test/metabase/notification/api/notification_test.clj
+++ b/test/metabase/notification/api/notification_test.clj
@@ -955,6 +955,28 @@
(testing "a non-admin user cannot"
(send! :rasta 403))))))))
+(deftest send-unsaved-notification-existing-channel-permissions-test
+ (testing "POST /api/notification/send naming an existing channel by channel_id requires channel write permission"
+ (mt/with-premium-features #{}
+ (mt/with-temp [:model/Card {card-id :id} {}
+ :model/Channel {chn-id :id} {:type :channel/http
+ :details {:url "https://example.com/webhook"
+ :auth-method "none"}}]
+ (let [send! (fn [user-or-id channel-id expected-status]
+ (mt/with-dynamic-fn-redefs [notification/send-notification! (fn [& _args] :done)]
+ (mt/user-http-request user-or-id :post expected-status "notification/send"
+ {:payload_type :notification/card
+ :handlers [{:channel_type :channel/http
+ :channel_id channel-id}]
+ :subscriptions []
+ :payload {:card_id card-id}})))]
+ (testing "an admin can send to an existing channel"
+ (send! :crowberto chn-id 200))
+ (testing "a non-admin user cannot"
+ (send! :rasta chn-id 403))
+ (testing "a non-admin user gets the same 403 for a channel id that doesn't exist"
+ (send! :rasta Integer/MAX_VALUE 403)))))))
+
(deftest list-notifications-basic-test
(testing "GET /api/notification"
(mt/with-model-cleanup [:model/Notification]