Skip to content

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

[perms] CSV uploads get `view-data: unrestricted` if whole schema has same (#76351)

Braden Shepherdson379f8bbd84ac
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj
index 3ded2245670..389f6003344 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj
@@ -182,6 +182,25 @@
(concat impersonation-group-ids sandbox-group-ids (perms/data-app-group-ids)))]
(zipmap group-ids (map #(if (blocked-groups %) :blocked :unrestricted) group-ids)))))
+(defenterprise new-table-sandboxed-groups
+ "Returns the subset of `group-ids` that must have new tables on `db-id` forced to `:blocked` view-data regardless of
+ the new table's schema, because they have a sandbox on a table in this DB. A sandbox is a stronger condition than
+ a few specific tables being blocked, so the presence of a sandbox on this DB for a group is sufficient to make any
+ new table `:blocked` for that group, even if the table came from a CSV upload.
+
+ OSS has no sandboxes, so the OSS implementation returns the empty set. On EE we deliberately do *not* consult
+ `enable-sandboxes?`: as with `apply-sandboxing`, we block *setting up* a sandbox without the feature (see
+ `metabase-enterprise.sandbox.api.gtap`) but keep enforcing one that is already configured, rather than silently
+ ignoring it when a token lapses. See UXW-4927 for the incident where an unlicensed EE instance leaked a
+ newly-synced table to a sandboxed group."
+ :feature :none ;; fail CLOSED if the feature is unavailable.
+ [db-id group-ids]
+ (if (empty? group-ids)
+ #{}
+ (into #{}
+ (map :group_id)
+ (advanced-permissions.db/sandboxed-group-ids-for-database db-id group-ids))))
+
(defenterprise new-table-view-data-permission-levels
"Returns a map of {group-id → permission-level} for multiple groups and a single DB."
:feature :none ;; fail CLOSED if the feature is unavailable.
@@ -191,9 +210,7 @@
;; We don't check for connection impersonations here, because impersonations are set at the
;; DB-level, so a new table should get `:unrestricted` and inherit the DB-level impersonation policy.
(let [blocked-group-ids (advanced-permissions.db/blocked-group-ids-for-database db-id group-ids)
- sandbox-group-ids (into #{}
- (map :group_id)
- (advanced-permissions.db/sandboxed-group-ids-for-database db-id group-ids))
+ sandbox-group-ids (new-table-sandboxed-groups db-id group-ids)
app-group-ids (set (perms/data-app-group-ids))
app-view-data (when (some app-group-ids group-ids)
(perms/data-app-view-data-permission-level db-id))
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
index 7749f3da654..4aa87d861d6 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
@@ -246,6 +246,65 @@
(is (= :blocked (perm-value new-table-id))
"New table blocks the sandboxed group instead of leaking :unrestricted")))))))))
+(deftest uploaded-table-without-premium-features-test
+ (testing "An *uploaded* table fails CLOSED to :blocked for a sandboxed group even without premium features"
+ ;; The upload override (UXW-3217) prefers the schema's unanimous :unrestricted over the DB-wide
+ ;; override, so the sandbox guard is the only thing standing between a sandboxed group and the
+ ;; uploaded table. That guard must not consult the token: an EE instance whose token has lapsed
+ ;; still has sandboxes configured, and silently ignoring them is the UXW-4927 leak by another path.
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/Database {db-id :id} {}
+ :model/Table {granted-table :id} {:db_id db-id :schema "PUBLIC"}
+ :model/Table {sandboxed-table :id} {:db_id db-id :schema "OTHER"}
+ :model/Sandbox _ {:group_id group-id :table_id sandboxed-table}]
+ ;; Unanimous :unrestricted for every existing table in the upload target schema, so that the
+ ;; schema-consistency rule *would* hand the new table over if the sandbox guard didn't fire.
+ (data-perms/set-table-permission! group-id granted-table :perms/view-data :unrestricted)
+ (doseq [features [#{} #{:advanced-permissions} #{:sandboxes} #{:advanced-permissions :sandboxes}]]
+ (testing (format "premium features = %s" (pr-str features))
+ (mt/with-premium-features features
+ (is (= #{group-id}
+ (advanced-permissions.common/new-table-sandboxed-groups db-id [group-id]))
+ "The sandbox guard does not consult the token")
+ (mt/with-temp [:model/Table {new-table-id :id} {:db_id db-id
+ :schema "PUBLIC"
+ :data_source :upload}]
+ (is (= :blocked
+ (t2/select-one-fn :perm_value :model/DataPermissions
+ :db_id db-id :group_id group-id
+ :table_id new-table-id :perm_type :perms/view-data))
+ "Uploaded table blocks the sandboxed group instead of inheriting the schema's :unrestricted"))))))))
+
+(deftest new-table-in-granted-schema-test
+ ;; Grant the "public" schema, block the "blocked" schema -- exactly the upload setup. The group
+ ;; therefore has a :blocked table in the DB, so the EE DB-wide override returns :blocked for any
+ ;; brand-new table (verified below). Uses the *real* enterprise functions, no redefs.
+ (mt/with-additional-premium-features #{:advanced-permissions}
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id-1 :id} {:db_id db-id :schema "public"}
+ :model/Table {table-id-2 :id} {:db_id db-id :schema "blocked"}]
+ (data-perms/set-table-permission! group-id table-id-1 :perms/view-data :unrestricted)
+ (data-perms/set-table-permission! group-id table-id-2 :perms/view-data :blocked)
+ (is (= {group-id :blocked}
+ (advanced-permissions.common/new-table-view-data-permission-levels db-id [group-id]))
+ "precondition: the DB-wide override would block a new table for this group")
+ (testing "Sync (default): a synced table in the granted schema fails safe to :blocked"
+ (mt/with-temp [:model/Table {table-id-3 :id} {:db_id db-id :schema "public"}]
+ (is (= :blocked
+ (t2/select-one-fn :perm_value :model/DataPermissions
+ :db_id db-id :group_id group-id
+ :table_id table-id-3 :perm_type :perms/view-data)))))
+ (testing "Upload (`:data_source :upload`): an uploaded table in the granted schema inherits :unrestricted (UXW-3217)"
+ (mt/with-temp [:model/Table {table-id-3 :id} {:db_id db-id
+ :schema "public"
+ :data_source :upload}]
+ (is (= :unrestricted
+ (t2/select-one-fn :perm_value :model/DataPermissions
+ :db_id db-id :group_id group-id
+ :table_id table-id-3 :perm_type :perms/view-data))
+ "new table in the granted schema must inherit :unrestricted, not :blocked"))))))
+
(deftest new-group-view-data-permission-levels-test
(mt/with-additional-premium-features #{:sandboxes :advanced-permissions}
(mt/with-temp [:model/Database {db-id :id} {}]
@@ -1045,6 +1104,51 @@
:create-queries :query-builder-and-native}}
(is (some? (upload-csv!)))))))))))
+(deftest upload-csv-keeps-permissions-on-granted-schema-test
+ (testing "Upload to fully `:unrestricted` schema doesn't get `:blocked`, so uploads keep working (UXW-3217)"
+ (mt/test-drivers (mt/normal-drivers-with-feature :uploads :schemas)
+ (mt/with-additional-premium-features #{:advanced-permissions}
+ ;; An empty, *dynamic* dataset: the upload physically creates a table, which drivers like Snowflake refuse
+ ;; to do in a shared static dataset (see `metabase.test.data.snowflake/create-table!`).
+ (mt/dataset (mt/dataset-definition "advanced_permissions" [])
+ (let [db-id (mt/id)
+ schema-name (sql.tx/session-schema driver/*driver*)
+ all-users-id (u/the-id (perms-group/all-users))]
+ (mt/with-restored-data-perms-for-group! all-users-id
+ ;; Grant the whole DB, then carve out a :blocked table in a *different* schema. This is the corrupting
+ ;; condition in UXW-3217: the group now has a blocked view-data row *anywhere* in the DB, so any new
+ ;; table would be forced to `:blocked`, except for the special handling of uploads being tested here.
+ (data-perms/set-database-permission! all-users-id db-id :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! all-users-id db-id :perms/create-queries :query-builder)
+ (mt/with-temp [:model/Table {granted-table :id} {:db_id db-id
+ :schema schema-name
+ :active true}
+ :model/Table {blocked-table :id} {:db_id db-id
+ :schema "uxw3217_other_schema"
+ :active true}]
+ ;; Set both rows explicitly rather than relying on the going-granular expansion to cover the upload
+ ;; target schema: the dataset is empty, so there is no other table there to make it unanimous.
+ (data-perms/set-table-permission! all-users-id granted-table :perms/view-data :unrestricted)
+ (data-perms/set-table-permission! all-users-id blocked-table :perms/view-data :blocked)
+ (is (= {all-users-id :blocked}
+ (advanced-permissions.common/new-table-view-data-permission-levels db-id [all-users-id]))
+ "precondition: the DB-wide override would block a new table for All Users")
+ (upload-test/do-with-uploaded-example-csv!
+ {:grant-permission? false
+ :schema-name schema-name
+ :table-prefix "uxw3217_"}
+ (fn [model]
+ (let [uploaded-table (t2/select-one [:model/Table :id :schema] :id (:table_id model))
+ uploaded-schema (:schema uploaded-table)]
+ (is (= :unrestricted
+ (data-perms/table-permission-for-groups #{all-users-id} :perms/view-data
+ db-id (:id uploaded-table)))
+ "uploaded table in the granted schema must stay :unrestricted")
+ (testing "so the user's effective schema permission stays :unrestricted and they can upload again"
+ (is (= :unrestricted
+ (data-perms/full-schema-permission-for-user
+ (mt/user->id :rasta) :perms/view-data db-id uploaded-schema)))))))))))))))
+
(deftest update-csv-data-perms-test
(mt/test-drivers (mt/normal-drivers-with-feature :uploads)
(mt/dataset (mt/dataset-definition "advanced_permissions" [])
diff --git a/src/metabase/permissions/db.clj b/src/metabase/permissions/db.clj
index 56a32c73cd7..9b0d2312f26 100644
--- a/src/metabase/permissions/db.clj
+++ b/src/metabase/permissions/db.clj
@@ -729,9 +729,12 @@
(t2/exists? :model/Database :id database-id :router_database_id [:not= nil]))
(mu/defn table-location
- "The ID, Database ID, and schema of the Table with `table-id`."
+ "The ID, Database ID, schema, and data source of the Table with `table-id`.
+
+ `:data_source` is included because new-table permission defaults branch on whether the table came from an upload;
+ see [[metabase.permissions.models.data-permissions/set-default-table-permissions!]]."
[table-id :- ::lib.schema.id/table]
- (t2/select-one [:model/Table :id :db_id :schema] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
+ (t2/select-one [:model/Table :id :db_id :schema :data_source] :id table-id {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))
(mu/defn table-database-id
"The Database ID of the Table with `table-id`."
diff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.clj
index 93b9c61b609..f36954389e9 100644
--- a/src/metabase/permissions/models/data_permissions.clj
+++ b/src/metabase/permissions/models/data_permissions.clj
@@ -1348,6 +1348,16 @@
[]
#{})
+(defenterprise new-table-sandboxed-groups
+ "Returns the subset of `group-ids` whose new tables on `db-id` have a sandbox somewhere in this DB, and must therefore
+ have their `view-data` forced to `:blocked` regardless of the new table's schema.
+
+ On OSS there are no sandboxes, so the set is always empty. The EE implementation is `:feature :none`: a sandbox that
+ is already configured keeps forcing new tables to `:blocked` even when the token no longer grants `:sandboxes`."
+ metabase-enterprise.advanced-permissions.common
+ [_db-id _group-ids]
+ #{})
+
;;; ---------------------------------------- Bulk permission functions ------------------------------------------------
;; These functions set permissions for newly-created entities (groups, databases, tables) using batch SQL operations
;; instead of per-row mutations. They are intended to be called from within a coarse cluster lock.
@@ -1507,19 +1517,43 @@
(update-in acc [group_id perm_type schema_name] (fnil conj #{}) perm_value))
{} table-level)
:all-db-tables (permissions.db/active-table-locations-for-database db-id)
- :view-data-levels (new-table-view-data-permission-levels db-id group-ids)}))
+ :view-data-levels (new-table-view-data-permission-levels db-id group-ids)
+ :sandboxed-groups (new-table-sandboxed-groups db-id group-ids)}))
(defn- compute-actual-value
- "Per-entry resolution: enterprise view-data override, then schema-consistency
- if all existing tables in the schema agree, else the caller's default."
- [{:keys [view-data-levels schema-vals-idx]}
+ "Per-entry resolution for a new table's permission value for a given `group-id` and `perm-type`.
+
+ For perms other than `view-data`, the new table inherits its schema's value when all existing tables in that schema
+ agree, otherwise the default supplied by the caller.
+
+ For `view-data` the order depends on where the table came from, which we read off its `:data_source`:
+
+ - Sync (any other data source): the enterprise DB-wide override wins. If the group has *any* `:blocked` table (or a
+ sandbox) in the DB, then it has only partial access, so a newly-discovered, unclassified table fails safe to
+ `:blocked`.
+ - Upload (`:data_source` is `:upload`): if the permissions for all (active) tables in the new table's schema are
+ unanimously `:unrestricted`, then the new table is granted the same permission.
+ - This is a specific override for an uploaded table, since otherwise the user would be both locked out of their
+ own freshly uploaded table *and* prevented from making any further uploads! (Since uploads require at least one
+ group with unanimous `:unrestricted` access to the target schema. See UXW-3217.)
+
+ Note that if the group has a sandbox anywhere on this DB, the uploaded table is still `:blocked`, preventing any
+ leak of data to that group which should be sandboxed."
+ [{:keys [sandboxed-groups schema-vals-idx view-data-levels]}
{:keys [group-id perm-type default-value table]}]
- (or (when (= perm-type :perms/view-data)
- (get view-data-levels group-id))
- (let [sv (get-in schema-vals-idx [group-id perm-type (:schema table)])]
- (when (and (seq sv) (= (count sv) 1))
- (first sv)))
- default-value))
+ (let [view-data? (= perm-type :perms/view-data)
+ upload? (= :upload (some-> table :data_source keyword))
+ schema-value (let [sv (get-in schema-vals-idx [group-id perm-type (:schema table)])]
+ (when (and (seq sv) (= (count sv) 1))
+ (first sv)))
+ override (when view-data?
+ (get view-data-levels group-id))]
+ (or (when (and view-data? (contains? sandboxed-groups group-id))
+ :blocked)
+ (if (and view-data? upload?)
+ (or schema-value override)
+ (or override schema-value))
+ default-value)))
(defn- classify-key
"For one `(group-id, perm-type)`, return `{:deletes [id?] :rows [perm-row...]}`.
@@ -1595,7 +1629,10 @@
`group-perm-defaults` is a seq of `{:group-id :perm-type :default-value}`
triples. Thin wrapper over [[set-default-table-permissions-bulk!]] for
- the single-table case."
+ the single-table case.
+
+ `table` may be a Table ID or a Table map. A map must carry `:data_source`, since [[compute-actual-value]] branches
+ on whether the table came from an upload; the `:model/Table` after-insert hook passes the full inserted row."
[table group-perm-defaults]
(let [table (if (map? table)
table
diff --git a/src/metabase/upload/impl.clj b/src/metabase/upload/impl.clj
index 9521f4ff095..73ec10f9bba 100644
--- a/src/metabase/upload/impl.clj
+++ b/src/metabase/upload/impl.clj
@@ -566,10 +566,16 @@
table-name (some->> table-name (ddl.i/format-name driver))
schema+table-name (table-identifier {:schema schema :name table-name})
{:keys [columns stats]} (create-from-csv! driver db schema+table-name filename file)
- ;; Sync immediately to create the Table and its Fields; the scan is settings-dependent and can be async
+ ;; Sync immediately to create the Table and its Fields; the scan is settings-dependent and can be async.
+ ;; `:data_source :upload` must be set *at insert time*: the `:model/Table` after-insert hook is what
+ ;; assigns permissions, and it reads the data source to decide that the uploader -- already checked to
+ ;; have unrestricted access to this schema (see `can-create-upload-error`) -- gets a table that inherits
+ ;; the schema's permissions rather than failing safe to `:blocked` the way a sync-discovered table would
+ ;; (UXW-3217). `mark-table-upload!` below re-asserts it along with the other upload columns.
table (sync/create-table! db {:name table-name
:schema (not-empty schema)
- :display_name display-name})
+ :display_name display-name
+ :data_source :upload})
_set_is_upload (upload.db/mark-table-upload! (:id table))
_sync (scan-and-sync-table! db table)
_set_names (set-display-names! (:id table) columns)
diff --git a/test/metabase/permissions/models/data_permissions_test.clj b/test/metabase/permissions/models/data_permissions_test.clj
index a2e347c0da8..947a6594939 100644
--- a/test/metabase/permissions/models/data_permissions_test.clj
+++ b/test/metabase/permissions/models/data_permissions_test.clj
@@ -1297,6 +1297,49 @@
:table_id table-id-4 :perm_type :perms/create-queries))
"should inherit :query-builder from PUBLIC schema, not the :no default")))))
+(defn- new-table-view-data-perm!
+ "Setup helper that mirrors the UXW-3217 repro: schema `\"public\"` is fully granted to the group, schema
+ `\"blocked-schema\"` is blocked. Because the group has a `:blocked` table in the DB, the usual EE rules will make
+ a new table also `:blocked`. Uploads have a special case that this is intended to test.
+
+ Parameterized by the `:upload?` and `:sandboxed?` options.
+
+ Returns the new table's `view-data` permission."
+ [{:keys [upload? sandboxed?]}]
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/PermissionsGroup {group-id :id} {}
+ :model/Table {table-id-1 :id} {:db_id db-id :schema "public"}
+ :model/Table {table-id-2 :id} {:db_id db-id :schema "public"}
+ :model/Table {table-id-3 :id} {:db_id db-id :schema "blocked-schema"}]
+ (data-perms/set-table-permission! group-id table-id-1 :perms/view-data :unrestricted)
+ (data-perms/set-table-permission! group-id table-id-2 :perms/view-data :unrestricted)
+ (data-perms/set-table-permission! group-id table-id-3 :perms/view-data :blocked)
+ (mt/with-temp [:model/Table {table-id-4 :id} {:db_id db-id :schema "public"}]
+ (t2/delete! :model/DataPermissions :group_id group-id :table_id table-id-4 :perm_type :perms/view-data)
+ (mt/with-dynamic-fn-redefs [data-perms/new-table-view-data-permission-levels
+ (fn [_db-id group-ids] (zipmap group-ids (repeat :blocked)))
+ data-perms/new-table-sandboxed-groups
+ (fn [_db-id group-ids] (if sandboxed? (set group-ids) #{}))]
+ (data-perms/set-default-table-permissions!
+ {:id table-id-4
+ :db_id db-id
+ :schema "public"
+ :data_source (if upload? :upload :ingested)}
+ [{:group-id group-id
+ :perm-type :perms/view-data
+ :default-value :unrestricted}]))
+ (t2/select-one-fn :perm_value :model/DataPermissions
+ :group_id group-id :db_id db-id
+ :table_id table-id-4 :perm_type :perms/view-data))))
+
+(deftest set-default-table-permissions!-view-data-granted-schema-test
+ (testing "Sync (default): a new table fails safe to :blocked because the group has only partial DB access"
+ (is (= :blocked (new-table-view-data-perm! {:upload? false :sandboxed? false}))))
+ (testing "Upload (`:data_source :upload`): a new table inherits the granted schema's :unrestricted (UXW-3217)"
+ (is (= :unrestricted (new-table-view-data-perm! {:upload? true :sandboxed? false}))))
+ (testing "Upload + sandbox: a sandbox still forces :blocked, schema be damned"
+ (is (= :blocked (new-table-view-data-perm! {:upload? true :sandboxed? true})))))
+
(defn- distinct-schema-vals-count
"Count the *distinct* (group, perm-type, schema, value) tuples for `db-id` —
the bounded result set the fixed [[load-perm-context]] loads."