Skip to content

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

Exempt admins from sandboxing on `query_metadata` (#62228)

John Swansond0f2438d44c5
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj b/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj
index 8c15cad342d..27b7cd77c5e 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj
@@ -70,8 +70,9 @@
"Given collection of table-ids, return the sandboxes that should be enforced for the current user on any of the tables. A
sandbox is not enforced if the user is in a different permissions group that grants full access to the table."
[table-ids]
- (let [enforced-sandboxes-for-user (perms/sandboxes-for-user)]
- (filter #((set table-ids) (:table_id %)) enforced-sandboxes-for-user)))
+ (when-not *is-superuser?*
+ (let [enforced-sandboxes-for-user (perms/sandboxes-for-user)]
+ (filter #((set table-ids) (:table_id %)) enforced-sandboxes-for-user))))
(defn sandboxed-user-for-db?
"Returns true if the currently logged in user has any enforced sandboxes for the provided database. Throws an
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/api/table_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/api/table_test.clj
index 357737afe87..e4f7e84125d 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/api/table_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/api/table_test.clj
@@ -35,6 +35,20 @@
(is (= all-columns
(field-names :crowberto)))))))
+(deftest query-metadata-not-sandboxed-for-admins-test
+ (testing "GET /api/table/:id/query_metadata"
+ ;; checks that admins are exempt from normal perms checking.
+ ;; the above test did not trigger this case because the admin is not *in* "All Users".
+ (met/with-gtaps-for-all-users!
+ {:gtaps {:venues
+ {:remappings {}
+ :query (mt.tu/restricted-column-query (mt/id))}}
+ :attributes {:cat 50}}
+ (is (= #{"ID" "CATEGORY_ID" "NAME"}
+ (field-names :rasta)))
+ (is (= all-columns
+ (field-names :crowberto))))))
+
(deftest native-query-metadata-test
(testing "GET /api/table/:id/query_metadata"
(met/with-gtaps! {:gtaps {:venues
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/test_util.clj b/enterprise/backend/test/metabase_enterprise/sandbox/test_util.clj
index 92a21ac6f9e..3f4179e97b1 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/test_util.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/test_util.clj
@@ -3,6 +3,7 @@
(:require
[malli.core :as mc]
[mb.hawk.parallel]
+ [metabase.permissions.core :as perms]
[metabase.permissions.models.data-permissions :as data-perms]
[metabase.request.core :as request]
[metabase.test :as mt]
@@ -55,6 +56,26 @@
[:remappings {:optional true} :map]]]]]
[:attributes {:optional true} :map]])
+(defn do-with-gtaps-for-all-users! [args-fn f]
+ (mb.hawk.parallel/assert-test-is-not-parallel "with-gtaps-for-user!")
+ (letfn [(thunk []
+ (mt/with-no-data-perms-for-all-users!
+ (let [{:keys [gtaps]} (mc/assert WithGTAPsArgs (args-fn))]
+ (mt/with-additional-premium-features #{:sandboxes}
+ (do-with-gtap-defs! (perms/all-users-group) gtaps f)))))]
+ ;; create a temp copy of the current DB if we haven't already created one. If one is already created, keep using
+ ;; that so we can test multiple sandboxed users against the same DB
+ (if data.impl/*db-is-temp-copy?*
+ (thunk)
+ (data/with-temp-copy-of-db
+ (thunk)))))
+
+(defmacro with-gtaps-for-all-users!
+ "Sets up a sandbox for the 'all users' group."
+ {:style/indent :defn}
+ [gtaps-and-attributes-map & body]
+ `(do-with-gtaps-for-all-users! (fn [] ~gtaps-and-attributes-map) (fn [] ~@body)))
+
(defn do-with-gtaps-for-user! [args-fn test-user-name-or-user-id f]
(mb.hawk.parallel/assert-test-is-not-parallel "with-gtaps-for-user!")
(letfn [(thunk []
diff --git a/enterprise/backend/test/metabase_enterprise/test.clj b/enterprise/backend/test/metabase_enterprise/test.clj
index 053eaf8bced..def1eabe059 100644
--- a/enterprise/backend/test/metabase_enterprise/test.clj
+++ b/enterprise/backend/test/metabase_enterprise/test.clj
@@ -10,4 +10,5 @@
[sandbox.tu
with-gtaps!
with-gtaps-for-user!
+ with-gtaps-for-all-users!
with-user-attributes!])