Skip to content

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

Align hardening with x.63 backport (#80066)

Nicola Mometto50a3a1177d58
diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
index a2a8b745d4c..fef4a8a77d5 100644
--- a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
@@ -35,7 +35,7 @@
[:sequential
[:map
[:name ms/NonBlankString]
- [:details ms/KeywordizedMap]]]]]]
+ [:details ms/Map]]]]]]
(api/check-400 (t2/exists? :model/DatabaseRouter :database_id router_database_id))
(api/check-400 (not (t2/exists? :model/Database :router_database_id router_database_id :name [:in (map :name destinations)]))
"A destination database with that name already exists.")
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 f1ed51ffcbe..eedaf9c1613 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
@@ -2,6 +2,8 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.channel.email.messages :as messages]
+ [metabase.notification.test-util :as notification.tu]
[metabase.test :as mt]
[metabase.util :as u]
[toucan2.core :as t2]))
@@ -43,3 +45,35 @@
(thunk))))
(testing "should succeed"
(is (thunk)))))))))))
+
+(defn- api:unsubscribe-undo
+ [expected-status handler-id email]
+ (mt/client :post expected-status "notification/unsubscribe/undo"
+ {:notification-handler-id handler-id
+ :email email
+ :hash (messages/generate-notification-unsubscribe-hash handler-id email)}))
+
+(deftest notification-recipient-validate-email-domains-test
+ (testing "every write path of a raw-value NotificationRecipient enforces subscription-allowed-domains (SEC-662)"
+ (mt/with-premium-features #{:email-allow-list}
+ (mt/with-temporary-setting-values [subscription-allowed-domains "metabase.com"]
+ (notification.tu/with-card-notification
+ [notification {:handlers [{:channel_type :channel/email
+ :recipients []}]}]
+ (let [handler-id (-> notification :handlers first :id)]
+ (testing "direct insert of a disallowed email fails"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"The following email addresses are not allowed: .*"
+ (t2/insert! :model/NotificationRecipient
+ {:type :notification-recipient/raw-value
+ :details {:value "[email protected]"}
+ :notification_handler_id handler-id}))))
+ (testing "POST /api/notification/unsubscribe/undo with a disallowed email fails"
+ (is (= "The following email addresses are not allowed: [email protected]"
+ (api:unsubscribe-undo 403 handler-id "[email protected]")))
+ (is (not (t2/exists? :model/NotificationRecipient :notification_handler_id handler-id))))
+ (testing "POST /api/notification/unsubscribe/undo with an allowed email succeeds"
+ (is (=? {:status "success"}
+ (api:unsubscribe-undo 200 handler-id "[email protected]")))
+ (is (t2/exists? :model/NotificationRecipient :notification_handler_id handler-id)))))))))
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index ef11d83952d..f5d570d662b 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -930,7 +930,7 @@
[:display {:optional true} [:maybe ::card-display]]
[:description {:optional true} [:maybe :string]]
[:collection_id {:optional true} [:maybe ms/PositiveInt]]
- [:visualization_settings {:optional true} [:maybe ms/KeywordizedMap]]])
+ [:visualization_settings {:optional true} [:maybe ms/Map]]])
(mr/def ::create-question-response
[:map
@@ -980,7 +980,7 @@
[:display {:optional true} [:maybe ::card-display]]
[:description {:optional true} [:maybe :string]]
[:collection_id {:optional true} [:maybe ms/PositiveInt]]
- [:visualization_settings {:optional true} [:maybe ms/KeywordizedMap]]])
+ [:visualization_settings {:optional true} [:maybe ms/Map]]])
(mr/def ::create-metric-response
[:map
@@ -1046,7 +1046,7 @@
[:description {:optional true} [:maybe :string]]
[:collection_id {:optional true} [:maybe ms/PositiveInt]]
[:display {:optional true} [:maybe ::card-display]]
- [:visualization_settings {:optional true} [:maybe ms/KeywordizedMap]]
+ [:visualization_settings {:optional true} [:maybe ms/Map]]
[:archived {:optional true} [:maybe :boolean]]
[:query {:optional true} [:maybe ms/NonBlankString]]])
@@ -1104,7 +1104,7 @@
[:description {:optional true} [:maybe :string]]
[:collection_id {:optional true} [:maybe ms/PositiveInt]]
[:display {:optional true} [:maybe ::card-display]]
- [:visualization_settings {:optional true} [:maybe ms/KeywordizedMap]]
+ [:visualization_settings {:optional true} [:maybe ms/Map]]
[:archived {:optional true} [:maybe :boolean]]
[:query {:optional true} [:maybe ms/NonBlankString]]])
diff --git a/src/metabase/app_db/honeysql_guard.clj b/src/metabase/app_db/honeysql_guard.clj
index 94ac743a57e..b0e337f82dc 100644
--- a/src/metabase/app_db/honeysql_guard.clj
+++ b/src/metabase/app_db/honeysql_guard.clj
@@ -82,10 +82,9 @@
[query]
(if (map? query)
(and (not (or (raw-honeysql-map? query) (inline-honeysql-map? query)))
- (every? safe-value? (vals (dissoc query :values :set :do-update-set)))
+ (every? safe-value? (vals (dissoc query :values :set)))
(every? safe-row? (:values query))
- (safe-row? (:set query))
- (safe-row? (:do-update-set query)))
+ (safe-row? (:set query)))
(safe-value? query)))
(methodical/defmethod t2.pipeline/build :around [:toucan.query-type/select.exists :default clojure.lang.IPersistentMap]
diff --git a/src/metabase/channel/impl/http.clj b/src/metabase/channel/impl/http.clj
index 7092cb57d0c..1bb876e9c3e 100644
--- a/src/metabase/channel/impl/http.clj
+++ b/src/metabase/channel/impl/http.clj
@@ -24,7 +24,7 @@
[:map {:closed true}
[:url ms/Url]
[:auth-method [:enum "none" "header" "query-param" "request-body"]]
- [:auth-info {:optional true} ms/KeywordizedMap]
+ [:auth-info {:optional true} ms/Map]
;; used by the frontend to display the auth info properly
[:fe-form-type {:optional true} [:enum "api-key" "bearer" "basic" "none"]]
;; request method
diff --git a/src/metabase/notification/models.clj b/src/metabase/notification/models.clj
index 21ef3443449..6dadf8f06a5 100644
--- a/src/metabase/notification/models.clj
+++ b/src/metabase/notification/models.clj
@@ -419,17 +419,24 @@
[_email-addresses]
nil)
+(defn- validate-raw-value-email-domain!
+ "Enforce the `subscription-allowed-domains` allow-list on any write of a raw-value email recipient. Must run on both
+ insert and update so no write path (e.g. the unauthenticated unsubscribe-undo endpoint) can skip it."
+ [instance]
+ (when (and (= :notification-recipient/raw-value (:type instance))
+ (u/email? (get-in instance [:details :value])))
+ (validate-email-domains! [(get-in instance [:details :value])])))
+
(t2/define-before-insert :model/NotificationRecipient
[instance]
(check-valid-recipient instance)
+ (validate-raw-value-email-domain! instance)
instance)
(t2/define-before-update :model/NotificationRecipient
[instance]
(check-valid-recipient instance)
- (when (and (= :notification-recipient/raw-value (:type instance))
- (u/email? (get-in instance [:details :value])))
- (validate-email-domains! [(get-in instance [:details :value])]))
+ (validate-raw-value-email-domain! instance)
instance)
;; ------------------------------------------------------------------------------------------------;;
diff --git a/src/metabase/search/appdb/specialization/postgres.clj b/src/metabase/search/appdb/specialization/postgres.clj
index 5d61a52ac93..fee83b8344c 100644
--- a/src/metabase/search/appdb/specialization/postgres.clj
+++ b/src/metabase/search/appdb/specialization/postgres.clj
@@ -35,7 +35,8 @@
{:insert-into table
:values entries
:on-conflict [:model :model_id]
- :do-update-set (zipmap update-keys (map excluded-kw update-keys))}))))
+ :do-update-set (with-meta (zipmap update-keys (map excluded-kw update-keys))
+ {:allow-subquery true})}))))
(defmethod specialization/base-query :postgres
[active-table search-term search-ctx select-items]
diff --git a/src/metabase/util/malli/schema.clj b/src/metabase/util/malli/schema.clj
index 73558ddd937..adb237bb5ae 100644
--- a/src/metabase/util/malli/schema.clj
+++ b/src/metabase/util/malli/schema.clj
@@ -4,7 +4,6 @@
For example the PositiveInt can be defined as (mr/def ::positive-int pos-int?)"
(:require
[clojure.string :as str]
- [clojure.walk :as walk]
[malli.util :as mut]
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
@@ -221,15 +220,6 @@
[:map {:closed false}]
(deferred-tru "Value must be a map.")))
-(def KeywordizedMap
- "Schema for an open map whose incoming string keys are normalized to keywords on API decode."
- (mu/with-api-error-message
- [:map {:closed false
- :decode/api (fn [m]
- (cond-> m
- (map? m) walk/keywordize-keys))}]
- (deferred-tru "Value must be a map.")))
-
(def Email
"Schema for a valid email string."
(mu/with-api-error-message
diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.clj
index 52b52dfa929..5b953b3a627 100644
--- a/src/metabase/warehouses_rest/api.clj
+++ b/src/metabase/warehouses_rest/api.clj
@@ -921,7 +921,7 @@
:- [:map
[:name ms/NonBlankString]
[:engine DBEngineString]
- [:details ms/KeywordizedMap]
+ [:details ms/Map]
[:is_full_sync {:default true} [:maybe ms/BooleanValue]]
[:is_on_demand {:default false} [:maybe ms/BooleanValue]]
[:schedules {:optional true} [:maybe sync.schedules/ExpandedSchedulesMap]]
@@ -986,7 +986,7 @@
{{:keys [engine details]} :details} :- [:map
[:details [:map
[:engine DBEngineString]
- [:details ms/KeywordizedMap]]]]]
+ [:details ms/Map]]]]]
(api/check-superuser)
(let [details-or-error (warehouses/test-connection-details engine details)]
;; details that come back without a `:valid` key at all are... valid!
@@ -1082,8 +1082,8 @@
[:name {:optional true} [:maybe ms/NonBlankString]]
[:engine {:optional true} [:maybe DBEngineString]]
[:refingerprint {:optional true} [:maybe :boolean]]
- [:details {:optional true} [:maybe ms/KeywordizedMap]]
- [:write_data_details {:optional true} [:maybe ms/KeywordizedMap]]
+ [:details {:optional true} [:maybe ms/Map]]
+ [:write_data_details {:optional true} [:maybe ms/Map]]
[:schedules {:optional true} [:maybe sync.schedules/ExpandedSchedulesMap]]
[:is_full_sync {:optional true} [:maybe ms/BooleanValue]]
[:is_on_demand {:optional true} [:maybe ms/BooleanValue]]
@@ -1093,7 +1093,7 @@
[:auto_run_queries {:optional true} [:maybe :boolean]]
[:cache_ttl {:optional true} [:maybe ms/PositiveInt]]
[:provider_name {:optional true} [:maybe :string]]
- [:settings {:optional true} [:maybe ms/KeywordizedMap]]
+ [:settings {:optional true} [:maybe ms/Map]]
[:is_stub {:optional true} [:maybe :boolean]]]]
(when (true? (:is_stub body))
(throw (ex-info (tru "is_stub may not be set via the API")