Skip to content

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

Better sandbox config validation (#84315)

Nathan Voxland6821a96bd07c
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/models/permissions/delete_sandboxes.clj b/enterprise/backend/src/metabase_enterprise/sandbox/models/permissions/delete_sandboxes.clj
index 1bd34e9c233..ab944a0a42f 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/models/permissions/delete_sandboxes.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/models/permissions/delete_sandboxes.clj
@@ -27,6 +27,35 @@
(and (some? table-change)
(not= table-change :sandboxed))))))))
+(defn- sandboxed-view-data?
+ "Does an API-graph `:view-data` value (a keyword, or a map of schema to keyword or map of table ID to keyword) sandbox
+ anything?"
+ [view-data]
+ (if (map? view-data)
+ (some sandboxed-view-data? (vals view-data))
+ (= view-data :sandboxed)))
+
+(defenterprise check-native-access-without-sandboxes!
+ "Throw a 400 if a group in `native-group-db-ids`, a set of `[group-id db-id]` pairs being granted native query
+ access, would also have a sandbox on that database once `changes` are applied: either one it newly sandboxes, or
+ an existing one that [[delete-gtaps-if-needed-after-permissions-change!]] won't delete. Native queries bypass
+ sandboxing."
+ :feature :sandboxes
+ [changes native-group-db-ids]
+ (when (seq native-group-db-ids)
+ (let [kept-sandboxes (into #{}
+ (comp (remove (partial should-delete-sandbox? changes))
+ (map (juxt :group_id :db_id)))
+ (sandbox.db/candidate-sandboxes-for-groups-and-databases
+ (into #{} (map first) native-group-db-ids)
+ (into #{} (map second) native-group-db-ids)))]
+ (when (some (fn [[group-id db-id :as group-db-id]]
+ (or (contains? kept-sandboxes group-db-id)
+ (sandboxed-view-data? (get-in changes [group-id db-id :view-data]))))
+ native-group-db-ids)
+ (throw (ex-info (tru "A group with row and column security on a database cannot also have native query access to it.")
+ {:status-code 400}))))))
+
(defenterprise delete-gtaps-if-needed-after-permissions-change!
"For use only inside `metabase.permissions.models.permissions`; don't call this elsewhere. Delete GTAPs (sandboxes) that are no
longer needed after the permissions graph is updated. `changes` are the parts of the graph that have changed, i.e.
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/api/dataset_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/api/dataset_test.clj
index 599619d0698..ceab25acee6 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/api/dataset_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/api/dataset_test.clj
@@ -5,6 +5,7 @@
[clojure.string :as str]
[clojure.test :refer :all]
[metabase-enterprise.test :as met]
+ [metabase.permissions.core :as perms]
[metabase.test :as mt]))
(deftest dataset-parameter-test
@@ -104,3 +105,30 @@
(is (nil? (get-in response [:data :native_form])))
(doseq [secret secrets]
(is (not (str/includes? (pr-str response) secret))))))))))
+
+(deftest sandboxed-user-native-query-test
+ (testing "A user with an enforced sandbox on a database can't use native queries there, even when their group grants
+ native query access, because native queries bypass the sandbox"
+ (met/with-gtaps! {:gtaps {:venues {:query (mt/mbql-query venues {:filter [:= $category_id 50]})}}}
+ (perms/set-database-permission! &group (mt/id) :perms/create-queries :query-builder-and-native)
+ (testing "POST /api/dataset refuses ad-hoc native SQL"
+ (is (=? {:status "failed"
+ :error "You do not have permissions to run this query."}
+ (mt/user-http-request :rasta :post "dataset"
+ (mt/native-query {:query "SELECT COUNT(*) FROM VENUES"})))))
+ (testing "POST /api/dataset/native refuses to compile a query to SQL"
+ (is (=? {:permissions-error? true
+ :message "You do not have permissions to run this query."}
+ (mt/user-http-request :rasta :post "dataset/native" (mt/mbql-query venues)))))
+ (testing "MBQL queries are still sandboxed"
+ (is (= [[10]]
+ (mt/rows (mt/user-http-request :rasta :post 202 "dataset"
+ (mt/mbql-query venues {:aggregation [[:count]]}))))))
+ (testing "native queries are allowed when another group grants unrestricted access to the sandboxed table"
+ (mt/with-temp [:model/PermissionsGroup {other-group-id :id} {}
+ :model/PermissionsGroupMembership _ {:group_id other-group-id
+ :user_id (mt/user->id :rasta)}]
+ (perms/set-database-permission! other-group-id (mt/id) :perms/view-data :unrestricted)
+ (is (= [[100]]
+ (mt/rows (mt/user-http-request :rasta :post 202 "dataset"
+ (mt/native-query {:query "SELECT COUNT(*) FROM VENUES"}))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/api/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/api/permissions_test.clj
index 17011a53ccd..526de1eefcb 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/api/permissions_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/api/permissions_test.clj
@@ -6,6 +6,7 @@
[metabase-enterprise.test :as met]
[metabase.model-persistence.models.persisted-info :as persisted-info]
[metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
+ [metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.query-processor.compile :as qp.compile]
[metabase.test :as mt]
@@ -183,3 +184,63 @@
(mt/id :people) :sandboxed}
(get-in (data-perms.graph/api-graph)
[:groups gid (mt/id) :view-data "PUBLIC"])))))))))
+
+(defn- venues-sandboxed-view-data
+ "API-graph `:view-data` for the test DB with VENUES sandboxed and every other PUBLIC Table unrestricted."
+ []
+ {"PUBLIC" (into {}
+ (for [table-id (t2/select-pks-set :model/Table :db_id (mt/id) :schema "PUBLIC" :active true)]
+ [table-id (if (= table-id (mt/id :venues)) :sandboxed :unrestricted)]))})
+
+(def ^:private native-with-sandbox-error
+ "A group with row and column security on a database cannot also have native query access to it.")
+
+(deftest graph-rejects-native-access-with-sandboxes-test
+ (testing "PUT /api/permissions/graph rejects granting native query access to a group with a sandbox on the database"
+ (mt/with-premium-features #{:advanced-permissions :sandboxes}
+ (mt/with-model-cleanup [:model/Sandbox]
+ (mt/with-temp [:model/PermissionsGroup {gid :id} {}
+ :model/Card {cid :id} {:dataset_query (mt/mbql-query venues {:filter [:= $category_id 50]})}]
+ (let [create-queries (fn []
+ (get-in (data-perms.graph/api-graph) [:groups gid (mt/id) :create-queries]))
+ sandbox-ids (fn []
+ (t2/select-pks-set :model/Sandbox :group_id gid))
+ sandbox-venues (fn [graph]
+ (-> graph
+ (assoc-in [:groups gid (mt/id) :view-data] (venues-sandboxed-view-data))
+ (assoc :sandboxes [{:table_id (mt/id :venues) :group_id gid :card_id cid}])))]
+ (perms/set-database-permission! gid (mt/id) :perms/create-queries :query-builder)
+ (testing "when the sandbox and native access are granted in the same request"
+ (is (= native-with-sandbox-error
+ (mt/user-http-request :crowberto :put 400 "permissions/graph"
+ (-> (data-perms.graph/api-graph)
+ sandbox-venues
+ (assoc-in [:groups gid (mt/id) :create-queries] :query-builder-and-native)))))
+ (is (empty? (sandbox-ids)))
+ (is (= :query-builder (create-queries))))
+ (testing "sandboxing a Table for a group with native access still revokes native access"
+ (perms/set-database-permission! gid (mt/id) :perms/create-queries :query-builder-and-native)
+ (mt/user-http-request :crowberto :put 200 "permissions/graph" (sandbox-venues (data-perms.graph/api-graph)))
+ (is (= 1 (count (sandbox-ids))))
+ (is (not= :query-builder-and-native (create-queries))))
+ (testing "when native access is granted to a group that already has a sandbox"
+ (is (= native-with-sandbox-error
+ (mt/user-http-request :crowberto :put 400 "permissions/graph"
+ (assoc-in (data-perms.graph/api-graph)
+ [:groups gid (mt/id) :create-queries]
+ :query-builder-and-native))))
+ (is (not= :query-builder-and-native (create-queries)))
+ (is (= 1 (count (sandbox-ids)))))
+ (testing "unrelated changes to a sandboxed group are still allowed"
+ (mt/user-http-request :crowberto :put 200 "permissions/graph"
+ (assoc-in (data-perms.graph/api-graph)
+ [:groups gid (mt/id) :download]
+ {:schemas :limited}))
+ (is (= 1 (count (sandbox-ids)))))
+ (testing "granting native access along with unrestricted access removes the sandbox, as the UI does"
+ (mt/user-http-request :crowberto :put 200 "permissions/graph"
+ (update-in (data-perms.graph/api-graph) [:groups gid (mt/id)] assoc
+ :view-data :unrestricted
+ :create-queries :query-builder-and-native))
+ (is (= :query-builder-and-native (create-queries)))
+ (is (empty? (sandbox-ids))))))))))
diff --git a/src/metabase/permissions_rest/data_permissions/graph.clj b/src/metabase/permissions_rest/data_permissions/graph.clj
index 01e96464ada..515f732b146 100644
--- a/src/metabase/permissions_rest/data_permissions/graph.clj
+++ b/src/metabase/permissions_rest/data_permissions/graph.clj
@@ -592,6 +592,25 @@
(throw (ex-info (tru "Transforms permission requires \"Query builder and native\" data access for the database.")
{:status-code 400}))))))
+(defenterprise check-native-access-without-sandboxes!
+ "Throw a 400 if a group in `native-group-db-ids`, a set of `[group-id db-id]` pairs being granted native query
+ access, would also have a sandbox on that database once `graph` is applied. OSS impl is a no-op, since sandboxes are
+ an EE-only feature."
+ metabase-enterprise.sandbox.models.permissions.delete-sandboxes
+ [_graph _native-group-db-ids])
+
+(defn- check-native-access-compatible-with-sandboxes!
+ "Native queries bypass sandboxing, so a group can't be granted native access to a database it has a sandbox on.
+ Sandboxing a Table for a group that already has native access is fine: creating the sandbox revokes it."
+ [graph]
+ (check-native-access-without-sandboxes!
+ graph
+ (into #{}
+ (for [[group-id db-updates] graph
+ [db-id db-changes] db-updates
+ :when (= :query-builder-and-native (:create-queries db-changes))]
+ [group-id db-id]))))
+
(defn check-audit-db-permissions
"Check that the changes coming in does not attempt to change audit database permission. Admins should
change these permissions implicitly via collection permissions."
@@ -621,6 +640,7 @@
{:keys [to-delete to-insert]} (compute-diff desired-state current-perms tables-by-db)]
(validate-blocked-permissions! graph)
(check-transforms-permissions! graph current-perms)
+ (check-native-access-compatible-with-sandboxes! graph)
(when (seq to-delete)
(perms/batch-delete-permissions! to-delete))
(when (seq to-insert)
diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.clj
index 800f550261d..0e62dc59164 100644
--- a/src/metabase/query_permissions/impl.clj
+++ b/src/metabase/query_permissions/impl.clj
@@ -445,6 +445,11 @@
(when (or (not (has-perm-for-query? query :perms/view-data required-perms))
(not (has-perm-for-query? query :perms/create-queries required-perms)))
(throw (perms-exception required-perms)))
+ ;; Native queries bypass sandboxing, so a user with an enforced sandbox on the database can't run them there, even
+ ;; if one of their groups grants native access.
+ (when (and (= :query-builder-and-native (:perms/create-queries required-perms))
+ (perms/sandboxed-user-for-db? (:database query)))
+ (throw (perms-exception required-perms)))
true
(catch clojure.lang.ExceptionInfo e
(if throw-exceptions?
diff --git a/src/metabase/query_processor/middleware/permissions.clj b/src/metabase/query_processor/middleware/permissions.clj
index 6324135b969..b67bb0e261a 100644
--- a/src/metabase/query_processor/middleware/permissions.clj
+++ b/src/metabase/query_processor/middleware/permissions.clj
@@ -180,8 +180,10 @@
[{database-id :database, :as _query}]
(or
(not *current-user-id*)
- (= (perms/full-database-permission-for-user *current-user-id* :perms/create-queries database-id)
- :query-builder-and-native)))
+ (and (= (perms/full-database-permission-for-user *current-user-id* :perms/create-queries database-id)
+ :query-builder-and-native)
+ ;; the compiled SQL of a sandboxed user's query exposes the sandbox definition
+ (not (perms/sandboxed-user-for-db? database-id)))))
(defn check-current-user-has-adhoc-native-query-perms
"Check that the current user (if bound) has adhoc native query permissions to run `query`, or throw an