Skip to content

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

Reject metric URIs that refer to saved questions or models (#84897)

Andrei Simionescu624aa443a730
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.clj
index 6955b696e0c..1d7274802af 100644
--- a/src/metabase/metabot/tools/resources.clj
+++ b/src/metabase/metabot/tools/resources.clj
@@ -206,6 +206,17 @@
:text (str "Call `search` and build the URI from a result's numeric `id`"
" attribute, or copy its `uri` attribute as-is.")}]))})
+(tools/defrecoverable not-a-metric!
+ "Report a metric URI that refers to a question or model."
+ {:payload [:map {:closed true}
+ [:id :int]
+ [:type [:enum "question" "model"]]
+ [:card-uri :string]]}
+ [{:keys [id type card-uri]}]
+ {:message (str "Card " id " is a " type ", not a metric.")
+ :recovery [{:uses #{}
+ :text (str "Read `" card-uri "` instead.")}]})
+
(tools/defrecoverable invalid-page!
"A `?page=N` outside the list's range."
{:payload [:map {:closed true}
@@ -589,11 +600,14 @@
(check-resource-database (:db_id table))))
(defn check-card-resource-database
- "Require that `card-id`'s (model/question/metric) backing database is addressable as a Metabot
- resource (see [[check-resource-database]]). Exported for [[metabase.metabot.tools.metadata]]."
+ "Require that `card-id`'s backing database is addressable as a Metabot resource, and return the card.
+
+ `card-id` names a model, question or metric; see [[check-resource-database]]. Exported for
+ [[metabase.metabot.tools.metadata]]."
[card-id]
(when-let [card (api/read-check :model/Card card-id)]
- (check-resource-database (:database_id card))))
+ (check-resource-database (:database_id card))
+ card))
(defn- check-measure-or-segment-resource-database [model id]
(when-let [table-id (case model
@@ -674,23 +688,34 @@
;; ----- Metric -----
+(defn- check-metric-resource
+ "Require that `metric-id` is a metric whose backing database is addressable as a Metabot resource.
+
+ The error for a question or model includes its resource URI with `path-segments` appended."
+ [metric-id & path-segments]
+ (let [{card-type :type} (check-card-resource-database metric-id)]
+ (when-not (= :metric card-type)
+ (not-a-metric! {:id metric-id
+ :type (name card-type)
+ :card-uri (apply llm-shape/metabase-uri card-type metric-id path-segments)}))))
+
(defn- fetch-metric [id-str]
(let [metric-id (parse-long id-str)]
- (check-card-resource-database metric-id)
+ (check-metric-resource metric-id)
(entity-details/get-metric-details {:metric-id metric-id
:with-queryable-dimensions? false
:with-field-values? false})))
(defn- fetch-metric-dimensions [id-str]
(let [metric-id (parse-long id-str)]
- (check-card-resource-database metric-id)
+ (check-metric-resource metric-id "fields")
(entity-details/get-metric-details {:metric-id metric-id
:with-queryable-dimensions? true
:with-field-values? false})))
(defn- fetch-metric-dimension [id-str dim-id]
(let [metric-id (parse-long id-str)]
- (check-card-resource-database metric-id)
+ (check-metric-resource metric-id "fields" dim-id)
(field-stats/field-values {:entity-type "metric"
:entity-id metric-id
:field-id dim-id
diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.clj
index ca30efea668..7deb7901854 100644
--- a/src/metabase/metabot/tools/util.clj
+++ b/src/metabase/metabot/tools/util.clj
@@ -190,9 +190,14 @@
(#{:question} (:type card)) (get :dataset-query))))))
(defn metric-query
- "Return a query based on the metric with ID `metric-id`."
+ "Return a query based on the metric with ID `metric-id`.
+
+ Throws an agent error that names the card's type when the card is a question or model."
[metric-id]
(when-let [card (get-card metric-id)]
+ (when-not (= :metric (:type card))
+ (throw (ex-info (str "Card " metric-id " is a " (name (:type card)) ", not a metric.")
+ {:agent-error? true :status-code 400})))
(let [mp (lib-be/application-database-metadata-provider (:database_id card))]
(lib/query mp (lib.metadata/metric mp metric-id)))))
diff --git a/test/metabase/metabot/tools/field_stats_test.clj b/test/metabase/metabot/tools/field_stats_test.clj
index 8f0066a24e9..16e6824a74e 100644
--- a/test/metabase/metabot/tools/field_stats_test.clj
+++ b/test/metabase/metabot/tools/field_stats_test.clj
@@ -2,6 +2,8 @@
{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase.metabot.tools.field-stats-test]}}}}}}
(:require
[clojure.test :refer :all]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.metabot.metadata-perms :as metabot.perms]
[metabase.metabot.tools.field-stats :as metabot.tools.field-stats]
[metabase.test :as mt]
@@ -157,6 +159,22 @@
(metabot.tools.field-stats/field-values
{:entity-type "metric", :entity-id metric-id, :field-id birth-date-id, :limit 5}))))))))
+(deftest field-values-of-a-card-that-is-not-a-metric-test
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
+ metric-values #(metabot.tools.field-stats/field-values
+ {:entity-type "metric", :entity-id %, :field-id (mt/id :orders :quantity), :limit 5})]
+ (mt/with-temp [:model/Card {question-id :id} {:type :question :dataset_query query}
+ :model/Card {model-id :id} {:type :model :dataset_query query}]
+ (mt/as-admin
+ (testing "reading a question or model as a metric names the card's type"
+ (doseq [[card-type card-id] [["question" question-id] ["model" model-id]]]
+ (is (= {:output (str "Card " card-id " is a " card-type ", not a metric.") :status-code 400}
+ (metric-values card-id)))))
+ (testing "a metric that doesn't exist is refused as not found"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Not found\."
+ (metric-values Integer/MAX_VALUE))))))))
+
(deftest field-values-unknown-entity-type-test
(testing "an unrecognized entity-type -- e.g. because the schema that constrains it isn't enforced in
production -- returns a graceful agent-facing message instead of throwing"
diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.clj
index 3d471889ac8..ffee1a92dc6 100644
--- a/test/metabase/metabot/tools/resources_test.clj
+++ b/test/metabase/metabot/tools/resources_test.clj
@@ -1086,6 +1086,32 @@
{:uris [(str "metabase://question/" q-from-model-id "/sources")]})]
(is (str/includes? output (str "uri=\"metabase://model/" model-id "\"")))))))))
+(deftest metric-uris-refuse-a-question-or-a-model-test
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
+ field-id (mt/id :orders :quantity)]
+ (mt/with-temp [:model/Card {question-id :id} {:type :question :dataset_query query}
+ :model/Card {model-id :id} {:type :model :dataset_query query}]
+ (testing "the error identifies the card type and gives a resource URI that can be read"
+ (doseq [[card-type card-id] [["question" question-id] ["model" model-id]]
+ [metric-path card-path] [["" ""]
+ ["/dimensions" "/fields"]
+ [(str "/dimensions/" field-id) (str "/fields/" field-id)]]
+ :let [card-uri (str "metabase://" card-type "/" card-id card-path)]]
+ (testing (str "metabase://metric/" card-id metric-path)
+ (is (= (str "Card " card-id " is a " card-type ", not a metric.\n"
+ "Read `" card-uri "` instead.")
+ (-> (read-uris {:uris [(str "metabase://metric/" card-id metric-path)]})
+ :resources first :error)))
+ (is (not (error? (read-uris {:uris [card-uri]})))))))
+ (testing "reading only an invalid metric URI marks the tool call as failed"
+ (is (=? {:class :recoverable
+ :code :metabase.metabot.tools.core/all-items-failed}
+ (test-util/tool-failure read-resource/read-resource-tool
+ {:uris [(str "metabase://metric/" question-id)]}
+ (:tool-names read-ctx)))))))))
+
(defn- measure-definition
"An MBQL5 measure definition: the sum of `field-id` over `table-id`."
[table-id field-id]