Skip to content

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

Do not allow publishing tables without query perms (#69171)

John Swanson78d36e43e484
diff --git a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
index ce1b645e336..3c83a7e65f3 100644
--- a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
@@ -8,6 +8,7 @@
[metabase.api.routes.common :refer [+auth]]
[metabase.collections.core :as collection]
[metabase.events.core :as events]
+ [metabase.models.interface :as mi]
[metabase.util.i18n :refer [tru]]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
@@ -113,6 +114,19 @@
[:map
[:target_collection [:maybe (ms/InstanceOf :model/Collection)]]])
+(defn- can-publish?
+ "Publishing a table means that it's now query-able by a new set of people. So we should not allow you to publish a
+ table if you don't *already* have permissions to query it - otherwise, maybe you can just publish it to circumvent your
+ lack of query permissions."
+ [table]
+ (and (mi/can-write? table) (mi/can-query? table)))
+
+(defn- can-publish-all-tables?
+ "This function returns `true` iff you have permission to publish every table passed."
+ [table-ids]
+ (every? can-publish? (when (seq table-ids)
+ (t2/select :model/Table :id [:in table-ids]))))
+
(api.macros/defendpoint :post "/publish-tables" :- ::publish-tables-response
"Set collection for each of selected tables and all upstream dependencies recursively."
[_route-params
@@ -133,6 +147,7 @@
where)
;; Get table IDs before update for event publishing
table-ids-to-update (t2/select-pks-set :model/Table {:where update-where})]
+ (api/check-403 (can-publish-all-tables? table-ids-to-update))
(t2/query {:update (t2/table-name :model/Table)
:set {:collection_id (:id target-collection)
:is_published true}
@@ -158,6 +173,7 @@
where)
;; Get table IDs before update for event publishing
table-ids-to-update (t2/select-pks-set :model/Table {:where update-where})]
+ (api/check-403 (can-publish-all-tables? table-ids-to-update))
(t2/query {:update (t2/table-name :model/Table)
:set {:collection_id nil
:is_published false}
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
index 83824af542f..2a3df43f3ce 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
@@ -4,6 +4,8 @@
[clojure.test :refer :all]
[metabase.collections.models.collection :as collection]
[metabase.collections.test-utils :refer [without-library]]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.data-permissions :as data-perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
@@ -74,7 +76,7 @@
{:table_ids [(mt/id :users)]}))))))
(deftest data-analyst-can-access-endpoints-test
- (mt/with-premium-features #{:library}
+ (mt/with-premium-features #{:library :advanced-permissions}
(testing "Data analysts (members of Data Analysts group) can access library endpoints"
(let [data-analyst-group-id (:id (perms-group/data-analyst))]
(mt/with-temp [:model/User {analyst-id :id} {:first_name "Data"
@@ -85,6 +87,8 @@
:model/Database {db-id :id} {}
:model/Table {table-id :id} {:db_id db-id}
:model/Collection _ {:type collection/library-data-collection-type}]
+ ;; Grant data analyst group view-data permission on this database
+ (data-perms/set-database-permission! data-analyst-group-id db-id :perms/view-data :unrestricted)
(testing "data analyst can publish tables"
(is (map? (mt/user-http-request analyst-id :post 200 "ee/data-studio/table/publish-tables"
{:table_ids [table-id]}))))
@@ -246,3 +250,46 @@
{:table_ids [customers-id]})
(are [table-id] (false? (t2/select-one-fn :is_published :model/Table table-id))
customers-id orders-id items-id))))))
+
+;;; ------------------------------------------ Publish/Unpublish requires write and query perms ------------------------------------------
+
+(deftest publish-tables-requires-write-and-query-perms-test
+ (mt/with-premium-features #{:library :advanced-permissions}
+ (testing "POST /api/ee/data-studio/table/publish-tables requires write and query permission on all tables"
+ (mt/with-temp [:model/Collection _ {:type collection/library-data-collection-type}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id :name "restricted_table"}
+ :model/User {analyst-id :id} {:first_name "Data"
+ :last_name "Analyst"
+ :is_data_analyst true}
+ :model/PermissionsGroupMembership _ {:user_id analyst-id
+ :group_id (:id (perms-group/data-analyst))}]
+ (mt/with-no-data-perms-for-all-users!
+ (data-perms/set-database-permission! (perms-group/data-analyst) db-id :perms/view-data :blocked)
+ (data-perms/set-database-permission! (perms-group/data-analyst) db-id :perms/create-queries :no)
+ ;; Analyst has data analyst role but no data perms on the table
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request analyst-id :post 403 "ee/data-studio/table/publish-tables"
+ {:table_ids [table-id]}))))))))
+
+(deftest unpublish-tables-requires-write-and-query-perms-test
+ (mt/with-premium-features #{:library :advanced-permissions}
+ (testing "POST /api/ee/data-studio/table/unpublish-tables requires write and query permission on all tables"
+ (mt/with-temp [:model/Collection {coll-id :id} {:type collection/library-data-collection-type}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id :name "restricted_table"
+ :is_published true :collection_id coll-id}
+ :model/User {analyst-id :id} {:first_name "Data"
+ :last_name "Analyst"
+ :is_data_analyst true}
+ :model/PermissionsGroupMembership _ {:user_id analyst-id
+ :group_id (:id (perms-group/data-analyst))}]
+ (mt/with-no-data-perms-for-all-users!
+ (perms/revoke-collection-permissions! (perms-group/all-users) coll-id)
+ (perms/revoke-collection-permissions! (perms-group/data-analyst) coll-id)
+ ;; Analyst has data analyst role but no data perms on the table
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request analyst-id :post 403 "ee/data-studio/table/unpublish-tables"
+ {:table_ids [table-id]}))))))))