The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GHY-4323: related-tables lookup does a permission query per table and an uncapped IN (#82962)
Eric Normand9db952d5680c
diff --git a/src/metabase/mcp/v2/tools/browse.clj b/src/metabase/mcp/v2/tools/browse.cljindex c2e0964bf6a..ac9ee001d02 100644--- a/src/metabase/mcp/v2/tools/browse.clj+++ b/src/metabase/mcp/v2/tools/browse.clj@@ -292,7 +292,11 @@[id (into [](comp (keep #(get-in % [:target :table_id]))(remove #{id})- (distinct))+ (distinct)+ ;; Cap before the lookup so its `IN` stays bounded. The+ ;; `:target` hydration already dropped unreadable targets,+ ;; so only inactive or unbrowsable ones can shrink the list.+ (take max-related-tables))fields)]))metadata-rows)related-ids (into #{} (mapcat val) targets-by-table)@@ -314,7 +318,6 @@(fn [target-ids](into [](comp (keep related)- (take max-related-tables)(map-indexed(fn [i {:keys [id name display_name schema]}](cond-> {:id id :name name :display_name display_name :schema schema}diff --git a/test/metabase/mcp/v2/api_test.clj b/test/metabase/mcp/v2/api_test.cljindex 641c492ec4b..1225fdac0a7 100644--- a/test/metabase/mcp/v2/api_test.clj+++ b/test/metabase/mcp/v2/api_test.clj@@ -4,6 +4,7 @@[clojure.test :refer :all][metabase.ai-tracing.core :as ait][metabase.auth-identity.core :as auth-identity]+ [metabase.mcp.db :as mcp.db][metabase.mcp.paths :as mcp.paths][metabase.mcp.session :as mcp.session][metabase.mcp.settings :as mcp.settings]@@ -14,6 +15,8 @@[metabase.mcp.v2.test-util :as v2.tu][metabase.metabot.scope :as metabot.scope][metabase.oauth-server.test-util :as oauth-server.tu]+ [metabase.permissions.models.data-permissions :as data-perms]+ [metabase.permissions.models.permissions-group :as perms-group][metabase.server.middleware.session :as mw.session][metabase.test :as mt][metabase.test.data.users :as test.users]@@ -205,6 +208,58 @@:sql (str "SELECT '" (str/replace planted-text "'" "''") "' AS X")})](check-planted-text-stays-data text #(ffirst (:rows %))))))))))+(defn- tool-call-as+ "Initialize a session as `user` and call `tool-name` with `arguments`; returns the tools/call response."+ [user tool-name arguments]+ (let [request #(client/client-full-response (test.users/username->token user)+ :post endpoint+ {:request-options {:headers %2}}+ %1)+ session-id (get-in (request (jsonrpc-request "initialize" {:capabilities {}}) {})+ [:headers "Mcp-Session-Id"])]+ (request (jsonrpc-request "tools/call" {:name tool-name :arguments arguments})+ {"mcp-session-id" session-id})))++(deftest get-fields-related-tables-scale-test+ (testing "GHY-4323: a table with more FK targets than get_fields surfaces as related tables, requested by a+ non-admin whose query permission is granted per table, so every related table's read check reaches the+ table-level permission cache"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {table-id :id} {:db_id db-id :schema "public" :name "hub" :active true}]+ (let [target-ids (t2/insert-returning-pks! :model/Table+ (for [i (range 60)]+ {:db_id db-id :schema "public" :active true+ :name (str "spoke_" i) :display_name (str "spoke_" i)}))]+ (t2/insert! :model/Field+ (for [[i target-id] (map-indexed vector target-ids)]+ {:table_id table-id :name (str "spoke_" i "_id") :active true+ :base_type :type/Integer :database_type "INT" :semantic_type :type/FK+ :fk_target_field_id (t2/insert-returning-pk! :model/Field+ {:table_id target-id :name "id" :active true+ :base_type :type/Integer :database_type "INT"+ :semantic_type :type/PK})}))+ (mt/with-no-data-perms-for-all-users!+ (data-perms/set-database-permission! (perms-group/all-users) db-id :perms/view-data :unrestricted)+ (doseq [id (cons table-id target-ids)]+ (data-perms/set-table-permission! (perms-group/all-users) id :perms/create-queries :query-builder))+ (let [looked-up (atom [])+ response (let [active-tables-by-ids (mt/original-fn #'mcp.db/active-tables-by-ids)]+ (mt/with-dynamic-fn-redefs [mcp.db/active-tables-by-ids+ (fn [ids]+ (swap! looked-up conj (count ids))+ (active-tables-by-ids ids))]+ (tool-call-as :rasta "browse_data" {:action "get_fields" :table_ids [table-id]})))+ result (get-in response [:body :result])]+ (testing "the call succeeds — the related tables' read checks don't run one permission query each"+ (is (= 200 (:status response)))+ (is (some? result) (pr-str (:body response)))+ (is (not (:isError result)) (pr-str result)))+ (testing "the related tables are capped"+ (let [[_ json] (v2.tu/data-parts (-> result :content first :text))]+ (is (= 50 (-> (json/decode+kw json) :tables first :related_tables count)))))+ (testing "the related-table lookup asks for no more ids than it can surface"+ (is (= [50] @looked-up)))))))))+(deftest initialize-instructions-explain-scope-failures-test(testing "GHY-4543: clients replace a scope denial with their own text (Claude Code: \"requires re-authorization(token expired)\", Codex: \"Insufficient scope\", mcp-remote: \"Tool execution failed\"), so the modeldiff --git a/test/metabase/mcp/v2/tools/browse_test.clj b/test/metabase/mcp/v2/tools/browse_test.cljindex 3e77e4fad73..7c27ea1c9f7 100644--- a/test/metabase/mcp/v2/tools/browse_test.clj+++ b/test/metabase/mcp/v2/tools/browse_test.clj@@ -19,6 +19,7 @@[clojure.string :as str][clojure.test :refer :all][metabase.collections.models.collection :as collection]+ [metabase.mcp.db :as mcp.db][metabase.mcp.v2.message :as message][metabase.mcp.v2.projections :as projections][metabase.mcp.v2.registry :as registry]@@ -491,6 +492,29 @@#"\"offset\" with get_fields pages the fields of one large table"(#'tools.browse/get-fields {:action "get_fields" :table_ids [1 2] :offset 5})))))+(deftest related-tables-cap-before-lookup-test+ (testing "GHY-4323: each requested table's FK targets are capped before the table lookup, so its `IN` is bounded+ by what the response can surface rather than by how many FK columns the tables have"+ (mt/with-temp [:model/Database {db-id :id} {}]+ (let [target-ids (t2/insert-returning-pks! :model/Table+ (for [i (range 60)]+ {:db_id db-id :schema "public" :active true+ :name (str "spoke_" i) :display_name (str "spoke_" i)}))+ rows [{:id -1+ :fields (for [id target-ids] {:target {:table_id id}})}]+ looked-up (atom [])+ related (let [active-tables-by-ids (mt/original-fn #'mcp.db/active-tables-by-ids)]+ (mt/with-dynamic-fn-redefs [mcp.db/active-tables-by-ids+ (fn [ids]+ (swap! looked-up conj (set ids))+ (active-tables-by-ids ids))]+ (mt/with-test-user :crowberto+ (#'tools.browse/related-tables-by-requested-table #{db-id} rows))))]+ (is (= [(set (take 50 target-ids))] @looked-up)+ "the lookup asks for the first 50 targets, in field order, and no more")+ (is (= (take 50 target-ids) (map :id (get related -1)))+ "the surfaced related tables are those same 50, in field order")))))+(deftest get-fields-dedups-table-ids-test(testing "GHY-4138: duplicate table_ids are deduped before the guards run — the same id twice isone table, so it does not trip the single-table `offset` rule or inflate the 20-id cap"