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
Check permissions on nested cards (#82600)
Nathan Voxland61d0fa5d0f89
diff --git a/src/metabase/parameters/custom_values.clj b/src/metabase/parameters/custom_values.cljindex 946e410e437..8426c0aafd8 100644--- a/src/metabase/parameters/custom_values.clj+++ b/src/metabase/parameters/custom_values.clj@@ -134,13 +134,24 @@(let [keep-idxs (into [] (keep-indexed (fn [i c] (when-not (drop-names (:name c)) i))) cols)](perf/mapv (fn [row] (perf/mapv #(nth row %) keep-idxs)) rows)))))+(defn- run-values-query+ "Run a value-source `query` through the QP. Permission errors raised by the QP (e.g. the user cannot read a Card the+ value-source Card's query nests) carry no HTTP status, so surface them as a 403 rather than a 500."+ [query]+ (try+ (qp/process-query query)+ (catch clojure.lang.ExceptionInfo e+ (if (:permissions-error? (ex-data e))+ (throw (ex-info (ex-message e) {:status-code 403} e))+ (throw e)))))+(mu/defn- values-from-card* :- ms/FieldValuesResult"Core of [[values-from-card]], working off a prebuilt value-source `query`."[query :- [:maybe ::lib.schema/query]field-ref :- [:or :mbql.clause/field :mbql.clause/expression]opts :- [:maybe ::values-from-card-query.options]](let [mbql-query (values-from-card-query query field-ref opts)- result (some-> mbql-query qp/process-query)+ result (some-> mbql-query run-values-query)values (some-> result result->rows)]{:values (or values []);; If the row_count returned = the limit we specified, then it's probably has more than that.diff --git a/src/metabase/query_processor/middleware/permissions.clj b/src/metabase/query_processor/middleware/permissions.cljindex 89b6412f0a2..dacead30ff7 100644--- a/src/metabase/query_processor/middleware/permissions.clj+++ b/src/metabase/query_processor/middleware/permissions.clj@@ -101,8 +101,16 @@;; set when querying for field values of dashboard filters, which only require;; collection perms for the dashboard and not ad-hoc query perms*param-values-query*- (when-not (query-perms/has-perm-for-query? outer-query :perms/view-data required-perms)- (throw (query-perms/perms-exception required-perms)))+ (do+ ;; The value-source Card itself is read-checked by the caller, but its query may nest other Cards; the+ ;; user must be able to read every one of those too, just like when running the Card normally.+ ;; Otherwise a readable wrapper Card launders the values of a Card the user cannot read. The+ ;; result_metadata check covers tables a Card's saved columns name but its query footprint does not.+ (doseq [card-id source-card-ids]+ (query-perms/check-card-read-perms database-id card-id)+ (query-perms/check-card-result-metadata-data-perms database-id card-id))+ (when-not (query-perms/has-perm-for-query? outer-query :perms/view-data required-perms)+ (throw (query-perms/perms-exception required-perms))));; Ad-hoc query (not a saved question):elsediff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.cljindex ae5d946dab2..85c368c8b41 100644--- a/test/metabase/dashboards_rest/api_test.clj+++ b/test/metabase/dashboards_rest/api_test.clj@@ -3586,6 +3586,44 @@(is (some? (mt/user-http-request :rasta :get 200 (chain-filter-values-url dashboard-id "abc"))))(is (some? (mt/user-http-request :rasta :get 200 (chain-filter-search-url dashboard-id "abc" "red")))))))))+(deftest parameter-values-from-card-nested-source-card-test+ (testing "users must have permissions to read every card the source card's query nests, not just the source card (SEC-1158)"+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp+ [:model/Collection private-coll {:name "Private nested card collection"}+ :model/Card {nested-card-id :id} {:collection_id (:id private-coll)+ :database_id (mt/id)+ :table_id (mt/id :venues)+ :dataset_query (mt/mbql-query venues {:limit 5})}+ :model/Collection wrapper-coll {:name "Readable wrapper card collection"}+ :model/Card {wrapper-card-id :id} {:collection_id (:id wrapper-coll)+ :database_id (mt/id)+ :dataset_query {:database (mt/id)+ :type :query+ :query {:source-table (str "card__" nested-card-id)}}}+ :model/Collection dash-coll {:name "Dashboard collection"}+ :model/Dashboard {dashboard-id :id} {:collection_id (:id dash-coll)+ :parameters [{:id "abc"+ :type "category"+ :name "CATEGORY"+ :values_source_type "card"+ :values_source_config {:card_id wrapper-card-id+ :value_field (mt/$ids $venues.name)}}]}]+ (perms/grant-collection-read-permissions! (perms-group/all-users) dash-coll)+ (perms/grant-collection-read-permissions! (perms-group/all-users) wrapper-coll)+ (testing "read permission on the wrapper card is not enough when its query nests a card the user cannot read"+ (is (= (format "You do not have permissions to view Card %d." nested-card-id)+ (mt/user-http-request :rasta :get 403 (chain-filter-values-url dashboard-id "abc"))))+ (is (= (format "You do not have permissions to view Card %d." nested-card-id)+ (mt/user-http-request :rasta :get 403 (chain-filter-search-url dashboard-id "abc" "red")))))+ ;; grant permission to read the collection containing the nested card+ (perms/grant-collection-read-permissions! (perms-group/all-users) private-coll)+ (testing "success once the user can read the nested card too"+ (is (=? {:values seq}+ (mt/user-http-request :rasta :get 200 (chain-filter-values-url dashboard-id "abc"))))+ (is (=? {:values seq}+ (mt/user-http-request :rasta :get 200 (chain-filter-search-url dashboard-id "abc" "red")))))))))+(deftest parameter-values-from-card-test-4;; TODO: Re-enable this test, or delete it. Now that mapping dashboard filters to fields on cards is powered by Lib,;; the FE does not use the /api/table/:card__id/query_metadata API call to determine the fields which can be filtereddiff --git a/test/metabase/queries_rest/api/card_test.clj b/test/metabase/queries_rest/api/card_test.cljindex aaa96c4bb99..733eff985ad 100644--- a/test/metabase/queries_rest/api/card_test.clj+++ b/test/metabase/queries_rest/api/card_test.clj@@ -3772,6 +3772,130 @@(is (some? (mt/user-http-request :rasta :get 200 (param-values-url card-id "abc"))))(is (some? (mt/user-http-request :rasta :get 200 (param-values-url card-id "abc" "search-query")))))))))+(defn- nested-card-wrapper-query+ "A `dataset_query` for a wrapper Card that nests Card `nested-card-id`, either as an MBQL source Card or via a+ native `{{#id}}` template tag."+ [query-type nested-card-id]+ (case query-type+ :mbql {:database (mt/id)+ :type :query+ :query {:source-table (str "card__" nested-card-id)}}+ :native (let [card-ref (format "#%d" nested-card-id)]+ {:database (mt/id)+ :type :native+ :native {:query (format "SELECT * FROM {{%s}}" card-ref)+ :template-tags {card-ref {:id "6a1c4c4e-6c9a-4b8b-9f2a-1a5d0b6a1d2c"+ :name card-ref+ :display-name card-ref+ :type :card+ :card-id nested-card-id}}}})))++(deftest parameters-with-source-is-card-nested-source-card-test+ (doseq [wrapper-query-type [:mbql :native]]+ (testing (format "users must have permissions to read every card the source card's %s query nests, not just the source card (SEC-1158)"+ (name wrapper-query-type))+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp+ [:model/Collection private-coll {:name "Private nested card collection"}+ :model/Card {nested-card-id :id} {:collection_id (:id private-coll)+ :database_id (mt/id)+ :table_id (mt/id :venues)+ :dataset_query (mt/mbql-query venues {:limit 5})}+ :model/Collection wrapper-coll {:name "Readable wrapper card collection"}+ :model/Card {wrapper-card-id :id} {:collection_id (:id wrapper-coll)+ :database_id (mt/id)+ :dataset_query (nested-card-wrapper-query wrapper-query-type nested-card-id)}+ :model/Collection own-coll {:name "Attacker card collection"}+ :model/Card {card-id :id} {:collection_id (:id own-coll)+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues)+ :parameters [{:id "abc"+ :type "category"+ :name "CATEGORY"+ :values_source_type "card"+ :values_source_config {:card_id wrapper-card-id+ ;; native columns carry no Field IDs+ :value_field (case wrapper-query-type+ :mbql (mt/$ids $venues.name)+ :native [:field "NAME" {:base-type :type/Text}])}}]+ :table_id (mt/id :venues)}]+ ;; a saved native Card carries `result_metadata`, which is what lets the value field be found on it; run+ ;; the wrapper as an admin to populate it, as saving the Card would+ (when (= wrapper-query-type :native)+ (t2/update! :model/Card wrapper-card-id+ {:result_metadata (mt/with-test-user :crowberto+ (-> (qp/process-query (nested-card-wrapper-query :native nested-card-id))+ (get-in [:data :results_metadata :columns])))}))+ (perms/grant-collection-read-permissions! (perms-group/all-users) own-coll)+ (perms/grant-collection-read-permissions! (perms-group/all-users) wrapper-coll)+ (testing "sanity check: the user can read the wrapper card but cannot run it, since it nests an unreadable card"+ (mt/user-http-request :rasta :get 200 (format "card/%d" wrapper-card-id))+ (mt/user-http-request :rasta :post 403 (format "card/%d/query" wrapper-card-id)))+ (testing "read permission on the wrapper card is not enough when its query nests a card the user cannot read"+ (is (= (format "You do not have permissions to view Card %d." nested-card-id)+ (mt/user-http-request :rasta :get 403 (param-values-url card-id "abc"))))+ (is (= (format "You do not have permissions to view Card %d." nested-card-id)+ (mt/user-http-request :rasta :get 403 (param-values-url card-id "abc" "red")))))+ ;; grant permission to read the collection containing the nested card+ (perms/grant-collection-read-permissions! (perms-group/all-users) private-coll)+ (testing "success once the user can read the nested card too"+ (is (=? {:values seq}+ (mt/user-http-request :rasta :get 200 (param-values-url card-id "abc"))))+ (is (=? {:values seq}+ (mt/user-http-request :rasta :get 200 (param-values-url card-id "abc" "red"))))))))))++(deftest parameters-with-source-is-card-result-metadata-data-perms-test+ (testing "view-data perms are enforced on tables that appear only in the source Card's result_metadata (SEC-1158)"+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp-copy-of-db+ (mt/with-no-data-perms-for-all-users!+ (data-perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)+ (data-perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)+ (data-perms/set-table-permission! (perms-group/all-users) (mt/id :users) :perms/view-data :blocked)+ (mt/with-temp+ [:model/Collection source-coll {:name "Source card collection"}+ :model/Card {source-card-id :id} {:collection_id (:id source-coll)+ :database_id (mt/id)+ :table_id (mt/id :venues)+ :dataset_query (mt/mbql-query venues {:limit 5})+ ;; hand-edited metadata claiming a column from a table the user+ ;; cannot view. The query footprint never mentions USERS, so+ ;; result_metadata is the only place the table shows up+ :result_metadata [{:name "NAME"+ :display_name "Name"+ :base_type :type/Text+ :id (mt/id :venues :name)+ :table_id (mt/id :venues)}+ {:name "USER_NAME"+ :display_name "User Name"+ :base_type :type/Text+ :id (mt/id :users :name)+ :table_id (mt/id :users)}]}+ :model/Collection own-coll {:name "Card collection"}+ :model/Card {card-id :id} {:collection_id (:id own-coll)+ :database_id (mt/id)+ :table_id (mt/id :venues)+ :dataset_query (mt/mbql-query venues)+ :parameters [{:id "abc"+ :type "category"+ :name "CATEGORY"+ :values_source_type "card"+ :values_source_config {:card_id source-card-id+ :value_field (mt/$ids $venues.name)}}]}]+ (perms/grant-collection-read-permissions! (perms-group/all-users) own-coll)+ (perms/grant-collection-read-permissions! (perms-group/all-users) source-coll)+ (testing "read permission on the source card is not enough when its result_metadata names a blocked table"+ (is (= (format "You do not have permission to view data of table %d in result_metadata." (mt/id :users))+ (mt/user-http-request :rasta :get 403 (param-values-url card-id "abc"))))+ (is (= (format "You do not have permission to view data of table %d in result_metadata." (mt/id :users))+ (mt/user-http-request :rasta :get 403 (param-values-url card-id "abc" "red")))))+ (testing "success once the user can view the table the result_metadata references"+ (data-perms/set-table-permission! (perms-group/all-users) (mt/id :users) :perms/view-data :unrestricted)+ (is (=? {:values seq}+ (mt/user-http-request :rasta :get 200 (param-values-url card-id "abc"))))+ (is (=? {:values seq}+ (mt/user-http-request :rasta :get 200 (param-values-url card-id "abc" "red")))))))))))+(deftest parameters-using-old-style-field-values(with-card-param-values-fixtures [{:keys [param-keys field-filter-card]}](testing "GET /api/card/:card-id/params/:param-key/values for field-filter based params"