Skip to content

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

Fix per-table permission queries in db metadata endpoint (#81397)

Timofey Kachalov01add7e6e2db
diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.clj
index 38f0914cd74..5cec6916b06 100644
--- a/src/metabase/warehouses_rest/api.clj
+++ b/src/metabase/warehouses_rest/api.clj
@@ -635,6 +635,7 @@
(if skip-fields?
[:tables :segments :metrics]
[:tables [:fields :has_field_values [:target :has_field_values]] :segments :metrics])))
+ _ (perms/prime-table-perms-cache {:db-ids #{id}})
db (if include-editable-data-model?
;; We need to check data model perms after hydrating tables, since this will also filter out tables for
;; which the *current-user* does not have data model perms
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index 0ae3d70422f..7a56771c15e 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -929,6 +929,21 @@
(let [resp (mt/derecordize (mt/user-http-request :rasta :get 200 (format "database/%d/metadata" (mt/id))))]
(assoc resp :tables (filter #(= "CATEGORIES" (:name %)) (:tables resp))))))))
+(deftest fetch-database-metadata-primes-table-perms-cache-test
+ (testing "GET /api/database/:id/metadata primes the table-perms cache before its per-table read checks"
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table _ {:db_id db-id :schema "public" :name "t1"}
+ :model/Table _ {:db_id db-id :schema "public" :name "t2"}
+ :model/Table _ {:db_id db-id :schema "public" :name "t3"}
+ :model/Table _ {:db_id db-id :schema "public" :name "t4"}
+ :model/Table _ {:db_id db-id :schema "public" :name "t5"}
+ :model/Table _ {:db_id db-id :schema "public" :name "t6"}]
+ (data-perms/set-database-permission! (perms-group/all-users) db-id :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! (perms-group/all-users) db-id :perms/create-queries :query-builder)
+ (let [tables (:tables (mt/user-http-request :rasta :get 200 (format "database/%d/metadata" db-id)))]
+ (is (= #{"t1" "t2" "t3" "t4" "t5" "t6"} (set (map :name tables)))
+ "every table is returned to a non-admin user with table-granular perms, without tripping the backstop")))))
+
(deftest ^:parallel fetch-database-fields-test
(letfn [(f [fields] (m/index-by #(str (:table_name %) "." (:name %)) fields))]
(testing "GET /api/database/:id/fields"