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
Fix user attribute filters against implicitly joined tables that are also sandboxed (#66790)
Cam Sauld25eba372ce1
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.cljindex d13aad16ea5..816503dcdb1 100644--- a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj+++ b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj@@ -285,28 +285,43 @@original-table-cols))(mu/defn- apply-sandbox-to-stage :- [:and- [:sequential {:min 2} ::lib.schema/stage]+ [:sequential {:min 1} ::lib.schema/stage]::lib.schema.util/unique-uuids]"Apply a Sandbox to a `stage`, returning a vector of replacement stages."[query :- ::lib.schema/query+ path :- ::lib.walk/path{:keys [source-table] :as stage} :- ::lib.schema/stagesandbox :- ::sandbox]- (let [sandbox-query (sandbox->query query sandbox)- sandbox-query (project-only-columns-from-original-table query sandbox-query source-table)- new-source-stages (mapv (fn [stage]- (-> stage- (assoc :query-permissions/sandboxed-table source-table)- lib/fresh-uuids))- (:stages sandbox-query))+ (let [sandbox-query (sandbox->query query sandbox)+ sandbox-query (project-only-columns-from-original-table query sandbox-query source-table)+ new-source-stages (mapv (fn [stage]+ (-> stage+ (assoc :query-permissions/sandboxed-table source-table)+ lib/fresh-uuids))+ (:stages sandbox-query));; merge stage metadata in the last source stage if needed- new-source-stages (m/update-existing-in new-source-stages- [(dec (count new-source-stages)) :lib/stage-metadata :columns]- (fn [cols]- (merge-original-table-metadata- cols- (lib/returned-columns query (lib.metadata/table query source-table)))))- replacement-stages (conj new-source-stages- (dissoc stage :source-table))]+ new-source-stages (m/update-existing-in new-source-stages+ [(dec (count new-source-stages)) :lib/stage-metadata :columns]+ (fn [cols]+ (merge-original-table-metadata+ cols+ (lib/returned-columns query (lib.metadata/table query source-table)))))+ is-stage-in-join? (->> path ; [:stages 0 :joins 0 :stages 0]+ (drop-last 3) ; [:stages 0 :joins]+ last+ (= :joins))+ ;; don't add a new additional stage IF WE'RE RECURSING INTO A JOIN and it only contains `:fields` and the last+ ;; of the `new-source-stages` already contains `:fields` anyway... the `:fields` for the join will just+ ;; basically be `SELECT *`, so at best this is completely unnecessary and at worst it can cause query failures+ ;; because the stuff in `:fields` can be wrong if some of those fields have been filtered out by the+ ;; sandbox (see #66781)+ skip-final-stage? (and is-stage-in-join?+ (:fields (last new-source-stages))+ (empty? (->> (keys stage)+ (remove #{:source-table :fields})+ (remove qualified-keyword?))))+ replacement-stages (cond-> new-source-stages+ (not skip-final-stage?) (conj (dissoc stage :source-table)))](log/tracef "Applied Sandbox: replaced stage\n\n%s\n\nwith stages\n\n%s"(u/cprint-to-str stage)(u/cprint-to-str replacement-stages))@@ -321,7 +336,7 @@;; `::sandbox?` key(lib.walk/walk-stagesquery- (fn [query _path stage]+ (fn [query path stage](when (and (= (:lib/type stage) :mbql.stage/mbql)(:source-table stage)(not (::sandbox? stage)))@@ -331,7 +346,7 @@(mapv (fn [stage](cond-> stage(:source-table stage) (assoc ::sandbox? true)))- (apply-sandbox-to-stage query stage sandbox)))))))+ (apply-sandbox-to-stage query path stage sandbox)))))))(mu/defn- expected-cols :- [:sequential ::mbql.s/legacy-column-metadata][query :- ::lib.schema/query]diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.cljindex ed019d2b996..902d380385c 100644--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj@@ -13,6 +13,7 @@[metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.test-util :as lib.tu]+ [metabase.lib.test-util.notebook-helpers :as lib.tu.notebook][metabase.lib.util.match :as lib.util.match][metabase.permissions.models.data-permissions :as data-perms][metabase.permissions.models.permissions :as perms]@@ -1781,3 +1782,34 @@result))(is (= ["First row, first column" "First row, second column"](->> result :data :rows first (drop 1)))))))))++(deftest test-66781+ (testing "Handle user attribute filters against implicitly joined tables that are also sandboxed correctly (#66781)"+ (met/with-gtaps! (let [mp (mt/metadata-provider)]+ {:gtaps {:orders {:query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (as-> $query (lib/remove-field $query+ -1+ (lib.tu.notebook/find-col-with-spec+ $query+ (lib/returned-columns $query)+ {:display-name "Orders"}+ {:display-name "Total"}))))+ :remappings {:user_id [:variable [:field+ (mt/id :people :id)+ {:source-field (mt/id :orders :user_id)}]]}}+ :people {:query (-> (lib/query mp (lib.metadata/table mp (mt/id :people)))+ (as-> $query (lib/remove-field $query+ -1+ (lib.tu.notebook/find-col-with-spec+ $query+ (lib/returned-columns $query)+ {:display-name "People"}+ {:display-name "Email"})))+ (lib/order-by (lib.metadata/field mp (mt/id :people :id))))+ :remappings {:user_id [:variable [:field (mt/id :people :id)]]}}}+ :attributes {:user_id 1}})+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :orders :quantity)))))]+ (is (= [[44]]+ (mt/rows (mt/user-http-request :rasta :post 202 "dataset" query))))))))