Skip to content

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

Query parameter cleanup (#72048)

John Swanson177a6515b7c7
diff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.clj
index 2bae71eacdd..030e47a8c8f 100644
--- a/src/metabase/permissions/models/data_permissions.clj
+++ b/src/metabase/permissions/models/data_permissions.clj
@@ -64,14 +64,22 @@
[perm-type]
(-> permissions.schema/data-permissions perm-type :values first))
+(defn- validate-perm-value!
+ [perm-type perm-value]
+ (let [values (-> permissions.schema/data-permissions perm-type :values)]
+ (when-not (contains? (set values) perm-value)
+ (throw (ex-info (tru "Invalid permission value {0} for permission type {1}" (pr-str perm-value) perm-type)
+ {:value perm-value :perm-type perm-type :valid-values values})))))
+
(mu/defn at-least-as-permissive?
"Returns true if value1 is at least as permissive as value2 for the given permission type."
[perm-type :- ::permissions.schema/data-permission-type
value1 :- ::permissions.schema/data-permission-value
value2 :- ::permissions.schema/data-permission-value]
+ (validate-perm-value! perm-type value1)
+ (validate-perm-value! perm-type value2)
(let [^PersistentVector values (-> permissions.schema/data-permissions perm-type :values)]
- (<= (.indexOf values value1)
- (.indexOf values value2))))
+ (<= (.indexOf values value1) (.indexOf values value2))))
(def ^:private model-by-perm-type
"A map from permission types directly to model identifiers (or `nil`)."
@@ -79,9 +87,7 @@
(defn- assert-value-matches-perm-type
[perm-type perm-value]
- (when-not (contains? (set (get-in permissions.schema/data-permissions [perm-type :values])) perm-value)
- (throw (ex-info (tru "Permission type {0} cannot be set to {1}" perm-type perm-value)
- {perm-type (permissions.schema/data-permissions perm-type)}))))
+ (validate-perm-value! perm-type perm-value))
;;; ---------------------------------------- Caching ------------------------------------------------------------------
diff --git a/src/metabase/queries/metadata.clj b/src/metabase/queries/metadata.clj
index 251dd5ca09c..91b363c8c65 100644
--- a/src/metabase/queries/metadata.clj
+++ b/src/metabase/queries/metadata.clj
@@ -54,7 +54,7 @@
(defn- collect-recursive-snippets
([initial-snippet-ids]
(when (seq initial-snippet-ids)
- (let [snippets (t2/select :model/NativeQuerySnippet :id [:in initial-snippet-ids])]
+ (let [snippets (into [] (filter mi/can-read?) (t2/select :model/NativeQuerySnippet :id [:in initial-snippet-ids]))]
(collect-recursive-snippets (set snippets) snippets (set initial-snippet-ids)))))
([all-snippets snippets-to-recurse seen-ids]
(let [->nested-snippet-ids (fn [snippet]
@@ -67,7 +67,7 @@
snippet-id)))
nested-snippet-ids (into #{} (mapcat ->nested-snippet-ids) snippets-to-recurse)
nested-snippets (when (seq nested-snippet-ids)
- (t2/select :model/NativeQuerySnippet :id [:in nested-snippet-ids]))]
+ (into [] (filter mi/can-read?) (t2/select :model/NativeQuerySnippet :id [:in nested-snippet-ids])))]
(if-not (seq nested-snippet-ids)
all-snippets
(recur (into all-snippets nested-snippets)
diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.clj
index e1780251a7b..5e41cbbafb3 100644
--- a/src/metabase/query_permissions/impl.clj
+++ b/src/metabase/query_permissions/impl.clj
@@ -283,36 +283,21 @@
{:query query}))))))
(defn- has-perm-for-db?
- "Checks that the current user has at least `required-perm` for the entire DB specified by `db-id`."
- [perm-type required-perm gtap-perms db-id]
- (or
- (perms/at-least-as-permissive? perm-type
- (perms/full-db-permission-for-user api/*current-user-id* perm-type db-id)
- required-perm)
- (when gtap-perms
- (perms/at-least-as-permissive? perm-type gtap-perms required-perm))))
+ [perm-type required-perm db-id]
+ (perms/at-least-as-permissive? perm-type
+ (perms/full-db-permission-for-user api/*current-user-id* perm-type db-id)
+ required-perm))
(defn- has-perm-for-table?
- "Checks that the current user has the permissions for tables specified in `table-id->perm`. This can be satisfied via
- the user's permissions stored in the database, or permissions in `gtap-table-perms` which are supplied by the
- `sandboxing` QP middleware when sandboxing is in effect. Returns true if access is allowed, otherwise false."
- [perm-type table-id->required-perm gtap-table-perms db-id]
- (let [table-id->has-perm?
- (into {} (for [[table-id required-perm] table-id->required-perm]
- [table-id (boolean
- (or (perms/user-has-permission-for-table?
- api/*current-user-id*
- perm-type
- required-perm
- db-id
- table-id)
- (when-let [gtap-perm (if (keyword? gtap-table-perms)
- ;; gtap-table-perms can be a keyword representing the DB permission...
- gtap-table-perms
- ;; ...or a map from table IDs to table permissions
- (get gtap-table-perms table-id))]
- (perms/at-least-as-permissive? perm-type gtap-perm required-perm))))]))]
- (every? true? (vals table-id->has-perm?))))
+ [perm-type table-id->required-perm db-id]
+ (every? (fn [[table-id required-perm]]
+ (perms/user-has-permission-for-table?
+ api/*current-user-id*
+ perm-type
+ required-perm
+ db-id
+ table-id))
+ table-id->required-perm))
(defn- card
[database-id card-id]
@@ -354,18 +339,15 @@
(mu/defn has-perm-for-query? :- :boolean
"Returns true when the query is accessible for the given perm-type and required-perms for individual tables, or the
entire DB, false otherwise. Only throws if the permission format is incorrect."
- [{{gtap-perms :gtaps} :query-permissions/perms, db-id :database :as _query} perm-type required-perms]
+ [{db-id :database :as _query} perm-type required-perms]
(boolean
(if-let [db-or-table-perms (perm-type required-perms)]
- ;; In practice, `view-data` will be defined at the table-level, and `create-queries` will either be table-level
- ;; or :query-builder-and-native for the entire DB. But we should enforce whatever `required-perms` are provided,
- ;; in case that ever changes.
(cond
(keyword? db-or-table-perms)
- (has-perm-for-db? perm-type db-or-table-perms (perm-type gtap-perms) db-id)
+ (has-perm-for-db? perm-type db-or-table-perms db-id)
(map? db-or-table-perms)
- (has-perm-for-table? perm-type db-or-table-perms (perm-type gtap-perms) db-id)
+ (has-perm-for-table? perm-type db-or-table-perms db-id)
:else
(throw (ex-info (tru "Invalid permissions format") required-perms)))
@@ -384,19 +366,13 @@
{:card-id card-id}))))))
(defn check-data-perms
- "Checks whether the current user has sufficient view data and query permissions to run `query`. Returns `true` if the
- user has perms for the query, and throws an exception otherwise (exceptions can be disabled by setting
- `throw-exceptions?` to `false`).
-
- If the [:gtap :query-permissions/perms] path is present in the query, these perms are implicitly granted to the current user."
- [{{gtap-perms :gtaps} :query-permissions/perms, :as query} required-perms & {:keys [throw-exceptions?]
- :or {throw-exceptions? true}}]
+ "Checks whether the current user has sufficient view data and query permissions to run `query`."
+ [query required-perms & {:keys [throw-exceptions?]
+ :or {throw-exceptions? true}}]
(try
- ;; Check any required v1 paths
(when-let [paths (:paths required-perms)]
- (let [paths-excluding-gtap-paths (set/difference paths (:paths gtap-perms))]
- (or (perms/set-has-full-permissions-for-set? @api/*current-user-permissions-set* paths-excluding-gtap-paths)
- (throw (perms-exception paths)))))
+ (or (perms/set-has-full-permissions-for-set? @api/*current-user-permissions-set* paths)
+ (throw (perms-exception paths))))
;; Check view-data and create-queries permissions, for individual tables or the entire DB:
(when (or (not (has-perm-for-query? query :perms/view-data required-perms))
@@ -437,17 +413,18 @@
queries they wouldn't be allowed to run!"
[query :- :map]
{:pre [(map? query)]}
- (when-not (can-run-query? query)
- (let [required-perms (try
- (required-perms-for-query query :throw-exceptions? true)
- (catch Throwable e
- e))]
- (throw (ex-info (tru "You cannot save this Question because you do not have permissions to run its query.")
- {:status-code 403
- :query query
- :required-perms (if (instance? Throwable required-perms)
- :error
- required-perms)
- :actual-perms @api/*current-user-permissions-set*}
- (when (instance? Throwable required-perms)
- required-perms))))))
+ (let [query (dissoc query :query-permissions/perms)]
+ (when-not (can-run-query? query)
+ (let [required-perms (try
+ (required-perms-for-query query :throw-exceptions? true)
+ (catch Throwable e
+ e))]
+ (throw (ex-info (tru "You cannot save this Question because you do not have permissions to run its query.")
+ {:status-code 403
+ :query query
+ :required-perms (if (instance? Throwable required-perms)
+ :error
+ required-perms)
+ :actual-perms @api/*current-user-permissions-set*}
+ (when (instance? Throwable required-perms)
+ required-perms)))))))
diff --git a/src/metabase/query_processor/api.clj b/src/metabase/query_processor/api.clj
index 484efb0ee7f..52106e9faa5 100644
--- a/src/metabase/query_processor/api.clj
+++ b/src/metabase/query_processor/api.clj
@@ -84,7 +84,7 @@
(:constraints query))]
(qp.pivot/run-pivot-query (-> query
(assoc :constraints constraints)
- (update :info merge info))
+ (assoc :info info))
rff))
(qp/process-query (update query :info merge info) rff))))))
diff --git a/src/metabase/query_processor/middleware/permissions.clj b/src/metabase/query_processor/middleware/permissions.clj
index feff62df251..67703bade8c 100644
--- a/src/metabase/query_processor/middleware/permissions.clj
+++ b/src/metabase/query_processor/middleware/permissions.clj
@@ -1,7 +1,6 @@
(ns metabase.query-processor.middleware.permissions
"Middleware for checking that the current user has permissions to run the current query."
(:require
- [clojure.set :as set]
[metabase.api.common :refer [*current-user-id* *current-user-permissions-set*]]
[metabase.audit-app.core :as audit]
[metabase.lib.core :as lib]
@@ -92,12 +91,20 @@
(fn [_query _path-type _path stage-or-join]
(dissoc stage-or-join :query-permissions/sandboxed-table))))
+(defn remove-persisted-info-native-keys
+ "Strips `:persisted-info/native` from query stages."
+ [query]
+ (lib.walk/walk
+ query
+ (fn [_query _path-type _path stage-or-join]
+ (dissoc stage-or-join :persisted-info/native))))
+
(mu/defn check-query-permissions*
"Check that User with `user-id` has permissions to run `query`, or throw an exception."
[query :- ::qp.schema/any-query]
(if (:lib/type query)
(recur (lib/->legacy-MBQL query))
- (let [{database-id :database, {gtap-perms :gtaps} :query-permissions/perms :as outer-query} query]
+ (let [{database-id :database :as outer-query} query]
(when *current-user-id*
(log/tracef "Checking query permissions. Current user permissions = %s"
(pr-str (perms/permissions-for-user *current-user-id*)))
@@ -105,7 +112,7 @@
(check-audit-db-permissions outer-query))
(check-query-does-not-access-inactive-tables outer-query)
(let [required-perms (query-perms/required-perms-for-query outer-query :already-preprocessed? true)
- source-card-ids (set/difference (:card-ids required-perms) (:card-ids gtap-perms))]
+ source-card-ids (:card-ids required-perms)]
;; On EE, check block permissions up front for all queries. If block perms are in place, reject all native queries
;; (unless overridden by `gtap-perms`) and any queries that touch blocked tables/DBs
(check-block-permissions outer-query)
diff --git a/src/metabase/query_processor/pivot.clj b/src/metabase/query_processor/pivot.clj
index dfca83a9328..848bbafcb98 100644
--- a/src/metabase/query_processor/pivot.clj
+++ b/src/metabase/query_processor/pivot.clj
@@ -24,7 +24,6 @@
[metabase.query-processor.metadata :as qp.metadata]
[metabase.query-processor.middleware.add-remaps :as qp.add-remaps]
[metabase.query-processor.middleware.normalize-query :as qp.middleware.normalize]
- [metabase.query-processor.middleware.permissions :as qp.perms]
[metabase.query-processor.pipeline :as qp.pipeline]
[metabase.query-processor.pivot.common :as pivot.common]
[metabase.query-processor.reducible :as qp.reducible]
@@ -505,18 +504,17 @@
([query :- ::qp.schema/any-query
rff :- [:maybe ::qp.schema/rff]]
(log/debugf "Running pivot query:\n%s" (u/pprint-to-str query))
- (binding [qp.perms/*card-id* (get-in query [:info :card-id])]
- (qp.setup/with-qp-setup [query query]
- (let [query (qp.middleware.normalize/normalize-preprocessing-middleware query) ; normalize to MBQL 5 if needed.
- rff (or rff qp.reducible/default-rff)
- pivot-opts (or
- (pivot-options query (get query :viz-settings))
- (pivot-options query (get-in query [:info :visualization-settings]))
- (not-empty (select-keys query [:pivot-rows :pivot-cols :pivot-measures :show-row-totals :show-column-totals])))
- pivot-limit (pivot-query-max-rows query)
- query (-> query
- (assoc-in [:middleware :pivot-options] pivot-opts)
- (assoc-in [:constraints :max-results] pivot-limit)
- add-canonical-col-info)
- all-queries (generate-queries query pivot-opts)]
- (process-multiple-queries all-queries rff pivot-limit))))))
+ (qp.setup/with-qp-setup [query query]
+ (let [query (qp.middleware.normalize/normalize-preprocessing-middleware query) ; normalize to MBQL 5 if needed.
+ rff (or rff qp.reducible/default-rff)
+ pivot-opts (or
+ (pivot-options query (get query :viz-settings))
+ (pivot-options query (get-in query [:info :visualization-settings]))
+ (not-empty (select-keys query [:pivot-rows :pivot-cols :pivot-measures :show-row-totals :show-column-totals])))
+ pivot-limit (pivot-query-max-rows query)
+ query (-> query
+ (assoc-in [:middleware :pivot-options] pivot-opts)
+ (assoc-in [:constraints :max-results] pivot-limit)
+ add-canonical-col-info)
+ all-queries (generate-queries query pivot-opts)]
+ (process-multiple-queries all-queries rff pivot-limit)))))
diff --git a/src/metabase/query_processor/preprocess.clj b/src/metabase/query_processor/preprocess.clj
index fd277a13b49..61313a6483d 100644
--- a/src/metabase/query_processor/preprocess.clj
+++ b/src/metabase/query_processor/preprocess.clj
@@ -61,6 +61,7 @@
#'qp.perms/remove-permissions-key
#'qp.perms/remove-source-card-keys
#'qp.perms/remove-sandboxed-table-keys
+ #'qp.perms/remove-persisted-info-native-keys
#'qp.constraints/maybe-add-default-userland-constraints
#'validate/validate-query
#'fetch-source-query/resolve-source-cards
diff --git a/test/metabase/permissions/models/data_permissions_test.clj b/test/metabase/permissions/models/data_permissions_test.clj
index a8cc2f90321..de796a2f235 100644
--- a/test/metabase/permissions/models/data_permissions_test.clj
+++ b/test/metabase/permissions/models/data_permissions_test.clj
@@ -56,7 +56,7 @@
(testing "A database-level permission cannot be set to an invalid value"
(is (thrown-with-msg?
ExceptionInfo
- #"Permission type :perms/create-queries cannot be set to :invalid-value"
+ #"Invalid permission value :invalid-value for permission type :perms/create-queries"
(data-perms/set-database-permission! group-id database-id :perms/create-queries :invalid-value))))))))
(deftest set-table-permissions!-test
@@ -111,7 +111,7 @@
(testing "A table-level permission cannot be set to an invalid value"
(is (thrown-with-msg?
ExceptionInfo
- #"Permission type :perms/create-queries cannot be set to :invalid"
+ #"Invalid permission value :invalid for permission type :perms/create-queries"
(data-perms/set-table-permissions! group-id :perms/create-queries {table-id-1 :invalid}))))
(testing "A table-level permission can be set to :block"
diff --git a/test/metabase/query_processor/middleware/permissions_test.clj b/test/metabase/query_processor/middleware/permissions_test.clj
index f55f64c1dac..6b2685772d3 100644
--- a/test/metabase/query_processor/middleware/permissions_test.clj
+++ b/test/metabase/query_processor/middleware/permissions_test.clj
@@ -753,18 +753,12 @@
clojure.lang.ExceptionInfo
#"You do not have permissions to run this query"
(qp/process-query (mt/mbql-query venues {:limit 1})))))
- (letfn [(process-query []
- (qp/process-query (assoc (mt/mbql-query venues {:limit 1})
- :query-permissions/perms {:gtaps {:perms/view-data :unrestricted
- :perms/create-queries {(mt/id :venues) :query-builder}}})))]
- (testing "Make sure the middleware is actually preventing something by disabling it"
- (with-redefs [qp.perms/remove-permissions-key identity]
- (is (=? {:status :completed}
- (process-query)))))
- (is (thrown-with-msg?
- clojure.lang.ExceptionInfo
- #"You do not have permissions to run this query"
- (process-query))))))))
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"You do not have permissions to run this query"
+ (qp/process-query (assoc (mt/mbql-query venues {:limit 1})
+ :query-permissions/perms {:gtaps {:perms/view-data :unrestricted
+ :perms/create-queries {(mt/id :venues) :query-builder}}}))))))))
(deftest e2e-ignore-user-supplied-sandboxed-tables-test
(testing "You shouldn't be able to bypass security restrictions by passing in `:query-permissions/sandboxed-table` in the query"