Skip to content

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

Better error handling on result_metadata.settings (#80909)

Nathan Voxland5c63c45970fb
diff --git a/src/metabase/models/visualization_settings.cljc b/src/metabase/models/visualization_settings.cljc
index e744c40887d..87d1603de76 100644
--- a/src/metabase/models/visualization_settings.cljc
+++ b/src/metabase/models/visualization_settings.cljc
@@ -29,6 +29,7 @@
[malli.core :as mc]
[medley.core :as m]
[metabase.lib.schema.id :as lib.schema.id]
+ [metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]))
@@ -446,7 +447,7 @@
(norm->db-generic-param-mapping k v))]
(assoc acc new-k new-v))) {} parameter-mapping)))
-(defn- db->norm-click-behavior [v]
+(defn- db->norm-click-behavior* [v]
(-> v
(assoc
::click-behavior-type
@@ -459,6 +460,21 @@
(dissoc :parameterMapping)
(set/rename-keys db->norm-click-behavior-keys)))
+(defn- db->norm-click-behavior
+ "Normalizes a DB-form click behavior, returning `nil` for one that can't be normalized.
+
+ Click behaviors are stored inside free-form JSON blobs (a card's `visualization_settings` and a column's
+ `settings`) that are only loosely validated on write, so anything at all can turn up here. Normalization assumes a
+ map; treat everything else as no click behavior rather than throwing, since the callers sit on the export and
+ static-viz render paths where a throw kills the whole download."
+ [v]
+ (when (map? v)
+ (try
+ (db->norm-click-behavior* v)
+ (catch #?(:clj Exception :cljs js/Error) e
+ (log/warnf "Ignoring malformed click behavior: %s" (ex-message e))
+ nil))))
+
(defn- db->norm-time-style
"Converts the deprecated k:mm format to HH:mm (#18112)"
[v]
@@ -466,66 +482,93 @@
"HH:mm"
v))
-(defn- db->norm-table-columns [v]
- (-> v
- (assoc ::table-columns (mapv (fn [tbl-col]
- (set/rename-keys tbl-col db->norm-table-columns-keys))
- (:table.columns v)))
- (dissoc :table.columns)))
+(defn- db->norm-table-columns
+ "Normalizes the `:table.columns` entry of a DB-form viz settings map.
+
+ `:table.columns` is a free-form JSON blob that is only validated as far as its enclosing map, so it need not be a
+ sequence of maps; anything it isn't is dropped rather than allowed to throw."
+ [v]
+ (let [tbl-cols (:table.columns v)]
+ (cond-> (dissoc v :table.columns)
+ (sequential? tbl-cols)
+ (assoc ::table-columns (into []
+ (comp (filter map?)
+ (map #(set/rename-keys % db->norm-table-columns-keys)))
+ tbl-cols)))))
(defn- db->norm-column-settings-entry
"Converts the DB form of a :column_settings entry value to its normalized form. Does the opposite of
- `norm->db-column-settings-entry`."
+ `norm->db-column-settings-entry`.
+
+ Entry values are only loosely validated on write (`result_metadata[].settings` types them as `:any`), so an entry
+ that cannot be normalized is dropped rather than allowed to throw. CSV, JSON and XLSX export and the static-viz
+ render path all normalize a column's settings through here, so one unnormalizable entry would otherwise take all
+ of them down."
[m k v]
- (case k
- :click_behavior
- (assoc m ::click-behavior (db->norm-click-behavior v))
+ (try
+ (case k
+ :click_behavior
+ (m/assoc-some m ::click-behavior (db->norm-click-behavior v))
- :time_style
- (assoc m ::time-style (db->norm-time-style v))
+ :time_style
+ (assoc m ::time-style (db->norm-time-style v))
- (assoc m (db->norm-column-settings-keys k) v)))
+ (assoc m (db->norm-column-settings-keys k) v))
+ (catch #?(:clj Exception :cljs js/Error) e
+ (log/warnf "Ignoring malformed column setting %s: %s" (pr-str k) (ex-message e))
+ m)))
(defn db->norm-column-settings-entries
- "Converts the DB form of a map of :column_settings entries to its normalized form."
+ "Converts the DB form of a map of :column_settings entries to its normalized form. Drops any entries that fail to be
+ normalized."
[entries]
- (reduce-kv db->norm-column-settings-entry {} entries))
+ (if (map? entries)
+ (reduce-kv db->norm-column-settings-entry {} entries)
+ {}))
(defn db->norm-column-settings
- "Converts a :column_settings DB form to its normalized form. Drops any columns that fail to be parsed."
+ "Converts a :column_settings DB form to its normalized form. Drops any columns that fail to be parsed, and treats a
+ `settings` that isn't a map as having no column settings at all — the per-column `catch` below can't help there,
+ since `reduce-kv` throws before it ever reaches the reducing fn."
[settings]
- (reduce-kv (fn [m k v]
- (try
- (let [k' (parse-db-column-ref k)
- v' (db->norm-column-settings-entries v)]
- (assoc m k' v'))
- (catch #?(:clj Throwable :cljs js/Error) _e
- m)))
- {}
- settings))
+ (if-not (map? settings)
+ {}
+ (reduce-kv (fn [m k v]
+ (try
+ (let [k' (parse-db-column-ref k)
+ v' (db->norm-column-settings-entries v)]
+ (assoc m k' v'))
+ (catch #?(:clj Exception :cljs js/Error) _e
+ m)))
+ {}
+ settings)))
(defn db->norm
"Converts a DB form of visualization settings (i.e. map with key `:visualization_settings`) into the equivalent
normalized form (i.e. map with keys `::column-settings`, `::click-behavior`, etc.).
- Does the opposite of `norm->db`."
+ Does the opposite of `norm->db`.
+
+ `vs` comes out of a free-form JSON column, so a blob that isn't a map at all normalizes to no settings rather than
+ throwing on the `dissoc` below."
{:added "0.40.0"}
[vs]
- (cond-> vs
- ;; column_settings at top level; ex: table card
- (:column_settings vs)
- (assoc ::column-settings (->> (:column_settings vs)
- db->norm-column-settings))
-
- ;; click behavior key at top level; ex: non-table card
- (:click_behavior vs)
- (assoc ::click-behavior (db->norm-click-behavior (:click_behavior vs)))
-
- (:table.columns vs)
- db->norm-table-columns
-
- :always
- (dissoc :column_settings :click_behavior)))
+ (let [vs (if (map? vs) vs {})]
+ (cond-> vs
+ ;; column_settings at top level; ex: table card
+ (:column_settings vs)
+ (assoc ::column-settings (->> (:column_settings vs)
+ db->norm-column-settings))
+
+ ;; click behavior key at top level; ex: non-table card
+ (:click_behavior vs)
+ (m/assoc-some ::click-behavior (db->norm-click-behavior (:click_behavior vs)))
+
+ (:table.columns vs)
+ db->norm-table-columns
+
+ :always
+ (dissoc :column_settings :click_behavior))))
(defn- norm->db-click-behavior-value [v]
(-> v
diff --git a/test/metabase/models/visualization_settings_test.cljc b/test/metabase/models/visualization_settings_test.cljc
index 651879fd43b..bd26634189f 100644
--- a/test/metabase/models/visualization_settings_test.cljc
+++ b/test/metabase/models/visualization_settings_test.cljc
@@ -186,3 +186,40 @@
;; make sure all keywords have the right namespace in normalized form
(t/is (empty? (filter #(some? (namespace %)) (all-keywords to-db))))
(t/is (= db-form to-db)))))
+
+(t/deftest ^:parallel db->norm-column-settings-entries-malformed-entry-test
+ (t/testing "entries that can't be normalized are dropped rather than thrown (SEC-868)"
+ (doseq [bad-click-behavior ["x" [1 2] 42 true]]
+ (t/testing (str "click_behavior = " (pr-str bad-click-behavior))
+ (t/is (= {::mb.viz/column-title "Legit Title"}
+ (mb.viz/db->norm-column-settings-entries
+ {:click_behavior bad-click-behavior
+ :column_title "Legit Title"})))))
+ (t/testing "a well-formed entry is still normalized"
+ (t/is (= {::mb.viz/click-behavior {::mb.viz/click-behavior-type ::mb.viz/link
+ ::mb.viz/link-type ::mb.viz/url
+ ::mb.viz/link-text-template "http://example.com"}}
+ (mb.viz/db->norm-column-settings-entries
+ {:click_behavior {:type "link" :linkType "url" :linkTextTemplate "http://example.com"}}))))
+ (t/testing "a non-associative settings value normalizes to nothing"
+ (doseq [bad ["x" [1 2] 42]]
+ (t/is (= {} (mb.viz/db->norm-column-settings-entries bad)))))))
+
+(t/deftest ^:parallel db->norm-malformed-blob-test
+ (t/testing "malformed values in a card's visualization_settings are dropped rather than thrown (SEC-868)"
+ (doseq [k [:click_behavior :column_settings :table.columns]
+ bad ["x" 42 [1 2] true]]
+ (t/testing (str (pr-str k) " = " (pr-str bad))
+ (t/is (map? (mb.viz/db->norm {k bad})))))
+ (t/testing "well-formed sibling keys survive alongside a malformed one"
+ ;; an unusable :column_settings normalizes to no column settings, as it already did for unparseable column refs
+ (t/is (= {:other "kept", ::mb.viz/column-settings {}}
+ (mb.viz/db->norm {:column_settings "x", :other "kept"}))))
+ (t/testing "a blob that isn't a map at all normalizes to no settings"
+ (doseq [bad ["x" 42 [1 2]]]
+ (t/is (= {} (mb.viz/db->norm bad)))))
+ (t/testing "a nil :click_behavior entry is dropped, not normalized to a map of nils"
+ (t/is (= {} (mb.viz/db->norm-column-settings-entries {:click_behavior nil}))))
+ (t/testing "well-formed values still normalize"
+ (t/is (= {::mb.viz/table-columns [{::mb.viz/table-column-enabled true}]}
+ (mb.viz/db->norm {:table.columns [{:enabled true}]}))))))
diff --git a/test/metabase/query_processor/streaming/common_test.clj b/test/metabase/query_processor/streaming/common_test.clj
index b87f3e23692..b3bc8b43d16 100644
--- a/test/metabase/query_processor/streaming/common_test.clj
+++ b/test/metabase/query_processor/streaming/common_test.clj
@@ -65,3 +65,36 @@
{::mb.viz/column-title "Column Name Title"}}}
titles (streaming.common/column-titles ordered-cols viz-settings format-rows?)]
(is (= ["Column Name Title"] titles)))))))
+
+(deftest ^:parallel viz-settings-for-col-malformed-column-settings-test
+ (testing "a malformed value in a column's stored :settings is dropped instead of killing the export (SEC-868)"
+ (doseq [bad-click-behavior ["x" [1 2] 42 true]]
+ (testing (str "click_behavior = " (pr-str bad-click-behavior))
+ (let [col {:name "N"
+ :display_name "N"
+ :base_type :type/Integer
+ :effective_type :type/Integer
+ :field_ref [:field "N" {:base-type :type/Integer}]
+ :source :native
+ :settings {:click_behavior bad-click-behavior}}
+ settings (streaming.common/viz-settings-for-col col {})]
+ (is (map? settings))
+ (is (not (contains? settings ::mb.viz/click-behavior))))))
+ (testing "well-formed sibling entries in the same :settings map survive"
+ (let [col {:name "N"
+ :base_type :type/Integer
+ :field_ref [:field "N" {:base-type :type/Integer}]
+ :settings {:click_behavior "x"
+ :column_title "Legit Title"}}
+ settings (streaming.common/viz-settings-for-col col {})]
+ (is (= "Legit Title" (::mb.viz/column-title settings)))))
+ (testing "a well-formed :click_behavior is still normalized"
+ (let [col {:name "N"
+ :base_type :type/Integer
+ :field_ref [:field "N" {:base-type :type/Integer}]
+ :settings {:click_behavior {:type "link" :linkType "url" :linkTextTemplate "http://example.com"}}}
+ settings (streaming.common/viz-settings-for-col col {})]
+ (is (= {::mb.viz/click-behavior-type ::mb.viz/link
+ ::mb.viz/link-type ::mb.viz/url
+ ::mb.viz/link-text-template "http://example.com"}
+ (::mb.viz/click-behavior settings)))))))
diff --git a/test/metabase/query_processor/streaming_test.clj b/test/metabase/query_processor/streaming_test.clj
index 7d2566795b0..4847166ff95 100644
--- a/test/metabase/query_processor/streaming_test.clj
+++ b/test/metabase/query_processor/streaming_test.clj
@@ -779,3 +779,38 @@
;; Should not throw any assertion errors
(is (some? (qp.streaming/-streaming-response :csv "test" mock-qp-fn))
"Streaming response should handle cancellation without assertion error"))))
+
+(deftest ^:parallel export-survives-malformed-model-column-settings-test
+ (testing (str "A model whose pinned result_metadata carries a malformed column `:settings` blob still exports. "
+ "Any user with collection-write could plant one and kill CSV/JSON/XLSX export of that model for "
+ "every later reader, including superusers and anonymous public viewers (SEC-868).")
+ (doseq [bad-click-behavior ["x" [1 2] 42]
+ export-format [:csv :json :xlsx]]
+ (testing (str export-format ", click_behavior = " (pr-str bad-click-behavior))
+ (let [query (assoc (mt/native-query {:query "SELECT 1 AS N"})
+ :info {:metadata/model-metadata
+ [{:name "N"
+ :display_name "N"
+ :base_type :type/Integer
+ :effective_type :type/Integer
+ :field_ref [:field "N" {:base-type :type/Integer}]
+ :source :native
+ :settings {:click_behavior bad-click-behavior}}]})
+ rows (streaming.test-util/process-query-basic-streaming export-format query ["N"])]
+ (is (= 1 (count (cond-> rows (= export-format :csv) rest)))
+ "the export should contain the one data row, not die mid-stream"))))))
+
+(deftest ^:parallel export-survives-malformed-card-visualization-settings-test
+ (testing (str "A card whose `visualization_settings` carry a malformed value still exports. Same root cause as the "
+ "model `result_metadata[].settings` blob, on the sibling `db->norm` path — the card API validates "
+ "`visualization_settings` only as a map, so every value under it is attacker-controlled (SEC-868).")
+ (doseq [k [:click_behavior :column_settings :table.columns]
+ bad-value ["x" 42]
+ export-format [:csv :json :xlsx]]
+ (testing (str export-format ", " k " = " (pr-str bad-value))
+ (let [query (-> (mt/native-query {:query "SELECT 1 AS N"})
+ (assoc :viz-settings {k bad-value}
+ :middleware {:process-viz-settings? true}))
+ rows (streaming.test-util/process-query-basic-streaming export-format query ["N"])]
+ (is (= 1 (count (cond-> rows (= export-format :csv) rest)))
+ "the export should contain the one data row, not die mid-stream"))))))