Skip to content

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

Remove `:feature` checks on some EE code (#64848)

John Swanson8dc5cec9184f
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj
index ab98980c27e..1361c63e6d8 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj
@@ -19,7 +19,11 @@
because of Collection perms; throw an Exception if they are; otherwise return `true`. The query is still allowed to
run if the current User has unrestricted data permissions from another Group. See the namespace documentation for
[[metabase.collections.models.collection]] for more details."
- :feature :advanced-permissions
+ ;; if a token check fails we don't want to fail open here
+ ;;
+ ;; run this even when the feature is not enabled - throwing here is better than silently ignoring the configured
+ ;; block.
+ :feature :none
[{database-id :database :as query}]
(let [{:keys [table-ids]} (query-perms/query->source-ids query)
table-permissions (map (partial perms/table-permission-for-user api/*current-user-id*
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
index caddd68d99f..8aead4b98fc 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
@@ -31,7 +31,7 @@
(mu/defmethod current-user-download-perms-level :mbql.stage/mbql
[{db-id :database, :as query} :- ::lib.schema/query]
(let [{:keys [table-ids native?]} (query-perms/query->source-ids query)
- perms (if (or native? (lib/any-native-stage? query))
+ perms (if (or native? (lib/any-native-stage-not-introduced-by-sandbox? query))
;; If we detect any native subqueries/joins, even with source-card IDs, require full native
;; download perms
#{(perms/native-download-permission-for-user api/*current-user-id* db-id)}
diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/middleware.clj b/enterprise/backend/src/metabase_enterprise/database_routing/middleware.clj
index 14f66fd2a56..e9c6e19dd08 100644
--- a/enterprise/backend/src/metabase_enterprise/database_routing/middleware.clj
+++ b/enterprise/backend/src/metabase_enterprise/database_routing/middleware.clj
@@ -7,10 +7,11 @@
[metabase.database-routing.core :refer [with-database-routing-on]]
[metabase.driver.util :as driver.u]
[metabase.lib.metadata :as lib.metadata]
- [metabase.premium-features.core :refer [defenterprise]]
- ;; legacy usage -- don't do things like this going forward
+ [metabase.premium-features.core :as premium-features :refer [defenterprise]]
+ ;; legacy usage -- don't do things like this going forward
^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.query-processor.store :as qp.store]
- [metabase.util :as u]))
+ [metabase.util :as u]
+ [metabase.util.i18n :refer [tru]]))
(defenterprise swap-destination-db
"Must be the last middleware before we actually hit the database. If a Router Database is specified, swaps out the
@@ -33,14 +34,17 @@
(defenterprise attach-destination-db-middleware
"Pre-processing middleware. Calculates the destination database that should be used for this query, e.g. for caching
purposes. Does not make any changes to the query besides (possibly) adding a `:destination-database/id` key."
- :feature :database-routing
+ ;; run this even when the `:database-routing` feature is not enabled, so that we can assert that it *is* enabled if
+ ;; DB routing is configured. (Throwing here is better than silently ignoring the configured routing.)
+ :feature :none
[query]
(let [database (lib.metadata/database (qp.store/metadata-provider))
destination-db-id (router-db-or-id->destination-db-id database)]
- (when (and destination-db-id
- (not (driver.u/supports? (:engine (lib.metadata/database (qp.store/metadata-provider)))
- :database-routing
- database)))
- (throw (ex-info "Unsupported database for database routing" {})))
+ (when destination-db-id
+ (premium-features/assert-has-feature :database-routing (tru "Database Routing"))
+ (when-not (driver.u/supports? (:engine (lib.metadata/database (qp.store/metadata-provider)))
+ :database-routing
+ database)
+ (throw (ex-info "Unsupported database for database routing" {}))))
(cond-> query
destination-db-id (assoc :destination-database/id destination-db-id))))
diff --git a/enterprise/backend/src/metabase_enterprise/impersonation/middleware.clj b/enterprise/backend/src/metabase_enterprise/impersonation/middleware.clj
index 88e96b8236f..c0a0df8e0c2 100644
--- a/enterprise/backend/src/metabase_enterprise/impersonation/middleware.clj
+++ b/enterprise/backend/src/metabase_enterprise/impersonation/middleware.clj
@@ -2,25 +2,34 @@
(:require
[metabase-enterprise.impersonation.driver :as impersonation.driver]
[metabase.lib.metadata :as lib.metadata]
- [metabase.premium-features.core :refer [defenterprise]]
+ [metabase.premium-features.core :as premium-features :refer [defenterprise]]
;; legacy usage -- don't do things like this going forward
- ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.query-processor.store :as qp.store]))
+ ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.query-processor.store :as qp.store]
+ [metabase.util.i18n :refer [tru]]))
(defenterprise apply-impersonation
"Pre-processing middleware. Adds a key to the query. Currently used solely for caching."
- :feature :advanced-permissions
+ ;; run this even when the `:advanced-permissions` feature is not enabled, so that we can assert that it *is* enabled
+ ;; if impersonation is configured. (Throwing here is better than silently ignoring the configured impersonation.)
+ :feature :none
[query]
(if-let [role (impersonation.driver/connection-impersonation-role
(lib.metadata/database (qp.store/metadata-provider)))]
- (assoc query :impersonation/role role)
+ (do
+ (premium-features/assert-has-feature :advanced-permissions (tru "Advanced Permissions"))
+ (assoc query :impersonation/role role))
query))
(defenterprise apply-impersonation-postprocessing
"Post-processing middleware. Binds the dynamic var"
- :feature :advanced-permissions
+ ;; run this even when the `:advanced-permissions` feature is not enabled, so that we can assert that it *is* enabled
+ ;; if impersonation is configured. (Throwing here is better than silently ignoring the configured impersonation.)
+ :feature :none
[qp]
(fn [query rff]
(if-let [role (:impersonation/role query)]
- (binding [impersonation.driver/*impersonation-role* role]
- (qp query rff))
+ (do
+ (premium-features/assert-has-feature :advanced-permissions (tru "Advanced Permissions"))
+ (binding [impersonation.driver/*impersonation-role* role]
+ (qp query rff)))
(qp query rff))))
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj b/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj
index c99fd1a63f3..b0762a79072 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/api/util.clj
@@ -39,7 +39,10 @@
WARNING: This should NOT be used directly for sandboxing enforcement. Use `*sandboxes-for-user*` or
`enforced-sandboxes-for-tables` below, so that the cache is used."
- :feature :sandboxes
+ ;; This needs to be `:none` because we need to be able to decide whether sandboxing is *configured* for a user, even
+ ;; if the feature isn't actually available to be enforced. (This way, we can block all requests that *would
+ ;; otherwise be* sandboxed when sandboxing is turned off.)
+ :feature :none
[user-id]
(when user-id
(let [user-group-ids (user/group-ids user-id)
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.clj
index 543e83853fc..d13aad16ea5 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
@@ -23,7 +23,7 @@
[metabase.lib.util :as lib.util]
[metabase.lib.util.match :as lib.util.match]
[metabase.lib.walk :as lib.walk]
- [metabase.premium-features.core :refer [defenterprise]]
+ [metabase.premium-features.core :as premium-features :refer [defenterprise]]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.pipeline :as qp.pipeline]
[metabase.query-processor.util.persisted-cache :as qp.persisted]
@@ -351,13 +351,16 @@
(or (when (not api/*is-superuser?*)
(when-let [table-id->sandbox (when *current-user-id*
(query->table-id->sandbox query))]
+ (premium-features/assert-has-feature :sandboxes (tru "Sandboxing"))
(sandboxed-query query table-id->sandbox)))
query))
(defenterprise apply-sandboxing
"Pre-processing middleware. Replaces source tables a User was querying against with source queries that (presumably)
restrict the rows returned, based on presence of sandboxes."
- :feature :sandboxes
+ ;; run this even when the `:sandboxes` feature is not enabled, so that we can assert that it *is* enabled if
+ ;; sandboxing is configured. (Throwing here is better than silently ignoring the configured sandbox.)
+ :feature :none
[query]
(apply-sandboxing* query))
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj
index cbf9829b590..ea16d36a1b1 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj
@@ -192,9 +192,9 @@
;; 'grant' the block permissions.
(testing "the highest permission level from any group wins (block doesn't override other groups anymore)"
(data-perms/set-database-permission! group-id (mt/id) :perms/view-data :blocked)
- (testing "if EE token does not have the `:advanced-permissions` feature: should not do check"
+ (testing "if EE token does not have the `:advanced-permissions` feature, but we're in EE, we still do the check"
(mt/with-premium-features #{}
- (is (nil? (check-block-perms)))))
+ (is (true? (check-block-perms)))))
(testing "should still not be able to run ad-hoc query"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
@@ -339,6 +339,46 @@
(is (= [[1] [2]] (mt/rows (process-query-for-card child-card)))
"view-data = unrestricted is sufficient to allow running the query"))))))))))
+(deftest blocked-in-ee-no-token-test
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card parent-card {:dataset_query {:database (mt/id)
+ :type :native
+ :native {:query "SELECT id FROM venues ORDER BY id ASC LIMIT 2;"}}
+ :database_id (mt/id)
+ :collection_id (u/the-id collection)}
+ :model/Card child-card {:dataset_query {:database (mt/id)
+ :type :query
+ :query {:source-table (format "card__%d" (u/the-id parent-card))}}
+ :collection_id (u/the-id collection)}]
+ (letfn [(rasta-view-data-perm= [perm] (is (= perm
+ (get-in (data-perms/permissions-for-user (mt/user->id :rasta))
+ [(mt/id) :perms/view-data (mt/id :venues)]))
+ "rasta should be blocked for this table."))]
+ (mt/with-full-data-perms-for-all-users!
+ (data-perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :blocked)
+ (perms/grant-collection-read-permissions! (perms-group/all-users) collection)
+ (letfn [(process-query-for-card [card]
+ (mt/user-http-request :rasta :post (format "card/%d/query" (u/the-id card))))]
+ (mt/with-test-user :rasta
+ (rasta-view-data-perm= :blocked)
+ (testing "Should STILL not be able to do it even without token"
+ (mt/with-test-user :rasta
+ (is (mi/can-read? parent-card))
+ (is (mi/can-read? collection))
+ (is (mi/can-read? child-card)))
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"You do not have permissions to run this query"
+ (mt/rows (mt/with-premium-features #{:advanced-permissions} (process-query-for-card child-card))))
+ "With premium feature")
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"You do not have permissions to run this query"
+ (mt/rows (mt/with-premium-features #{} (process-query-for-card child-card))))
+ "Without premium feature behavior is the same"))))))))))
+
(deftest cannot-run-any-native-queries-when-blocked-test
(mt/with-premium-features #{:advanced-permissions}
(mt/with-non-admin-groups-no-root-collection-perms
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj
index 539c9d0515a..38701d28027 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj
@@ -4,6 +4,7 @@
[clojure.test :refer :all]
[metabase-enterprise.advanced-permissions.query-processor.middleware.permissions :as ee.qp.perms]
[metabase-enterprise.sandbox.query-processor.middleware.sandboxing :as sandboxing]
+ [metabase-enterprise.test :as met]
[metabase.lib.core :as lib]
[metabase.lib.schema :as lib.schema]
[metabase.lib.test-metadata :as meta]
@@ -25,7 +26,7 @@
(let [all-users-group-id (u/the-id (perms/all-users-group))
db-id (u/the-id db-or-id)
revision (:revision (data-perms.graph/api-graph))]
- (mt/with-premium-features #{:advanced-permissions}
+ (mt/with-additional-premium-features #{:advanced-permissions}
(perms.test-util/with-restored-data-perms!
(data-perms.graph/update-data-perms-graph! {:revision revision
:groups {all-users-group-id {db-id {:download graph}}}})
@@ -165,11 +166,7 @@
(deftest check-download-permissions-when-advanced-mbql-sandboxed-test
(testing "Applying a basic sandbox does not affect the download permissions for a table"
(mt/with-current-user (mt/user->id :rasta)
- (mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/mbql-query checkins {:filter [:> $date "2014-01-01"]})}
- :model/Sandbox _ {:group_id (u/the-id (perms/all-users-group))
- :table_id (mt/id :checkins)
- :card_id card-id
- :attribute_remappings {}}]
+ (met/with-gtaps! {:gtaps {:checkins {:query (mt/mbql-query checkins {:filter [:> $date "2014-01-01"]})}}}
(with-download-perms! (mt/id) {:schemas {"PUBLIC" {(mt/id :users) :full
(mt/id :categories) :none
(mt/id :venues) :limited
@@ -186,11 +183,7 @@
(deftest check-download-permissions-when-advanced-sql-sandboxed-test
(testing "Applying a advanced sandbox does not affect the download permissions for a table"
(mt/with-current-user (mt/user->id :rasta)
- (mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/native-query {:query "SELECT ID FROM CHECKINS"})}
- :model/Sandbox _ {:group_id (u/the-id (perms/all-users-group))
- :table_id (mt/id :checkins)
- :card_id card-id
- :attribute_remappings {}}]
+ (met/with-gtaps! {:gtaps {:checkins {:query (mt/native-query {:query "SELECT ID FROM CHECKINS"})}}}
(with-download-perms! (mt/id) {:schemas {"PUBLIC" {(mt/id :users) :full
(mt/id :categories) :none
(mt/id :venues) :limited
@@ -198,11 +191,11 @@
(mt/id :products) :limited
(mt/id :people) :limited
(mt/id :reviews) :limited
- (mt/id :orders) :limited}}})
- (mt/with-metadata-provider (mt/id)
- (let [with-sandbox (apply-row-level-permissions (mbql-download-query 'checkins))]
- (is (= with-sandbox
- (check-download-permissions with-sandbox)))))))))
+ (mt/id :orders) :limited}}}
+ (mt/with-metadata-provider (mt/id)
+ (let [with-sandbox (apply-row-level-permissions (mbql-download-query 'checkins))]
+ (is (= with-sandbox
+ (check-download-permissions with-sandbox))))))))))
;;; +----------------------------------------------------------------------- -----------------------------------------+
;;; | E2E tests |
@@ -443,11 +436,7 @@
(testing "Do we correctly check download perms for queries that involve a sandbox? (#57861)"
(mt/with-full-data-perms-for-all-users!
(with-redefs [ee.qp.perms/max-rows-in-limited-downloads 3]
- (mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/native-query {:query "SELECT ID FROM CHECKINS"})}
- :model/Sandbox _ {:group_id (u/the-id (perms/all-users-group))
- :table_id (mt/id :checkins)
- :card_id card-id
- :attribute_remappings {}}]
+ (met/with-gtaps! {:gtaps {:checkins {:query (mt/native-query {:query "SELECT ID FROM CHECKINS"})}}}
(with-download-perms! (mt/id) {:schemas {"PUBLIC" {(mt/id :categories) :none
(mt/id :checkins) :full}}}
(streaming-test/do-test!
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
index d190a405362..ff0e7896e99 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
@@ -758,3 +758,41 @@
java.lang.Exception
(mt/run-mbql-query checkins
{:aggregation [[:count]]}))))))))))))
+
+(deftest impersonated-throws-without-token-test
+ (mt/test-drivers (mt/normal-drivers-with-feature :connection-impersonation)
+ (mt/with-premium-features #{:advanced-permissions}
+ (let [venues-table (sql.tx/qualify-and-quote driver/*driver* "test-data" "venues")
+ checkins-table (sql.tx/qualify-and-quote driver/*driver* "test-data" "checkins")
+ role-a (u/lower-case-en (mt/random-name))
+ role-b (u/lower-case-en (mt/random-name))]
+ (tx/with-temp-roles! driver/*driver*
+ (impersonation-granting-details driver/*driver* (mt/db))
+ {role-a {venues-table {}}
+ role-b {checkins-table {}}}
+ (impersonation-default-user driver/*driver*)
+ (impersonation-default-role driver/*driver*)
+ (mt/with-temp [:model/Database database {:engine driver/*driver*,
+ :details (impersonation-details driver/*driver* (mt/db))}]
+ (mt/with-db database
+ (when (driver/database-supports? driver/*driver* :connection-impersonation-requires-role nil)
+ (t2/update! :model/Database :id (mt/id) (assoc-in (mt/db) [:details :role] (impersonation-default-role driver/*driver*))))
+ (sync/sync-database! database {:scan :schema})
+ ;; this creates impersonations for the rasta user by default, and does `(request/with-test-user :rasta ...)`
+ (impersonation.util-test/with-impersonations! {:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]
+ :attributes {"impersonation_attr" role-a}}
+ (mt/with-premium-features #{}
+ (testing "impersonated user is blocked"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"Advanced Permissions is a paid feature not currently available"
+ (mt/formatted-rows [int]
+ (mt/run-mbql-query venues
+ {:aggregation [[:count]]})))))
+ (testing "admin should still be able to query"
+ (request/as-admin
+ (is (= [100]
+ (map
+ long
+ (mt/first-row
+ (mt/run-mbql-query venues
+ {:aggregation [[:count]]}))))))))))))))))
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.clj
index 67e67848af1..77824739ae2 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
@@ -38,6 +38,8 @@
(use-fixtures :once (fixtures/initialize :db))
+(def sandboxing-disabled-error #"Sandboxing is a paid feature")
+
(defn- identifier
([table-key]
(qp.store/with-metadata-provider (mt/id)
@@ -266,19 +268,29 @@
(filter #(isa? driver/hierarchy % :sql))
(mt/normal-drivers-with-feature :nested-queries)))
+(defmacro ^:private fails-without-token
+ "built to make small assertions that sandboxing DOES NOT WORK without a valid token enabling the feature, and that
+ instead an exception is thrown informing the user that sandboxing is disabled."
+ [& body]
+ `(testing "and it fails when the feature isn't available"
+ (mt/with-premium-features #{}
+ (~'is (~'thrown-with-msg? clojure.lang.ExceptionInfo sandboxing-disabled-error ~@body)))))
+
(deftest e2e-test-1
(mt/test-drivers (e2e-test-drivers)
(testing "Basic test around querying a table by a user with segmented only permissions and a GTAP question that is a native query"
(met/with-gtaps! {:gtaps {:venues (venues-category-native-sandbox-def)}, :attributes {"cat" 50}}
(is (= [[10]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-2
(mt/test-drivers (e2e-test-drivers)
(testing "Basic test around querying a table by a user with segmented only permissions and a GTAP question that is MBQL"
(met/with-gtaps! {:gtaps {:venues (venues-category-mbql-gtap-def)}, :attributes {"cat" 50}}
(is (= [[10]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-3
(mt/test-drivers (e2e-test-drivers)
@@ -288,7 +300,8 @@
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Query requires user attribute `cat`"
- (mt/run-mbql-query venues {:aggregation [[:count]]})))))))
+ (mt/run-mbql-query venues {:aggregation [[:count]]})))
+ (fails-without-token (mt/run-mbql-query venues {:aggregation [[:count]]}))))))
(deftest e2e-test-4
(mt/test-drivers (e2e-test-drivers)
@@ -298,14 +311,16 @@
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Query requires user attribute `cat`"
- (mt/run-mbql-query venues {:aggregation [[:count]]})))))))
+ (mt/run-mbql-query venues {:aggregation [[:count]]})))
+ (fails-without-token (mt/run-mbql-query venues {:aggregation [[:count]]}))))))
(deftest e2e-test-5
(mt/test-drivers (e2e-test-drivers)
(testing "Another basic test, same as above, but with a numeric string that needs to be coerced"
(met/with-gtaps! {:gtaps {:venues (venues-category-mbql-gtap-def)}, :attributes {"cat" "50"}}
(is (= [[10]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-6
(mt/test-drivers (e2e-test-drivers)
@@ -314,7 +329,8 @@
:remappings {:cat ["variable" [:field (mt/id :venues :latitude) nil]]}}}
:attributes {"cat" "34.1018"}}
(is (= [[3]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-7
(mt/test-drivers (e2e-test-drivers)
@@ -323,7 +339,8 @@
:remappings {:something.different ["variable" ["template-tag" "cat"]]}}}
:attributes {"something.different" 50}}
(is (= [[10]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-8
(mt/test-drivers (e2e-test-drivers)
@@ -333,7 +350,9 @@
(mt/formatted-rows
[int str]
(mt/run-mbql-query venues
- {:limit 2, :order-by [[:asc [:field (mt/id :venues :id)]]]}))))))))
+ {:limit 2, :order-by [[:asc [:field (mt/id :venues :id)]]]}))))
+ (fails-without-token (mt/run-mbql-query venues
+ {:limit 2, :order-by [[:asc [:field (mt/id :venues :id)]]]}))))))
(deftest e2e-test-9
(mt/test-drivers (e2e-test-drivers)
@@ -341,7 +360,8 @@
(met/with-gtaps! {:gtaps {:venues (dissoc (venues-category-mbql-gtap-def) :query)}
:attributes {"cat" 50}}
(is (= [[10]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-10
(mt/test-drivers (e2e-test-drivers)
@@ -349,7 +369,8 @@
(met/with-gtaps! {:gtaps {:venues (dissoc (venues-category-mbql-gtap-def) :query)}
:attributes {"cat" "50"}}
(is (= [[10]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (fails-without-token (run-venues-count-query))))))
(deftest e2e-test-11
(mt/test-drivers (e2e-test-drivers)
@@ -357,7 +378,10 @@
(met/with-gtaps-for-user! :crowberto {:gtaps {:venues (venues-category-mbql-gtap-def)}
:attributes {"cat" 50}}
(is (= [[100]]
- (run-venues-count-query)))))))
+ (run-venues-count-query)))
+ (testing "this works the same without sandboxing enabled"
+ (mt/with-premium-features #{}
+ (= [[100]] (run-venues-count-query))))))))
(deftest e2e-test-12
(mt/test-drivers (e2e-test-drivers)
@@ -367,7 +391,13 @@
(mt/with-test-user :rasta
(request/as-admin
(is (= [[100]]
- (run-venues-count-query)))))))))
+ (run-venues-count-query)))))
+ (mt/with-premium-features #{}
+ (testing "This works the same without sandboxing enabled"
+ (mt/with-test-user :rasta
+ (request/as-admin
+ (= [[100]]
+ (run-venues-count-query))))))))))
(deftest e2e-test-13
(mt/test-drivers (e2e-test-drivers)
@@ -389,7 +419,14 @@
(qp/process-query {:database (mt/id)
:type :query
:query {:source-table (mt/id :venues)
- :limit 1}}))))))))))))))
+ :limit 1}})))))))
+ (mt/with-premium-features #{}
+ (testing "this works the same without sandboxing enabled"
+ (mt/with-test-user :rasta
+ (binding [qp.perms/*card-id* (u/the-id card)]
+ (is (= 1
+ (count (mt/rows
+ (qp/process-query (mt/mbql-query venues {:limit 1})))))))))))))))))
(deftest e2e-test-14
(mt/test-drivers (e2e-test-drivers)
@@ -411,7 +448,15 @@
(is (= [[100]]
(mt/format-rows-by
[int]
- (mt/rows (qp/process-query query)))))))))))
+ (mt/rows (qp/process-query query)))))))
+ (mt/with-premium-features #{}
+ (testing "this works the same without sandboxing enabled"
+ (mt/with-test-user :rasta
+ (mt/with-native-query-testing-context query
+ (is (= [[100]]
+ (mt/format-rows-by
+ [int]
+ (mt/rows (qp/process-query query)))))))))))))
;; Test that we can follow FKs to related tables and breakout by columns on those related tables. This test has
;; several things wrapped up which are detailed below
@@ -436,7 +481,8 @@
:venues nil}
:attributes {"user" 5}}
(is (= [[1 10] [2 36] [3 4] [4 5]]
- (run-checkins-count-broken-out-by-price-query)))))))
+ (run-checkins-count-broken-out-by-price-query)))
+ (fails-without-token (run-checkins-count-broken-out-by-price-query))))))
(deftest e2e-fks-test-2
(mt/test-drivers (sandboxing-fk-drivers)
@@ -447,7 +493,8 @@
:venues (venues-price-mbql-gtap-def)}
:attributes {"user" 5, "price" 1}}
(is (= #{[nil 45] [1 10]}
- (set (run-checkins-count-broken-out-by-price-query))))))))
+ (set (run-checkins-count-broken-out-by-price-query))))
+ (fails-without-token (run-checkins-count-broken-out-by-price-query))))))
(deftest e2e-fks-test-3
(mt/test-drivers (sandboxing-fk-drivers)
@@ -456,7 +503,8 @@
:venues (dissoc (venues-price-mbql-gtap-def) :query)}
:attributes {"user" 5, "price" 1}}
(is (= #{[nil 45] [1 10]}
- (set (run-checkins-count-broken-out-by-price-query))))))))
+ (set (run-checkins-count-broken-out-by-price-query))))
+ (fails-without-token (run-checkins-count-broken-out-by-price-query))))))
(deftest e2e-fks-test-4
(mt/test-drivers (sandboxing-fk-drivers)
@@ -474,7 +522,11 @@
(mt/run-mbql-query checkins
{:aggregation [[:count]]
:order-by [[:asc $venue_id->venues.price]]
- :breakout [$venue_id->venues.price $user_id->users.name]}))))))))))
+ :breakout [$venue_id->venues.price $user_id->users.name]}))))))
+ (fails-without-token (mt/run-mbql-query checkins
+ {:aggregation [[:count]]
+ :order-by [[:asc $venue_id->venues.price]]
+ :breakout [$venue_id->venues.price $user_id->users.name]}))))))
(defn- run-query-returning-remark! [run-query-fn]
(let [remark (atom nil)
@@ -507,7 +559,10 @@
(mt/rows
(mt/run-mbql-query venues
{:aggregation [[:count]]
- :breakout [$price]})))))))))
+ :breakout [$price]})))))
+ (fails-without-token (mt/run-mbql-query venues
+ {:aggregation [[:count]]
+ :breakout [$price]}))))))
(deftest sql-with-join-test
(mt/test-drivers (into #{}
@@ -527,7 +582,9 @@
[72 1]]
(mt/formatted-rows
[int int]
- (qp/process-query query))))))))))
+ (qp/process-query query)))))
+ (fails-without-token (mt/with-native-query-testing-context query
+ (qp/process-query query))))))))
(deftest sql-with-join-test-2
(mt/test-drivers (into #{}
@@ -536,16 +593,18 @@
(testing (str "If we use a parameterized SQL GTAP that joins a Table the user doesn't have access to, does it "
"still work? (EE #230) If we pass the query in directly without anything that would require nesting "
"it, it should work")
- (is (= [[2 1]
- [72 1]]
- (mt/format-rows-by
- [int int]
- (mt/rows
- (met/with-gtaps! {:gtaps {:checkins (parameterized-sql-with-join-sandbox-def)}
- :attributes {"user" 1}}
+ (met/with-gtaps! {:gtaps {:checkins (parameterized-sql-with-join-sandbox-def)}
+ :attributes {"user" 1}}
+ (is (= [[2 1]
+ [72 1]]
+ (mt/format-rows-by
+ [int int]
+ (mt/rows
(mt/run-mbql-query checkins
{:order-by [[:asc $id]]
- :limit 2})))))))))
+ :limit 2})))))
+ (fails-without-token (mt/run-mbql-query checkins {:order-by [[:asc $id]]
+ :limit 2}))))))
(defn- do-correct-metadata-test [f]
(let [cols (fn []
@@ -579,7 +638,8 @@
(met/with-gtaps! {:gtaps {:venues (dissoc (venues-category-mbql-gtap-def) :query)}
:attributes {"cat" 50}}
(is (=? (expected-cols)
- (cols)))))))))
+ (cols)))
+ (fails-without-token (cols))))))))
(deftest correct-metadata-test-2
(testing (str "We should return the same metadata as the original Table when running a query against a sandboxed "
@@ -590,7 +650,8 @@
(met/with-gtaps! {:gtaps {:venues (venues-category-mbql-gtap-def)}
:attributes {"cat" 50}}
(is (=? (expected-cols)
- (cols)))))))))
+ (cols)))
+ (fails-without-token (cols))))))))
(deftest correct-metadata-test-3
(testing (str "We should return the same metadata as the original Table when running a query against a sandboxed "
@@ -609,7 +670,8 @@
:remappings {:cat ["variable" ["template-tag" "cat"]]}}}
:attributes {"cat" 50}}
(is (=? (expected-cols)
- (cols)))))))))
+ (cols)))
+ (fails-without-token (cols))))))))
(deftest correct-metadata-test-4
(testing (str "We should return the same metadata as the original Table when running a query against a sandboxed "
@@ -630,7 +692,8 @@
:attributes {"cat" 50}}
(let [[id-col name-col _ _ longitude-col price-col] (expected-cols)]
(is (=? [id-col name-col longitude-col price-col]
- (cols))))))))))
+ (cols)))
+ (fails-without-token (cols)))))))))
(deftest sql-with-joins-test
(testing "Should be able to use a Saved Question with no source Metadata as a GTAP (EE #525)"
@@ -660,7 +723,8 @@
(is (= [[2 "2014-09-18T00:00:00Z" 1 31 31 "Bludso's BBQ" 5 33.8894 -118.207 2]
[72 "2015-04-18T00:00:00Z" 1 1 1 "Red Medicine" 4 10.0646 -165.374 3]
[80 "2013-12-27T00:00:00Z" 1 99 99 "Golden Road Brewing" 10 34.1505 -118.274 2]]
- (mt/rows (qp/process-query query)))))))))
+ (mt/rows (qp/process-query query))))
+ (fails-without-token (qp/process-query query)))))))
(deftest run-sql-queries-to-infer-columns-test
(testing "Run SQL queries to infer the columns when used as GTAPS (#13716)\n"
@@ -684,6 +748,14 @@
(testing "GTAP Card should not yet current have result_metadata"
(is (= nil
(t2/select-one-fn :result_metadata :model/Card :id venues-gtap-card-id))))
+ (fails-without-token (mt/run-mbql-query venues
+ {:fields [$id $name] ; joined fields get appended automatically because we specify :all :below
+ :joins [{:fields :all
+ :source-table $$venues
+ :condition [:= $id &Venue.id]
+ :alias "Venue"}]
+ :order-by [[:asc $id]]
+ :limit 3}))
(testing "Should be able to run the query"
(is (= [[1 "Red Medicine" 1 "Red Medicine" 4 10.0646 -165.374 3]]
(mt/rows
@@ -744,7 +816,8 @@
(fn [{:keys [run-query]}]
(testing "Query without weird stuff going on should work"
(is (= [[1 "Red Medicine" 1 "Red Medicine" 4 10.0646 -165.374 3]]
- (mt/rows (run-query))))))))))
+ (mt/rows (run-query))))
+ (fails-without-token (run-query))))))))
(deftest run-queries-to-infer-columns-error-on-column-type-changes-test-2
(testing "If we have to run a query to infer columns (see above) we should validate column constraints (#14099)\n"
@@ -758,7 +831,8 @@
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Sandbox Questions can't return columns that have different types than the Table they are sandboxing"
- (run-query)))))))))
+ (run-query))))
+ (fails-without-token (run-query)))))))
(deftest run-queries-to-infer-columns-error-on-column-type-changes-test-3
(testing "If we have to run a query to infer columns (see above) we should validate column constraints (#14099)\n"
@@ -770,7 +844,8 @@
"WHERE ID IN ({{sandbox}})")
(fn [{:keys [run-query]}]
(is (= [[1 "Red Medicine" 1 "Red Medicine" 4 10.0646 -165.374 3]]
- (mt/rows (run-query))))))))))
+ (mt/rows (run-query))))
+ (fails-without-token (run-query))))))))
(deftest dont-cache-sandboxes-test
(cache-test/with-mock-cache! [save-chan]
@@ -1599,4 +1674,14 @@
[:field {:join-alias "people" :lib/uuid string?} (mt/id :people :email)]
[:field {:join-alias "people" :lib/uuid string?} (mt/id :people :name)]
[:field {:join-alias "people" :lib/uuid string?} (mt/id :people :city)]]
- (-> preprocessed lib/joins first lib/join-fields)))))))))
+ (-> preprocessed lib/joins first lib/join-fields))))
+ (testing "Preprocessing fails when we don't have a valid token"
+ (mt/with-premium-features #{}
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo sandboxing-disabled-error (qp.preprocess/preprocess query))))))))))
+
+(deftest sandboxing-throws-on-ee-without-token
+ (mt/test-drivers (e2e-test-drivers)
+ (testing "Basic test around querying a table by a user with segmented only permissions and a GTAP question that is a native query"
+ (met/with-gtaps! {:gtaps {:venues (venues-category-native-sandbox-def)}, :attributes {"cat" 50}}
+ (mt/with-premium-features #{}
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo sandboxing-disabled-error (run-venues-count-query))))))))
diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljc
index b6f43ce51ae..83ff2ab4e58 100644
--- a/src/metabase/lib/core.cljc
+++ b/src/metabase/lib/core.cljc
@@ -500,4 +500,5 @@
all-template-tags
all-template-tags-map
all-template-tags-id->field-ids
- any-native-stage?])
+ any-native-stage?
+ any-native-stage-not-introduced-by-sandbox?])
diff --git a/src/metabase/lib/walk/util.cljc b/src/metabase/lib/walk/util.cljc
index 77359783929..bc60404fd25 100644
--- a/src/metabase/lib/walk/util.cljc
+++ b/src/metabase/lib/walk/util.cljc
@@ -104,6 +104,26 @@
nil))
@has-native-stage?))
+(mu/defn any-native-stage-not-introduced-by-sandbox?
+ "Sandboxing can introduce native stages to a query, because, for example, a table can be replaced with a Question that
+ is based on a native query.
+
+ Sometimes we need to check whether the query has a native stage that was introduced at the 'user level' (this could
+ be the user's question itself, or one of the sources of their question, e.g. if the user makes Card A -> Card B ->
+ Card C where Card C is a native question), rather than a native stage that was introduced by a sandbox (e.g. Card A
+ -> Table B, which is swapped by a sandbox for Card C where Card C is a native question)."
+ [query :- ::lib.schema/query]
+ (let [has-native-stage? (volatile! false)]
+ (lib.walk/walk-stages
+ query
+ (fn [_query _path stage]
+ (when (and (not @has-native-stage?)
+ (not (:query-permissions/sandboxed-table stage))
+ (= (:lib/type stage) :mbql.stage/native))
+ (vreset! has-native-stage? true))
+ nil))
+ @has-native-stage?))
+
(mu/defn all-field-ids :- [:set ::lib.schema.id/field]
"Set of all Field IDs referenced in `:field` refs in a query or MBQL clause."
[query-or-clause :- [:or
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 61fbb8fd5b1..dbb28eba227 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -46,7 +46,7 @@
;; 2. Make sure we're in Honey SQL 2 mode for all the little SQL snippets we're compiling in these
;; tests.
(binding [sync-util/*log-exceptions-and-continue?* false]
- (thunk))))
+ (mt/with-test-user :rasta (thunk)))))
(deftest all-zero-dates-test
(mt/test-driver :mysql
diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.clj
index 448b86e8680..d97af3bef63 100644
--- a/test/metabase/driver/postgres_test.clj
+++ b/test/metabase/driver/postgres_test.clj
@@ -60,7 +60,7 @@
;; 2. Make sure we're in Honey SQL 2 mode for all the little SQL snippets we're compiling in these
;; tests.
(binding [sync-util/*log-exceptions-and-continue?* false]
- (thunk))))
+ (mt/with-test-user :rasta (thunk)))))
(deftest ^:parallel interval-test
(is (= ["INTERVAL '2 day'"]
diff --git a/test/metabase/driver/sql_jdbc/actions_test.clj b/test/metabase/driver/sql_jdbc/actions_test.clj
index 962cb280547..46c79366b6b 100644
--- a/test/metabase/driver/sql_jdbc/actions_test.clj
+++ b/test/metabase/driver/sql_jdbc/actions_test.clj
@@ -17,6 +17,10 @@
[toucan2.core :as t2])
(:import (clojure.lang ExceptionInfo)))
+(use-fixtures :each (fn [t]
+ (mt/with-test-user :rasta
+ (t))))
+
(mu/defn- cast-values :- ::actions.args/row
[driver :- :keyword
column->value :- ::actions.args/row