The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Add validation and permission checks for content endpoints (#80277)
Nicola Momettodad470d19761
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj b/enterprise/backend/src/metabase_enterprise/dependencies/calculation.cljindex d0a70127d98..d6c55db0813 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj@@ -118,7 +118,8 @@vis-setting-target-ids (fn [link-type](into #{} (keep (fn [dashcard](let [cb (:click_behavior (:visualization_settings dashcard))]- (when (= (:linkType cb) link-type)+ (when (and (= (:linkType cb) link-type)+ (pos-int? (:targetId cb)))(:targetId cb))))dashcards)))vis-setting-card-ids (vis-setting-target-ids "question")@@ -128,7 +129,8 @@(map (fn [dashcard](keep (fn [[_col col-setting]](let [cb (:click_behavior col-setting)]- (when (= (:linkType cb) link-type)+ (when (and (= (:linkType cb) link-type)+ (pos-int? (:targetId cb)))(:targetId cb))))(:column_settings (:visualization_settings dashcard))))dashcards)))diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.clj b/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.cljindex 3dfc3d65747..79048bdc654 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.clj@@ -244,6 +244,13 @@:else node))children)))))+(defn- ensure-entity-id+ [id]+ (when-not (pos-int? id)+ (throw (ex-info "Dependency entity id must be a positive integer"+ {:status-code 400, :id id})))+ id)+(defn replace-dependencies!"Replace the dependencies of the entity of type `entity-type` with id `entity-id` withthe ones specified in `dependencies-by-type`. "@@ -262,7 +269,7 @@{:from_entity_type entity-type:from_entity_id entity-id:to_entity_type to-entity-type- :to_entity_id to-entity-id})]+ :to_entity_id (ensure-entity-id to-entity-id)})](t2/with-transaction [_conn](when (seq to-remove)(t2/delete! :model/Dependency :id [:in to-remove]))diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.cljindex 19d7527d696..bf118d7da8b 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.clj@@ -2,6 +2,7 @@(:require[clojure.test :refer [deftest is testing]][metabase-enterprise.dependencies.calculation :as calculation]+ [metabase.documents.prose-mirror :as prose-mirror][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]@@ -654,3 +655,25 @@(lib/aggregate (lib/sum-where quantity (lib/ref segment-meta))))}](is (= {:measure #{} :segment #{segment-id} :table #{orders-id}}(calculation/calculate-deps :measure measure)))))))))++(deftest ^:parallel non-integer-ids-are-discarded-during-extraction-test+ (testing "a document smartLink whose entityId is a map is dropped rather than forwarded"+ (is (empty? (#'calculation/document-deps+ {:content_type prose-mirror/prose-mirror-content-type+ :document {:type "doc"+ :content [{:type "smartLink"+ :attrs {:model "card" :entityId {:raw "x"}}}]}}))))+ (testing "a dashcard click_behavior whose targetId is a map is dropped"+ (let [deps (calculation/calculate-deps*+ :dashboard+ {:dashcards [{:visualization_settings+ {:click_behavior {:linkType "question"+ :targetId {:raw "x"}}}}]})]+ (is (empty? (:card deps)))+ (is (empty? (:dashboard deps)))))+ (testing "a legitimate integer targetId is still collected"+ (let [deps (calculation/calculate-deps*+ :dashboard+ {:dashcards [{:visualization_settings+ {:click_behavior {:linkType "question" :targetId 7777}}}]})]+ (is (= #{7777} (:card deps))))))diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.cljindex a4e116cdd1d..47dd34d4808 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.clj@@ -350,3 +350,16 @@:to_entity_type :table:to_entity_id (:id table2)))"Should have exactly one dependency to table2"))))))++(deftest replace-dependencies!-rejects-non-integer-ids-test+ (testing "a non-integer to-entity-id is rejected before it is inserted"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be a positive integer"+ (deps.graph/replace-dependencies! "document" 1 {"card" #{{:raw "x"}}}))))+ (testing "legitimate integer ids are accepted and inserted"+ (mt/with-temp [:model/Card card {}]+ (mt/with-model-cleanup [:model/Dependency]+ (deps.graph/replace-dependencies! "card" (:id card) {"card" #{(:id card)}})+ (is (pos? (t2/count :model/Dependency+ :from_entity_type "card"+ :from_entity_id (:id card))))))))diff --git a/src/metabase/dashboards/models/dashboard_card.clj b/src/metabase/dashboards/models/dashboard_card.cljindex f22455858e4..75f5775454f 100644--- a/src/metabase/dashboards/models/dashboard_card.clj+++ b/src/metabase/dashboards/models/dashboard_card.clj@@ -29,12 +29,52 @@:visualization_settings mi/transform-visualization-settings:inline_parameters mi/transform-json})+(defn- ensure-integer-link-card-id+ [id]+ (when-not (integer? id)+ (throw (ex-info "Link card entity id must be an integer"+ {:status-code 400, :id id})))+ id)++(defn- validate-link-card-entity-id+ "Require the link-card entity id to be an integer before it is stored. It is used as an id when the linked+ entity is looked up on read, so validating on write keeps a malformed value from being persisted."+ [dashcard]+ (when-let [id (get-in dashcard [:visualization_settings :link :entity :id])]+ (ensure-integer-link-card-id id))+ dashcard)++(defn- validate-click-behavior-target-ids+ "Require click-behavior target ids to be integers before they are stored. They are later used as entity ids+ (e.g. by the dependency backfill), so validating on write keeps a malformed value from being persisted."+ [dashcard]+ (let [viz (:visualization_settings dashcard)]+ (doseq [cb (cons (:click_behavior viz)+ (map (comp :click_behavior val) (:column_settings viz)))+ :let [id (:targetId cb)]+ :when (some? id)]+ (when-not (integer? id)+ (throw (ex-info "Click behavior target id must be an integer"+ {:status-code 400, :id id})))))+ dashcard)++(defn- validate-dashcard-on-write+ [dashcard]+ (-> dashcard+ validate-link-card-entity-id+ validate-click-behavior-target-ids))+(t2/define-before-insert :model/DashboardCard[dashcard]- (merge {:parameter_mappings []- :visualization_settings {}- :inline_parameters []}- dashcard))+ (-> (merge {:parameter_mappings []+ :visualization_settings {}+ :inline_parameters []}+ dashcard)+ validate-dashcard-on-write))++(t2/define-before-update :model/DashboardCard+ [dashcard]+ (validate-dashcard-on-write dashcard));;; Update visualizer dashboard cards in stats to have card id references instead of entity ids(t2/define-after-select :model/DashboardCard@@ -324,13 +364,6 @@(def ^:private link-card-models(set (keys serdes/link-card-model->toucan-model)))-(defn- ensure-integer-link-card-id- [id]- (when-not (integer? id)- (throw (ex-info "Link card entity id must be an integer"- {:status-code 400, :id id})))- id)-(defn link-card-info-query-for-model"Return a honeysql query that is used to fetch info for a linkcard."[model id-or-ids]diff --git a/src/metabase/notification/payload/execute.clj b/src/metabase/notification/payload/execute.cljindex 282d0b5db5d..a089fbfabdf 100644--- a/src/metabase/notification/payload/execute.clj+++ b/src/metabase/notification/payload/execute.clj@@ -92,20 +92,26 @@This function should be executed under pulse's creator permissions (`with-current-user`)."[dashcard](assert api/*current-user-id* "Makes sure you wrapped this with a `with-current-user`.")- (let [link-card (get-in dashcard [:visualization_settings :link])]- (cond- (some? (:url link-card))- (link-card->text-part link-card)-- ;; if link card link to an entity, update the setting because- ;; the info in viz-settings might be out-of-date- (some? (:entity link-card))- (let [{:keys [model id]} (:entity link-card)- instance (t2/select-one- (serdes/link-card-model->toucan-model model)- (dashboard-card/link-card-info-query-for-model model id))]- (when (mi/can-read? instance)- (link-card->text-part (assoc link-card :entity instance)))))))+ ;; Degrade a single unrenderable link card to a missing part instead of aborting the whole dashboard's+ ;; parts. `dashcards->part` realizes the sequence eagerly, so an uncaught throw here takes out every part.+ (try+ (let [link-card (get-in dashcard [:visualization_settings :link])]+ (cond+ (some? (:url link-card))+ (link-card->text-part link-card)++ ;; if link card link to an entity, update the setting because+ ;; the info in viz-settings might be out-of-date+ (some? (:entity link-card))+ (let [{:keys [model id]} (:entity link-card)+ instance (t2/select-one+ (serdes/link-card-model->toucan-model model)+ (dashboard-card/link-card-info-query-for-model model id))]+ (when (mi/can-read? instance)+ (link-card->text-part (assoc link-card :entity instance))))))+ (catch Throwable e+ (log/warn e "Error rendering link card; skipping it")+ nil)))(defn- resolve-inline-parameters"Resolves the full parameter definitions for inline parameters on a dashcard, and adds them to the dashcard'sdiff --git a/src/metabase/query_processor/api.clj b/src/metabase/query_processor/api.cljindex 291fcb831b6..b4254f3baa2 100644--- a/src/metabase/query_processor/api.clj+++ b/src/metabase/query_processor/api.clj@@ -26,7 +26,9 @@[metabase.query-processor.middleware.constraints :as qp.constraints][metabase.query-processor.middleware.permissions :as qp.perms][metabase.query-processor.pivot :as qp.pivot]+ [metabase.query-processor.preprocess :as qp.preprocess][metabase.query-processor.schema :as qp.schema]+ [metabase.query-processor.setup :as qp.setup][metabase.query-processor.streaming :as qp.streaming][metabase.server.core :as server][metabase.util :as u]@@ -201,15 +203,29 @@[:database ms/PositiveInt][:pretty {:default true} [:maybe :boolean]]]](model-persistence/with-persisted-substituion-disabled- (let [query (lib-be/normalize-query (dissoc query :pretty))]+ (let [query (-> (lib-be/normalize-query (dissoc query :pretty))+ (dissoc :constraints :middleware)+ lib/disable-default-limit)](qp.perms/check-current-user-has-adhoc-native-query-perms query)- (let [driver (driver.u/database->driver database)- prettify (partial driver/prettify-native-form driver)- compiled (qp.compile/compile-with-inline-parameters (-> query- (dissoc :constraints :middleware)- lib/disable-default-limit))]- (cond-> compiled- pretty (update :query prettify))))))+ (qp.setup/with-qp-setup [query query]+ (binding [driver/*compile-with-inline-parameters* true]+ ;; Preprocess once, then run the same permission checks the run path (execute chain) runs, so both+ ;; endpoints agree on which referenced cards and tables the caller may use. Preprocessing resolves+ ;; `card__N` source tables and card/snippet template tags, so the referenced entities are known by+ ;; the time we check.+ (let [preprocessed (qp.preprocess/preprocess query)]+ (try+ (qp.perms/check-query-permissions* preprocessed)+ (catch clojure.lang.ExceptionInfo e+ (throw (if (:permissions-error? (ex-data e))+ (ex-info (ex-message e) (assoc (ex-data e) :status-code 403) e)+ e))))+ (let [compiled (qp.compile/compile-preprocessed preprocessed)+ driver (driver.u/database->driver database)]+ ;; Return only the compiled query and its params, not the internal keys the compiler carries+ ;; through (e.g. :lib/type, :query-permissions/referenced-card-ids).+ (-> (select-keys compiled [:query :params])+ (cond-> pretty (update :query #(driver/prettify-native-form driver %)))))))))))(api.macros/defendpoint :post "/pivot":- (server/streaming-response-schema ::qp.schema/query-result)diff --git a/test/metabase/dashboards/models/dashboard_card_test.clj b/test/metabase/dashboards/models/dashboard_card_test.cljindex 1154a1b4c26..7133f243343 100644--- a/test/metabase/dashboards/models/dashboard_card_test.clj+++ b/test/metabase/dashboards/models/dashboard_card_test.clj@@ -1,6 +1,7 @@(ns metabase.dashboards.models.dashboard-card-test(:require[clojure.test :refer :all]+ [honey.sql :as sql][metabase.dashboards.models.dashboard :as dashboard][metabase.dashboards.models.dashboard-card :as dashboard-card][metabase.queries.models.card-test :as card-test]@@ -352,3 +353,81 @@(let [loaded (t2/select-one :model/DashboardCard :id (:id dc))](is (= [{:sourceId (str "card:" (:id src))}](get-in loaded [:visualization_settings :visualization :columnValuesMapping :COLUMN_1]))))))))++(deftest link-card-entity-id-validated-on-write-test+ (testing "a link-card entity id must be an integer before it is stored, and a legitimate id round-trips"+ (mt/with-temp [:model/Dashboard {dash-id :id} {}]+ (testing "a map id is rejected on insert"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be an integer"+ (t2/insert! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings+ {:virtual_card {:display "link"}+ :link {:entity {:id {:raw "x"} :model "card"}}}}))))+ (testing "the dashboard is still readable afterwards"+ (is (=? {:id dash-id} (mt/user-http-request :crowberto :get 200 (str "dashboard/" dash-id)))))+ (testing "a map id is rejected on update too, not just insert"+ (let [dc (t2/insert-returning-instance! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings {}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be an integer"+ (t2/update! :model/DashboardCard (:id dc)+ {:visualization_settings+ {:link {:entity {:id {:raw "x"} :model "card"}}}})))))+ (testing "a legitimate integer id still works"+ (mt/with-temp [:model/Card {card-id :id} {}]+ (is (some? (t2/insert! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings+ {:link {:entity {:id card-id :model "card"}}}}))))))))++(deftest click-behavior-target-id-validated-on-write-test+ (testing "a click-behavior target id must be an integer before it is stored, and a legitimate id round-trips"+ (mt/with-temp [:model/Dashboard {dash-id :id} {}]+ (testing "a map target id is rejected on insert"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be an integer"+ (t2/insert! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings+ {:click_behavior {:type "link" :linkType "question" :targetId {:raw "x"}}}}))))+ (testing "a map target id in column settings is rejected on insert"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be an integer"+ (t2/insert! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings+ {:column_settings+ {"[\"name\",\"abc\"]"+ {:click_behavior {:type "link" :linkType "dashboard" :targetId {:raw "x"}}}}}}))))+ (testing "a map target id is rejected on update too, not just insert"+ (let [dc (t2/insert-returning-instance! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings {}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be an integer"+ (t2/update! :model/DashboardCard (:id dc)+ {:visualization_settings+ {:click_behavior {:type "link" :linkType "question" :targetId {:raw "x"}}}})))))+ (testing "a legitimate integer target id still works"+ (mt/with-temp [:model/Card {card-id :id} {}]+ (is (some? (t2/insert! :model/DashboardCard+ {:dashboard_id dash-id :row 0 :col 0 :size_x 4 :size_y 4+ :visualization_settings+ {:click_behavior {:type "link" :linkType "question" :targetId card-id}+ :column_settings+ {"[\"name\",\"abc\"]"+ {:click_behavior {:type "link" :linkType "question" :targetId card-id}}}}}))))))))++(deftest ^:parallel link-card-id-compiles-as-query-parameter-test+ (testing "a non-integer id is rejected when building the link-card query"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must be an integer"+ (dashboard-card/link-card-info-query-for-model "card" {:raw "x"}))))+ (testing "single-id and collection forms both pass a legitimate id as a bind parameter"+ (doseq [ids [42 #{42} [42]]]+ (let [[query & params] (sql/format (dashboard-card/link-card-info-query-for-model "card" ids))]+ (is (re-find #"\?" query) "the id appears as a bind parameter")+ (is (= [42] params))))))diff --git a/test/metabase/query_processor/api_test.clj b/test/metabase/query_processor/api_test.cljindex c997881669c..5987193e17c 100644--- a/test/metabase/query_processor/api_test.clj+++ b/test/metabase/query_processor/api_test.clj@@ -21,6 +21,7 @@[metabase.lib.schema.id :as lib.schema.id][metabase.lib.test-util :as lib.tu][metabase.lib.util.unique-name-generator]+ [metabase.models.interface :as mi][metabase.parameters.custom-values :as custom-values][metabase.permissions.core :as perms][metabase.query-processor.api :as api.dataset]@@ -325,6 +326,40 @@#(str/split-lines (or (driver/prettify-native-form :h2 %)"error: no query generated")))))))))))))+(deftest native-checks-source-card-read-permission-test+ (testing "POST /api/dataset/native requires read permission on a referenced source Card"+ (mt/with-temp [:model/Collection coll {}+ :model/Card card {:collection_id (:id coll)+ :database_id (mt/id)+ :dataset_query (mt/native-query {:query "SELECT 1 AS n"})}]+ (perms/revoke-collection-permissions! (perms/all-users-group) coll)+ (mt/with-no-data-perms-for-all-users!+ (perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/view-data :unrestricted)+ (perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/create-queries :query-builder-and-native)+ (is (false? (mt/with-test-user :rasta (mi/can-read? card))))+ (testing "source-table card__N"+ (is (mt/user-http-request :rasta :post 403 "dataset/native"+ {:database (mt/id)+ :type "query"+ :query {:source-table (str "card__" (:id card))}})))+ (testing "card template tag"+ (is (mt/user-http-request :rasta :post 403 "dataset/native"+ {:database (mt/id)+ :type "native"+ :native {:query "SELECT * FROM {{#c}}"+ :template-tags {"#c" {:id "x" :name "#c" :display-name "c"+ :type "card" :card-id (:id card)}}}})))))))++(deftest native-compiles-readable-source-card-test+ (testing "POST /api/dataset/native returns the compiled query when the user can read the source Card"+ (mt/with-temp [:model/Card card {:database_id (mt/id)+ :dataset_query (mt/native-query {:query "SELECT 1 AS n"})}]+ (is (=? {:query some?}+ (mt/user-http-request :crowberto :post 200 "dataset/native"+ {:database (mt/id)+ :type "query"+ :query {:source-table (str "card__" (:id card))}}))))))+(deftest formatted-results-ignore-query-constraints(testing "POST /api/dataset/:format"(testing "Downloading CSV/JSON/XLSX results shouldn't be subject to the default query constraints (#9831)"