Skip to content

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

Only serialize Channel :details for callers who can write the channel (#80057)

bryan58c85c338cee
diff --git a/src/metabase/channel/models/channel.clj b/src/metabase/channel/models/channel.clj
index 1ebd6155447..f7bff7205ea 100644
--- a/src/metabase/channel/models/channel.clj
+++ b/src/metabase/channel/models/channel.clj
@@ -58,6 +58,15 @@
(or (mi/superuser?)
(perms/current-user-has-application-permissions? :setting)))
+(methodical/defmethod mi/to-json :model/Channel
+ "Only include `:details` for callers who can write the channel, matching `remove-details-if-needed` in the channel
+ API. Encoding at the model boundary keeps every response that returns a Channel consistent."
+ [channel json-generator]
+ (next-method (if (mi/can-write? channel)
+ channel
+ (dissoc channel :details))
+ json-generator))
+
(t2/define-before-update :model/Channel
[instance]
(let [deactivation? (false? (:active (t2/changes instance)))]
diff --git a/test/metabase/channel/models/channel_test.clj b/test/metabase/channel/models/channel_test.clj
index b56a55614b4..38bda841f9c 100644
--- a/test/metabase/channel/models/channel_test.clj
+++ b/test/metabase/channel/models/channel_test.clj
@@ -6,6 +6,7 @@
[metabase.test :as mt]
[metabase.util.encryption :as encryption]
[metabase.util.encryption-test :as encryption-test]
+ [metabase.util.json :as json]
[toucan2.core :as t2]))
(deftest channel-details-is-encrypted
@@ -14,6 +15,26 @@
(let [channel (t2/insert-returning-instance! :model/Channel notification.tu/default-can-connect-channel)]
(is (encryption/possibly-encrypted-string? (t2/select-one-fn :details :channel (:id channel))))))))
+(deftest channel-details-json-encoding-test
+ (testing "JSON-encoding a Channel includes :details only for callers who can write it"
+ (mt/with-temp
+ [:model/Channel channel {:name "prod-webhook"
+ :type :channel/http
+ :active true
+ :details {:url "https://example.com/hook"
+ :auth-method "header"
+ :auth-info {:Authorization "Bearer token-value"}}}]
+ (testing "a user who cannot write the channel gets no :details"
+ (mt/with-test-user :rasta
+ (let [encoded (json/encode channel)]
+ (is (not (re-find #"token-value" encoded)))
+ (is (nil? (:details (json/decode+kw encoded))))
+ (testing "but the rest of the channel is still present"
+ (is (re-find #"prod-webhook" encoded))))))
+ (testing "a user who can write the channel still gets :details"
+ (mt/with-test-user :crowberto
+ (is (re-find #"token-value" (json/encode channel))))))))
+
(deftest deactivate-channel-test
(mt/with-temp
[:model/Channel {id :id} notification.tu/default-can-connect-channel