Skip to content

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

Encrypt subscription/alert recipient details; enforce allow-list on send (#81227)

Alexander Polyankin8b478a76ab11
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/models/notification_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/models/notification_test.clj
index eedaf9c1613..4284632ee04 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_config/models/notification_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/models/notification_test.clj
@@ -3,6 +3,7 @@
[clojure.string :as str]
[clojure.test :refer :all]
[metabase.channel.email.messages :as messages]
+ [metabase.notification.models :as models.notification]
[metabase.notification.test-util :as notification.tu]
[metabase.test :as mt]
[metabase.util :as u]
@@ -77,3 +78,22 @@
(is (=? {:status "success"}
(api:unsubscribe-undo 200 handler-id "[email protected]")))
(is (t2/exists? :model/NotificationRecipient :notification_handler_id handler-id)))))))))
+
+(deftest validate-email-handlers!-test
+ (testing "the send-time check reuses validate-email-handlers!, which throws on a disallowed raw external recipient"
+ (let [handler (fn [email] {:channel_type :channel/email
+ :recipients [{:type :notification-recipient/raw-value :details {:value email}}
+ {:type :notification-recipient/user :user {:email "[email protected]"}}]})]
+ (mt/with-premium-features #{:email-allow-list}
+ (mt/with-temporary-setting-values [subscription-allowed-domains "metabase.com"]
+ (testing "throws for a disallowed external recipient"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"not allowed"
+ (models.notification/validate-email-handlers! [(handler "[email protected]")]))))
+ (testing "does not throw for an allowed external recipient (user recipients are never domain-checked)"
+ (is (nil? (models.notification/validate-email-handlers! [(handler "[email protected]")]))))))
+ (testing "no-op without the :email-allow-list feature"
+ (mt/with-premium-features #{}
+ (mt/with-temporary-setting-values [subscription-allowed-domains "metabase.com"]
+ (is (nil? (models.notification/validate-email-handlers! [(handler "[email protected]")])))))))))
diff --git a/resources/migrations/058_update_migrations.yaml b/resources/migrations/058_update_migrations.yaml
index fcc7f957aef..130057a1e01 100644
--- a/resources/migrations/058_update_migrations.yaml
+++ b/resources/migrations/058_update_migrations.yaml
@@ -1608,6 +1608,18 @@ databaseChangeLog:
- customChange:
class: metabase.app_db.custom_migrations.EncryptPublicUuids
+ - changeSet:
+ id: v58.2026-08-25T00:00:19
+ author: ranquild
+ comment: >-
+ Encrypt at rest the details column of notification_recipient and pulse_channel, which stores raw external email
+ recipients for alerts and dashboard subscriptions, so a plaintext value written directly to the app DB is
+ rejected on the strict read once MB_ENCRYPTION_SECRET_KEY is set. Already-encrypted values are left untouched.
+ On rollback the values are decrypted back to plaintext.
+ changes:
+ - customChange:
+ class: metabase.app_db.custom_migrations.EncryptNotificationAndPulseChannelDetails
+
# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<
########################################################################################################################
diff --git a/src/metabase/app_db/custom_migrations.clj b/src/metabase/app_db/custom_migrations.clj
index 0ee9a696ce9..386932fa617 100644
--- a/src/metabase/app_db/custom_migrations.clj
+++ b/src/metabase/app_db/custom_migrations.clj
@@ -2303,3 +2303,27 @@
(t2/reducible-query {:select [:id :public_uuid]
:from [table]
:where [:!= :public_uuid nil]})))))
+
+(define-reversible-migration EncryptNotificationAndPulseChannelDetails
+ (when (encryption/default-encryption-enabled?)
+ (doseq [table [:notification_recipient :pulse_channel]]
+ (run! (fn [{:keys [id details]}]
+ (when (and (string? details)
+ (not (encryption/possibly-encrypted-string? details)))
+ (t2/query {:update table
+ :set {:details (encryption/maybe-encrypt details)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :details]
+ :from [table]
+ :where [:!= :details nil]}))))
+ (when (encryption/default-encryption-enabled?)
+ (doseq [table [:notification_recipient :pulse_channel]]
+ (run! (fn [{:keys [id details]}]
+ (when (and (string? details)
+ (encryption/possibly-encrypted-string? details))
+ (t2/query {:update table
+ :set {:details (encryption/maybe-decrypt details)}
+ :where [:= :id id]})))
+ (t2/reducible-query {:select [:id :details]
+ :from [table]
+ :where [:!= :details nil]})))))
diff --git a/src/metabase/app_db/encryption.clj b/src/metabase/app_db/encryption.clj
index 0b73b58cffa..e7227388c3f 100644
--- a/src/metabase/app_db/encryption.clj
+++ b/src/metabase/app_db/encryption.clj
@@ -39,7 +39,9 @@
[:report_card :public_uuid]
[:report_dashboard :public_uuid]
[:action :public_uuid]
- [:document :public_uuid]])
+ [:document :public_uuid]
+ [:notification_recipient :details]
+ [:pulse_channel :details]])
(def ^:private encrypted-bytes-columns
"`^bytes` columns encrypted at rest via `mi/transform-secret-value` (a strict `maybe-decrypt-bytes` on read). Unlike
diff --git a/src/metabase/notification/models.clj b/src/metabase/notification/models.clj
index aae129f9542..2140302e5eb 100644
--- a/src/metabase/notification/models.clj
+++ b/src/metabase/notification/models.clj
@@ -365,7 +365,7 @@
(t2/deftransforms :model/NotificationRecipient
{:type (mi/transform-validator mi/transform-keyword (partial mi/assert-enum notification-recipient-types))
- :details mi/transform-json})
+ :details mi/transform-encrypted-json})
(defn- notification-recipient-schema
[{:keys [with-id?]}]
diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.clj
index d1aada9d2d4..fb2624f5640 100644
--- a/src/metabase/notification/send.clj
+++ b/src/metabase/notification/send.clj
@@ -158,6 +158,12 @@
{:notification-id id}))))
(let [hydrated-notification (hydrate-notification notification-info)
handlers (:handlers hydrated-notification)]
+ (try
+ (models.notification/validate-email-handlers! handlers)
+ (catch clojure.lang.ExceptionInfo _e
+ (throw (ex-info "A subscription recipient is not permitted by subscription-allowed-domains"
+ {:status-code 403
+ :notification-id id}))))
(task-history/with-task-history {:task "notification-send"
:task_details {:notification_id id
:notification_handlers (map #(select-keys % [:id :channel_type :channel_id :template_id]) handlers)}}
diff --git a/src/metabase/pulse/models/pulse_channel.clj b/src/metabase/pulse/models/pulse_channel.clj
index f87dc383d41..eb97f122f17 100644
--- a/src/metabase/pulse/models/pulse_channel.clj
+++ b/src/metabase/pulse/models/pulse_channel.clj
@@ -121,7 +121,7 @@
(derive ::mi/write-policy.superuser))
(t2/deftransforms :model/PulseChannel
- {:details mi/transform-json
+ {:details mi/transform-encrypted-json
:channel_type mi/transform-keyword
:schedule_type mi/transform-keyword
:schedule_frame mi/transform-keyword})
diff --git a/test/metabase/app_db/custom_migrations_test.clj b/test/metabase/app_db/custom_migrations_test.clj
index a9b7e817a15..baf3bef1e32 100644
--- a/test/metabase/app_db/custom_migrations_test.clj
+++ b/test/metabase/app_db/custom_migrations_test.clj
@@ -2755,6 +2755,26 @@
(is (= "plaintext-bcrypt-hash" (raw-key plain-id)))
(is (= "another-bcrypt-hash" (raw-key enc-id)))))))))
+(deftest encrypt-notification-and-pulse-channel-details-test
+ (testing "v58.2026-08-25T00:00:19 encrypts pulse_channel details at rest"
+ (impl/test-migrations ["v58.2026-08-25T00:00:19"] [migrate!]
+ (let [user-id (:id (new-instance-with-default :core_user))
+ pulse-id (:id (new-instance-with-default :pulse {:creator_id user-id}))
+ pc-details (json/encode {:emails ["[email protected]"]})
+ pc-id (:id (new-instance-with-default :pulse_channel
+ {:pulse_id pulse-id
+ :channel_type "email"
+ :schedule_type "daily"
+ :details pc-details}))
+ raw-pc #(:details (t2/query-one {:select [:details] :from [:pulse_channel] :where [:= :id pc-id]}))]
+ (testing "plaintext before migration (written with no encryption key)"
+ (is (not (encryption/possibly-encrypted-string? (raw-pc)))))
+ (encryption-test/with-secret-key "dont-tell-anyone-about-this"
+ (migrate!)
+ (testing "encrypted after migration, and still decrypts to the original value"
+ (is (true? (encryption/possibly-encrypted-string? (raw-pc))))
+ (is (= pc-details (encryption/maybe-decrypt (raw-pc))))))))))
+
(deftest backfill-transform-target-db-id-test
(testing "v59.2026-01-31T12:01:23 : backfill target_db_id from target and source JSON"
(impl/test-migrations ["v59.2026-01-31T12:01:23"] [migrate!]