Skip to content

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

Don't feed Metabot queries the user has no permission to run (#80083)

Andrei Simionescu4c5146d536fb
diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.clj
index 19b40718a63..ff194f23743 100644
--- a/src/metabase/metabot/agent/core.clj
+++ b/src/metabase/metabot/agent/core.clj
@@ -437,9 +437,9 @@
(defn- client-content-ids
"Ids of the queries and charts this request's viewing context seeds, as opposed to ones the
agent's own tools wrote. A refusal to present one of these is a real access attempt and gets
- the audited treatment; see [[metabase.metabot.tools.shared.content-store]]. Seeding a fresh
- map keeps this to the context of the turn being served, which is where the distinction comes
- from - the conversation's `:state` carries no provenance."
+ the audited treatment; see [[metabase.metabot.tools.shared.content-store]]. Seeding a fresh map
+ keeps this to what the client sent this turn; [[metabase.metabot.agent.memory/add-client-ids]]
+ adds it to what earlier turns recorded."
[context]
(let [seeded (-> {} (seed-state context) (seed-charts context))]
(into (set (keys (:queries seeded)))
@@ -482,9 +482,9 @@
(seed-state context)
(seed-chart-configs context)
(seed-charts context))
- memory (assoc (memory/initialize messages seeded context)
- :conversation-id conversation-id
- :client-ids (client-content-ids context))
+ memory (-> (memory/initialize messages seeded context)
+ (assoc :conversation-id conversation-id)
+ (memory/add-client-ids (client-content-ids context)))
memory-atom (doto (or external-memory-atom (atom nil)) (reset! memory))
tools (tools/wrap-tools-with-state base-tools memory-atom metabot-id profile-id)]
(log/info "Starting agent" {:profile profile-id
diff --git a/src/metabase/metabot/agent/memory.clj b/src/metabase/metabot/agent/memory.clj
index 1234ac6c260..7f4985c48a7 100644
--- a/src/metabase/metabot/agent/memory.clj
+++ b/src/metabase/metabot/agent/memory.clj
@@ -116,6 +116,14 @@
[memory todos]
(record memory [:todos] (vec todos)))
+(defn add-client-ids
+ "Add `ids` to the queries and charts this conversation knows the client supplied.
+ Earlier turns' ids are kept. A tool can persist a client-supplied query and a later turn read
+ it back long after it left the viewing context, so the provenance has to live as long as the
+ query does."
+ [memory ids]
+ (record memory [:client-ids] (into (set (get-in memory [:state :client-ids])) ids)))
+
(defn set-link-registry
"Set the link registry (url → stable id mappings)."
[memory link-registry]
diff --git a/src/metabase/metabot/agent/user_context.clj b/src/metabase/metabot/agent/user_context.clj
index 78fc87384f0..fa9af7df856 100644
--- a/src/metabase/metabot/agent/user_context.clj
+++ b/src/metabase/metabot/agent/user_context.clj
@@ -7,6 +7,7 @@
[clojure.string :as str]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
+ [metabase.metabot.query-export :as query-export]
[metabase.metabot.tmpl :as te]
[metabase.metabot.tools.entity-details :as entity-details]
[metabase.metabot.tools.resources :as resources-tools]
@@ -253,8 +254,8 @@
database and query the tables it references. The database refusal is audited for the same
reason the query's card ids get the audited store: the id is the caller's own."
[query]
- (when-let [[gated mp] (shared.content-store/query-for-export query true)]
- (llm-shape/export-query-for-llm gated mp shared.content-store/audited-store)))
+ (some-> (shared.content-store/query-for-export query true)
+ (query-export/export->text shared.content-store/audited-store)))
;; Format adhoc query (notebook editor) viewing context.
(defmethod format-entity "adhoc"
diff --git a/src/metabase/metabot/query_export.clj b/src/metabase/metabot/query_export.clj
new file mode 100644
index 00000000000..4e4f5eae000
--- /dev/null
+++ b/src/metabase/metabot/query_export.clj
@@ -0,0 +1,58 @@
+(ns metabase.metabot.query-export
+ "Whether a query Metabot is about to put in front of the model may be shown at all, and in what
+ form. Rendering one resolves table and field ids to names through an unfiltered metadata
+ provider, so the permission check in [[metabase.metabot.tools.shared.content-store]] runs first
+ and this namespace turns its answer into the text the caller renders."
+ (:require
+ [metabase.metabot.tools.shared.content-store :as shared.content-store]
+ [metabase.metabot.tools.shared.llm-shape :as llm-shape]
+ [metabase.metabot.util :as metabot.u]))
+
+(set! *warn-on-reflection* true)
+
+(defn- native-query?
+ "Whether `query` has a native stage anywhere in it, in either the MBQL 5 or the legacy shape.
+ [[metabase.metabot.util/extract-sql-content]] is no substitute: it returns nil for a multi-stage
+ query on purpose, so a caller can't mistake stage 0's SQL for the whole thing."
+ [query]
+ (boolean
+ (some (fn [node]
+ (and (map? node)
+ (or (= :mbql.stage/native (:lib/type node))
+ (= "mbql.stage/native" (get node "lib/type"))
+ (contains? node :native)
+ (contains? node "native"))))
+ (tree-seq coll? seq query))))
+
+(defn export->text
+ "Render what [[shared.content-store/query-for-export]] returned, through `store`.
+ Nil when the permission check could not be made and the query is native: a structured query still
+ prints then, resolving nothing, but native SQL spells its table and column names out in the text."
+ [{:keys [query mp unchecked?]} store]
+ (when-not (and unchecked? (native-query? query))
+ (llm-shape/export-gated-query-for-llm query mp store)))
+
+(defn- source-query-text
+ "A transform's stored source query as the model should see it, or nil when it must not be shown
+ at all. Rendering happens here so it uses the query and provider the permission check already
+ produced, rather than normalizing and resolving a second time."
+ [query]
+ (when-let [{:keys [query mp unchecked?] :as export} (shared.content-store/query-for-export query false)]
+ (cond
+ ;; A cleared query renders the way it always has: native SQL verbatim, anything else through
+ ;; the provider the check already built.
+ mp (or (metabot.u/extract-sql-content query)
+ (llm-shape/export-gated-query-for-llm query mp shared.content-store/default-store))
+ unchecked? (export->text export shared.content-store/default-store)
+ ;; Nothing to check: no database, or the database is gone and its metadata with it. An
+ ;; orphaned transform still shows its source, which is what admins repair it from.
+ :else (llm-shape/transform-query->text query))))
+
+(defn transform-with-exportable-source
+ "Render a transform's stored source query for the model, or withhold it.
+ The query is withheld when the current user may not run it, or when the permission check could
+ not be made and the query is native. The rest of the transform stays readable either way."
+ [transform]
+ (if-let [text (source-query-text (get-in transform [:source :query]))]
+ (assoc-in transform [:source :query] text)
+ (update transform :source dissoc :query)))
diff --git a/src/metabase/metabot/schema.clj b/src/metabase/metabot/schema.clj
index 656403b29ad..315ba64d2e1 100644
--- a/src/metabase/metabot/schema.clj
+++ b/src/metabase/metabot/schema.clj
@@ -149,7 +149,12 @@
[:chart-configs {:optional true} [:map-of ::state-map-key ::chart-config]]
[:todos {:optional true} [:sequential ::todo]]
[:transforms {:optional true} [:map-of ::state-map-key ::transform]]
- [:link-registry {:optional true} [:map-of ::state-map-key :string]]])
+ [:link-registry {:optional true} [:map-of ::state-map-key :string]]
+ ;; JSON has no sets, so a state read back out of metabot_message arrives as a vector here and
+ ;; is validated before [[normalize-state]] gets a chance to turn it back into one
+ [:client-ids {:optional true} [:or {:decode/normalize set}
+ [:set ::state-map-key]
+ [:sequential ::state-map-key]]]])
(defn normalize-state
"Normalize dynamic state-map keys to strings according to [[::state]]."
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.clj
index 02e35164d3c..149eaf1eb12 100644
--- a/src/metabase/metabot/tools/resources.clj
+++ b/src/metabase/metabot/tools/resources.clj
@@ -72,6 +72,7 @@
[metabase.documents.prose-mirror :as prose-mirror]
[metabase.metabot.agent.streaming :as streaming]
[metabase.metabot.db :as metabot.db]
+ [metabase.metabot.query-export :as query-export]
[metabase.metabot.scope :as scope]
[metabase.metabot.tmpl :as te]
[metabase.metabot.tools.entity-details :as entity-details]
@@ -582,6 +583,7 @@
(defn- fetch-transform [id-str]
{:structured-output (-> (transforms/get-transform (parse-long id-str))
+ query-export/transform-with-exportable-source
(assoc :result-type :entity :type :transform))})
(defn- fetch-transform-sources [id-str]
@@ -734,10 +736,10 @@
is routine presentation and stays quiet."
[query-id query]
(let [audited? (contains? (shared/current-client-ids) query-id)]
- (when-let [[gated mp] (shared.content-store/query-for-export query audited?)]
- (llm-shape/export-query-for-llm gated mp (if audited?
- shared.content-store/audited-store
- shared.content-store/default-store)))))
+ (some-> (shared.content-store/query-for-export query audited?)
+ (query-export/export->text (if audited?
+ shared.content-store/audited-store
+ shared.content-store/default-store)))))
(defn- fetch-conversation-query
"Present a query stored in this conversation's agent state (created by tools or pasted
diff --git a/src/metabase/metabot/tools/shared.clj b/src/metabase/metabot/tools/shared.clj
index 8ba0ea306a9..5861be4478e 100644
--- a/src/metabase/metabot/tools/shared.clj
+++ b/src/metabase/metabot/tools/shared.clj
@@ -53,10 +53,11 @@
(defn current-client-ids
"Ids of the queries and charts seeded from this request's viewing context, as opposed to
written by the agent's own tools. A refusal to present one of these is a real access attempt
- and gets the audited treatment; see `metabase.metabot.tools.shared.content-store`. Request
- local: the conversation `:state` the client round-trips carries no provenance."
+ and gets the audited treatment; see `metabase.metabot.tools.shared.content-store`. Kept in state
+ rather than per request, since a tool can persist one of these queries and a later turn read it
+ back after it has left the viewing context."
[]
- (get (current-memory) :client-ids #{}))
+ (get-in (current-memory) [:state :client-ids] #{}))
(defn current-chart-configs-state
"Returns the current chart-configs state map from agent memory.
diff --git a/src/metabase/metabot/tools/shared/content_store.clj b/src/metabase/metabot/tools/shared/content_store.clj
index 747b3b59344..c87406ea650 100644
--- a/src/metabase/metabot/tools/shared/content_store.clj
+++ b/src/metabase/metabot/tools/shared/content_store.clj
@@ -185,16 +185,21 @@
refuses them without a trail. Measure / segment refs are not checked here; the stores gate
those with the caller's audit polarity.
- Nil when they may not. Normalizing is the first thing the check does, so the result comes back
- rather than leaving the export to repeat it."
+ Nil when they may not, and `::unchecked` when the check could not be made at all - a query that
+ will not normalize, permissions that will not calculate. That is not a denial, so the caller
+ still renders the query, just without resolving anything in it. Normalizing is the first thing
+ the check does, so the result comes back rather than leaving the export to repeat it."
[audited? resolved]
(try
- (let [normalized (lib-be/normalize-query resolved)
+ ;; strict, or a query that will not parse degrades to {} and reads as a denial instead of
+ ;; reaching the catch below (it also logs at ERROR on the way past)
+ (let [normalized (lib-be/normalize-query nil resolved {:strict? true})
database-id (:database normalized)
{:keys [table card field]} (exported-entity-ids normalized)]
(when (and (pos-int? database-id)
(every? #(readable? audited? :model/Card %) card)
- ;; throw on a calculation failure so only a denial reads as false
+ ;; throw on a calculation failure, so it lands in the catch below as a check we
+ ;; could not make rather than reading as a denial
(query-perms/can-run-query? normalized false true))
(let [field-table (metabot.perms/field-id->table-id field)
table-ids (into (set table) (vals field-table))]
@@ -202,8 +207,9 @@
(sandbox-visible-fields? field-table))
normalized))))
(catch Exception e
- (log/debugf "Omitting a query that could not be permission-checked: %s" (ex-message e))
- nil)))
+ (log/debugf "Rendering a query unresolved, since it could not be permission-checked: %s"
+ (ex-message e))
+ ::unchecked)))
(defn- cached-pass
"Memoize an allowed gate result on the agent's memory for the rest of the turn: the check
@@ -220,22 +226,37 @@
(f)))
(defn query-for-export
- "`[query mp]` for [[metabase.metabot.tools.shared.llm-shape/export-query-for-llm]] when the
- current user may run `query`, else nil. The query comes back normalized with a provider over
- its database; one carrying no `:database` passes through untouched and without a provider,
- since it only ever pprints. With `audited?` the saved-question refusals are audited; for
- client-supplied queries, where the ids are the caller's own. Run permission is the whole rule:
- reading the database is not enough, and a saved question the user can read authorizes a query
- over a database they cannot. A database that no longer exists passes, since there is no
- metadata behind it to leak; one we can't resolve does not."
+ "How the caller should render `query`, or nil when the current user may not see it at all.
+ Run permission is the whole rule: reading the database is not enough, and a saved question the
+ user can read authorizes a query over a database they cannot. With `audited?` the saved-question
+ refusals are audited, for client-supplied queries where the ids are the caller's own.
+
+ Three shapes come back, because a check that was never needed and a check that could not be made
+ are not the same thing:
+
+ `{:query q :mp mp}` cleared. `q` is normalized and exports through `mp`, resolving its
+ ids to names.
+ `{:query q}` there was nothing to check. The query carries no `:database`, or its
+ database is gone and the metadata with it, so rendering it in full
+ reveals nothing. This is what master did.
+ `{:query q :unchecked? true}`
+ the check could not be made at all: a database id that will not
+ resolve, a query that will not parse, permissions that will not
+ calculate. Not a refusal, so the caller may still render the query,
+ but nothing in it may be resolved to a name."
[query audited?]
(if-not (and (map? query) (:database query))
- [query nil]
+ {:query query}
(cached-pass
[query audited?]
(fn []
- (when-let [resolved (resolve-effective-database query)]
+ (if-let [resolved (resolve-effective-database query)]
(if (metabot.db/database-exists? (:database resolved))
- (when-let [normalized (runnable-normalized-query audited? resolved)]
- [normalized (lib-be/application-database-metadata-provider (:database normalized))])
- [resolved nil]))))))
+ (let [normalized (runnable-normalized-query audited? resolved)]
+ (cond
+ (= ::unchecked normalized) {:query resolved :unchecked? true}
+ (some? normalized) {:query normalized
+ :mp (lib-be/application-database-metadata-provider
+ (:database normalized))}))
+ {:query resolved})
+ {:query query :unchecked? true})))))
diff --git a/src/metabase/metabot/tools/shared/llm_shape.clj b/src/metabase/metabot/tools/shared/llm_shape.clj
index 6ccd41ff3bf..fcdc3582089 100644
--- a/src/metabase/metabot/tools/shared/llm_shape.clj
+++ b/src/metabase/metabot/tools/shared/llm_shape.clj
@@ -82,48 +82,64 @@
[query]
(u/pprint-to-str (cond-> query (map? query) (dissoc :lib/metadata))))
-(defn export-query-for-llm
- "Render a `query` (legacy or MBQL 5 map, or a pre-resolved string) for the LLM. A query
- map with a `:database` is normalized and exported to the portable representations form
- the `construct_notebook_query` tool consumes (a JSON code block); pre-resolved string
- sources pass through; a `pprint`'d map is the last-resort fallback. A permission-refused
- export renders nothing at all rather than the fallback.
-
- `store` gates the Card / Measure / Segment lookups and every caller names one:
+(defn- exported-or-edn
+ "Export `normalized` through `mp`, falling back to EDN of `fallback-query` when the export fails
+ for a reason other than a refusal.
+
+ The refusal marker, not the status code, is what tells \"you may not read this\" from \"the export
+ failed\". A by-id denial collapses into the same not-found a missing id produces, on purpose, so
+ the agent cannot ask for hidden content and learn it exists, which means the 403 this used to
+ catch never arrives on that path. Without the marker a refused card falls through and the raw
+ query is printed."
+ [mp normalized store fallback-query]
+ (binding [shared.content-store/*last-lookup-refused?* (atom false)]
+ (try
+ (or (repr-data->llm-block (repr.resolve/export-query mp normalized store))
+ (query-edn-fallback normalized))
+ (catch Exception e
+ (when-not (or (= 403 (:status-code (ex-data e)))
+ @shared.content-store/*last-lookup-refused?*)
+ (log/debugf "Failed to export query for LLM, using EDN fallback: %s" (ex-message e))
+ (query-edn-fallback fallback-query))))))
+
+(defn export-gated-query-for-llm
+ "Render a query [[shared.content-store/query-for-export]] cleared, from the `:query` and `:mp`
+ it hands back. With `mp` the query is already normalized and exports through it, resolving its
+ ids to names. Without one there is nothing to resolve, so it renders only by the paths that
+ resolve nothing.
+
+ `store` decides the Card / Measure / Segment lookups and every caller names one:
[[shared.content-store/default-store]] for queries loaded from the app DB,
- [[shared.content-store/audited-store]] for client-supplied queries so a denied lookup
- keeps its audit trail. No defaulting arity, so the choice stays visible at the call site.
-
- Passing `mp` says `query` is already normalized and names the provider to export it through -
- what [[shared.content-store/query-for-export]] hands back, so the export doesn't
- repeat the normalization its permission check just did."
- ([query store]
- (export-query-for-llm query nil store))
- ([query mp store]
- (cond
- (string? query) query
- (string? (:query-content query)) (:query-content query)
- (and (map? query) (:database query))
- ;; The refusal marker, not the status code, is what distinguishes "you may not read this"
- ;; from "the export failed". A by-id denial collapses into the same not-found a missing id
- ;; produces — deliberately, so the agent cannot probe for hidden content — so the 403 this
- ;; used to catch does not arrive on that path. Without the marker a refused card would fall
- ;; through to the EDN fallback and print the raw query.
- (binding [shared.content-store/*last-lookup-refused?* (atom false)]
- (try
- (let [normalized (if mp query (lib-be/normalize-query query))
- mp (or mp (lib-be/application-database-metadata-provider (:database normalized)))
- exported (repr.resolve/export-query mp normalized store)]
- (or (repr-data->llm-block exported)
- (query-edn-fallback normalized)))
- (catch Exception e
- (when-not (or (= 403 (:status-code (ex-data e)))
- @shared.content-store/*last-lookup-refused?*)
- (log/debugf "Failed to export query for LLM, using EDN fallback: %s" (ex-message e))
- (query-edn-fallback query)))))
- (string? (get-in query [:native :query])) (get-in query [:native :query])
- (map? query) (query-edn-fallback query)
- :else (some-> query str))))
+ [[shared.content-store/audited-store]] for client-supplied queries so a denied lookup keeps its
+ audit trail. No defaulting arity, so the choice stays visible at the call site."
+ [query mp store]
+ (cond
+ (string? query) query
+ (string? (:query-content query)) (:query-content query)
+
+ mp (exported-or-edn mp query store query)
+
+ (string? (get-in query [:native :query])) (get-in query [:native :query])
+ (map? query) (query-edn-fallback query)
+ :else (some-> query str)))
+
+(defn export-query-for-llm
+ "Render a `query` (legacy or MBQL 5 map, or a pre-resolved string) for the LLM, normalizing it
+ and building its metadata provider here. For callers with no permission check in front of them;
+ one that has been through [[shared.content-store/query-for-export]] uses
+ [[export-gated-query-for-llm]] with what that returned instead of normalizing a second time."
+ [query store]
+ (if (and (map? query) (:database query))
+ (try
+ (let [normalized (lib-be/normalize-query query)
+ mp (lib-be/application-database-metadata-provider (:database normalized))]
+ ;; `query` rather than the normalized form, so a failure here renders what the caller
+ ;; handed us
+ (exported-or-edn mp normalized store query))
+ (catch Exception e
+ (log/debugf "Failed to normalize query for LLM, rendering it unresolved: %s" (ex-message e))
+ (export-gated-query-for-llm query nil store)))
+ (export-gated-query-for-llm query nil store)))
(defn transform-query->text
"Render a transform source query for model context: native SQL verbatim, anything else
diff --git a/src/metabase/metabot/tools/transforms.clj b/src/metabase/metabot/tools/transforms.clj
index 4e27f62df2a..8d6260a8c0b 100644
--- a/src/metabase/metabot/tools/transforms.clj
+++ b/src/metabase/metabot/tools/transforms.clj
@@ -2,6 +2,7 @@
"Tools for reading transform definitions."
(:require
[clojure.string :as str]
+ [metabase.metabot.query-export :as query-export]
[metabase.metabot.scope :as scope]
[metabase.metabot.tools.shared.llm-shape :as llm-shape]
[metabase.metabot.tools.util :as metabot.tools.u]
@@ -57,7 +58,8 @@
"Get information about a transform."
[{:keys [transform_id]} :- [:map {:closed true} [:transform_id :int]]]
(try
- (add-output {:structured_output (transforms/get-transform transform_id)}
+ (add-output {:structured_output (query-export/transform-with-exportable-source
+ (transforms/get-transform transform_id))}
format-transform-details-output)
(catch Exception e
(if (= 403 (:status-code (ex-data e)))
diff --git a/test/metabase/metabot/agent/memory_test.clj b/test/metabase/metabot/agent/memory_test.clj
index a8efe0db750..9b17bf6a15e 100644
--- a/test/metabase/metabot/agent/memory_test.clj
+++ b/test/metabase/metabot/agent/memory_test.clj
@@ -4,7 +4,9 @@
[clojure.test :refer :all]
[metabase.metabot.agent.links :as links]
[metabase.metabot.agent.memory :as memory]
- [metabase.metabot.tools.shared :as shared]))
+ [metabase.metabot.schema :as metabot.schema]
+ [metabase.metabot.tools.shared :as shared]
+ [metabase.util.malli.registry :as mr]))
(deftest ^:parallel initialize-test
(testing "seeds the working state and starts an empty turn-state"
@@ -92,6 +94,23 @@
(let [mem (memory/initialize [] {:queries {"q1" {:id "q1"}}})]
(is (nil? (memory/turn-state mem))))))
+(deftest ^:parallel client-ids-survive-the-app-db-round-trip-test
+ (testing "JSON has no sets, so state read back out of metabot_message carries a vector here. It
+ has to validate in that shape, since the agent entry point checks it before memory is
+ initialized, and it has to arrive as a set."
+ (let [persisted {:client-ids ["seeded-q"]}]
+ (is (mr/validate ::metabot.schema/state persisted))
+ (is (= #{"seeded-q"}
+ (get-in (memory/initialize [] persisted) [:state :client-ids]))))))
+
+(deftest ^:parallel add-client-ids-test
+ (testing "ids accumulate across turns and are written to the persisted delta, so a query a tool
+ stored still audits its refusals once it has left the viewing context"
+ (let [memory (-> (memory/initialize [] {:client-ids #{"from-an-earlier-turn"}})
+ (memory/add-client-ids #{"seeded-now"}))]
+ (is (= #{"from-an-earlier-turn" "seeded-now"} (get-in memory [:state :client-ids])))
+ (is (= #{"from-an-earlier-turn" "seeded-now"} (get-in memory [:turn-state :client-ids]))))))
+
(deftest ^:parallel set-link-registry-test
(testing "an empty or unchanged registry is not written into the turn delta"
(let [without-registry (memory/initialize [] {})
diff --git a/test/metabase/metabot/agent/user_context_test.clj b/test/metabase/metabot/agent/user_context_test.clj
index 6cf44bd215a..334ed94259b 100644
--- a/test/metabase/metabot/agent/user_context_test.clj
+++ b/test/metabase/metabot/agent/user_context_test.clj
@@ -6,12 +6,15 @@
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-metadata :as meta]
[metabase.metabot.agent.user-context :as user-context]
+ [metabase.metabot.test-util :as test-util]
[metabase.metabot.tools.entity-details :as entity-details]
[metabase.metabot.tools.resources :as resources-tools]
[metabase.metabot.tools.shared.content-store :as shared.content-store]
[metabase.metabot.tools.shared.llm-shape :as llm-shape]
[metabase.models.interface :as mi]
[metabase.models.serialization.resolve.mp :as resolve.mp]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[toucan2.core :as t2]))
@@ -482,6 +485,26 @@
(is (str/includes? result "1111")
"Formatting result should contain database id")))
+(deftest exportable-query-source-card-test
+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :venues)}}}]
+ (let [query {:database (mt/id)
+ :type :query
+ :query {:source-table (str "card__" card-id)}}]
+ (testing "a query sourced from a readable card is exportable even when its database is not queryable"
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues)
+ :perms/manage-table-metadata :yes)
+ (mt/with-test-user :rasta
+ (is (not (mi/can-query? :model/Database (mt/id))))
+ (is (some? (shared.content-store/query-for-export query false))))))
+ (testing "and not exportable when the card is not readable"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-test-user :rasta
+ (is (nil? (shared.content-store/query-for-export query false))))))))))
+
(deftest ^:parallel adhoc-viewing-context-includes-query-test
(testing "adhoc viewing context renders the query so the model can see the chart"
(let [out (user-context/format-viewing-context
@@ -491,24 +514,35 @@
(is (str/includes? out "notebook editor"))
(is (str/includes? out "source-table")))))
-(deftest adhoc-viewing-context-read-checks-database-test
- (let [viewing (fn [db-id]
- {:user_is_viewing [{:type "adhoc"
- :query {:database db-id
- :type "query"
- :query {:source-table (mt/id :venues)}}}]})]
- (testing "renders the query when the user can read its database"
+(deftest adhoc-viewing-context-permission-checks-database-test
+ (let [viewing {:user_is_viewing [{:type "adhoc"
+ :query {:database (mt/id)
+ :type "query"
+ :query {:source-table (mt/id :venues)}}}]}]
+ (testing "renders the query when the user can query its database"
(mt/with-test-user :crowberto
- (let [out (user-context/format-viewing-context (viewing (mt/id)))]
+ (let [out (user-context/format-viewing-context viewing)]
(is (str/includes? out "notebook editor"))
(is (str/includes? out "source-table")))))
- (testing "omits the query when the user cannot read its database"
- (mt/with-temp [:model/Database {db-id :id} {}]
- (mt/with-no-data-perms-for-all-users!
- (mt/with-test-user :rasta
- (let [out (user-context/format-viewing-context (viewing db-id))]
- (is (str/includes? out "notebook editor"))
- (is (not (str/includes? out "source-table"))))))))))
+ (testing "omits the query when the user can read the database but not query it"
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues)
+ :perms/manage-table-metadata :yes)
+ (mt/with-test-user :rasta
+ (is (mi/can-read? :model/Database (mt/id)))
+ (is (not (mi/can-query? :model/Database (mt/id))))
+ (let [out (user-context/format-viewing-context viewing)]
+ (is (str/includes? out "notebook editor"))
+ (is (not (str/includes? out "source-table")))))))))
+
+(deftest adhoc-viewing-context-unpermissionable-native-source-test
+ (testing "native SQL under a later stage is withheld when its permissions cannot be calculated"
+ (let [query (test-util/unpermissionable-native-query (mt/id))]
+ (mt/with-test-user :rasta
+ (is (:unchecked? (shared.content-store/query-for-export query true)))
+ (let [out (user-context/format-viewing-context {:user_is_viewing [{:type "adhoc" :query query}]})]
+ (is (str/includes? out "notebook editor"))
+ (is (not (str/includes? out "SELECT"))))))))
(deftest adhoc-viewing-context-virtual-database-id-gates-real-database-test
(let [viewing (fn [card-id]
diff --git a/test/metabase/metabot/test_util.clj b/test/metabase/metabot/test_util.clj
index 38f1b022f79..90079983288 100644
--- a/test/metabase/metabot/test_util.clj
+++ b/test/metabase/metabot/test_util.clj
@@ -173,6 +173,24 @@
(let [tool-defs (map #(%) [get-time-tool convert-currency-tool mock-llm-tool no-arg-tool])]
(into {} (map (juxt :tool-name identity)) tool-defs)))
+;;; ──────────────────────────────────────────────────────────────────
+;;; Queries
+;;; ──────────────────────────────────────────────────────────────────
+
+(defn unpermissionable-native-query
+ "Build a query on `database-id` with native SQL under a later stage, whose permissions can't be calculated.
+ Its snippet tag is missing `:snippet-name`, so the permission check can't normalize it."
+ [database-id]
+ {:lib/type :mbql/query
+ :database database-id
+ :stages [{:lib/type :mbql.stage/native
+ :native "SELECT * FROM {{snip}}"
+ :template-tags {"snip" {:type :snippet
+ :name "snip"
+ :display-name "snip"
+ :snippet-id Integer/MAX_VALUE}}}
+ {:lib/type :mbql.stage/mbql}]})
+
;;; ──────────────────────────────────────────────────────────────────
;;; Profiles
;;; ──────────────────────────────────────────────────────────────────
diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.clj
index 9b16bcb77fe..83cbd70213d 100644
--- a/test/metabase/metabot/tools/resources_test.clj
+++ b/test/metabase/metabot/tools/resources_test.clj
@@ -6,6 +6,7 @@
[medley.core :as m]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.schema.id :as lib.schema.id]
[metabase.metabot.test-util :as test-util]
[metabase.metabot.tools :as metabot.tools]
[metabase.metabot.tools.resources :as read-resource]
@@ -14,6 +15,8 @@
[metabase.metabot.tools.shared.llm-shape :as llm-shape]
[metabase.models.interface :as mi]
[metabase.models.serialization.resolve.mp :as resolve.mp]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.permissions-group :as perms-group]
[metabase.query-processor :as qp]
[metabase.test :as mt]
[metabase.transforms.core :as transforms.core]
@@ -380,11 +383,11 @@
"references content the user cannot read"))
(distinct @used)))))]
(binding [tools.shared/*memory-atom*
- (atom {:client-ids #{"seeded-chart" "seeded-q"}
- :state {:queries {"seeded-q" (query)}
- :charts {"seeded-chart" (chart "seeded-chart" "seeded-chart")
- "tool-chart" (chart "tool-chart" "tool-q")
- "created-chart" (chart "created-chart" "seeded-q")}}})]
+ (atom {:state {:client-ids #{"seeded-chart" "seeded-q"}
+ :queries {"seeded-q" (query)}
+ :charts {"seeded-chart" (chart "seeded-chart" "seeded-chart")
+ "tool-chart" (chart "tool-chart" "tool-q")
+ "created-chart" (chart "created-chart" "seeded-q")}}})]
(mt/with-test-user :rasta
(testing "a client-seeded chart or query audits the refusal"
(doseq [uri ["metabase://chart/seeded-chart" "metabase://query/seeded-q"]]
@@ -405,6 +408,76 @@
(is (not (str/includes? (:output result) "cannot read")))
(is (str/includes? (:output result) "source-table")))))))
+(defn- conversation-query-state
+ [query]
+ (atom {:state {:queries {"q-1" query}
+ :charts {"chart-1" {:chart_id "chart-1"
+ :query_id "q-1"
+ :visualization_settings {:chart_type "line"}}}}}))
+
+(deftest read-conversation-query-resource-permission-test
+ (testing "a stored query the user may not run has its body withheld"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-current-user (mt/user->id :rasta)
+ (binding [tools.shared/*memory-atom*
+ (conversation-query-state {:database (mt/id)
+ :type "query"
+ :query {:source-table (mt/id :orders)}})]
+ (doseq [uri ["metabase://query/q-1" "metabase://chart/chart-1"]]
+ (let [result (read-resource/read-resource {:uris [uri]})]
+ (is (str/includes? (:output result) "references content the user cannot read") uri)
+ (is (not (str/includes? (:output result) "ORDERS")) uri))))))))
+
+(deftest read-conversation-unpermissionable-native-query-resource-test
+ (testing "native SQL whose permissions cannot be calculated has its body withheld, whether a tool
+ stored the query or the client sent it"
+ (let [query (test-util/unpermissionable-native-query (mt/id))]
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (:unchecked? (shared.content-store/query-for-export query false)))
+ (doseq [client-ids [#{} #{"q-1"}]]
+ (binding [tools.shared/*memory-atom* (doto (conversation-query-state query)
+ (swap! assoc-in [:state :client-ids] client-ids))]
+ (doseq [uri ["metabase://query/q-1" "metabase://chart/chart-1"]]
+ (let [result (read-resource/read-resource {:uris [uri]})]
+ (is (str/includes? (:output result) "references content the user cannot read") uri)
+ (is (not (str/includes? (:output result) "SELECT")) uri)))))))))
+
+(deftest read-conversation-source-card-query-resource-test
+ (testing "a query sourced from a readable card exports through the card's collection access alone,
+ with no database permission of any kind"
+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :orders)}}}]
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-current-user (mt/user->id :rasta)
+ (doseq [database-id [(mt/id) lib.schema.id/saved-questions-virtual-database-id]]
+ (binding [tools.shared/*memory-atom*
+ (conversation-query-state {:database database-id
+ :type :query
+ :query {:source-table (str "card__" card-id)}})]
+ (doseq [uri ["metabase://query/q-1" "metabase://chart/chart-1"]]
+ (let [result (read-resource/read-resource {:uris [uri]})]
+ (is (=? {:resources [{:content map?}]} result))
+ (is (str/includes? (:output result) "source-card")))))))))))
+
+(deftest read-conversation-source-card-query-unreadable-card-test
+ (testing "a query sourced from a card in a collection the user cannot read has its body withheld"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection {coll-id :id} {}
+ :model/Card {card-id :id} {:collection_id coll-id
+ :dataset_query {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :orders)}}}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (binding [tools.shared/*memory-atom*
+ (conversation-query-state {:database (mt/id)
+ :type :query
+ :query {:source-table (str "card__" card-id)}})]
+ (doseq [uri ["metabase://query/q-1" "metabase://chart/chart-1"]]
+ (let [result (read-resource/read-resource {:uris [uri]})]
+ (is (str/includes? (:output result) "references content the user cannot read") uri)
+ (is (not (str/includes? (:output result) "ORDERS")) uri)))))))))
+
(deftest read-transform-resource-test
(mt/with-premium-features #{:transforms-basic :hosting}
(mt/with-current-user (mt/user->id :crowberto)
@@ -425,6 +498,29 @@
(is (=? {:resources [{:error string?}]}
(read-resource/read-resource {:uris ["metabase://transform/99999"]}))))))))
+(deftest read-transform-resource-source-permission-test
+ (testing "transforms/get-transform refuses a transform whose stored query the user cannot run, even
+ with query access to another table in its database, so the resource never reaches the source"
+ (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:name "Orders Rollup"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (=? {:resources [{:error "You don't have permissions to do that."}]}
+ (read-resource/read-resource {:uris [(str "metabase://transform/" transform-id)]}))))
+ (testing "and the query renders once the source table is granted"
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [result (read-resource/read-resource {:uris [(str "metabase://transform/" transform-id)]})]
+ (is (some? (get-in result [:resources 0 :content :structured-output :source :query]))))))))))))
+
(defn- read-title
"The chain-of-thought title `read-resource` derives from what it read."
[& uris]
diff --git a/test/metabase/metabot/tools/shared/content_store_test.clj b/test/metabase/metabot/tools/shared/content_store_test.clj
index ddca1c9bd55..f8be72cc4ec 100644
--- a/test/metabase/metabot/tools/shared/content_store_test.clj
+++ b/test/metabase/metabot/tools/shared/content_store_test.clj
@@ -26,7 +26,9 @@
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.schema.id :as lib.schema.id]
[metabase.lib.test-util :as lib.tu]
+ [metabase.metabot.test-util :as test-util]
[metabase.metabot.tools.shared.content-store :as shared.content-store]
[metabase.models.interface :as mi]
[metabase.models.serialization.resolve :as resolve]
@@ -289,6 +291,24 @@
(is (some? (shared.content-store/query-for-export (query-on (mt/id :orders)) audited?)))
(is (nil? (shared.content-store/query-for-export (query-on (mt/id :venues)) audited?)))))))))
+(deftest unparseable-query-renders-unresolved-test
+ (testing "a stored query that will not parse is not a denial: it comes back with no provider, so
+ the caller renders it without resolving anything in it"
+ (mt/with-test-user :rasta
+ (let [malformed {:lib/type :mbql/query :type :query :database (mt/id) :stages []}]
+ (is (= {:query malformed :unchecked? true}
+ (shared.content-store/query-for-export malformed false)))))))
+
+(deftest unresolvable-database-is-unchecked-test
+ (testing "a database id that will not resolve is a check that could not be made, so native SQL on it
+ is withheld like any other unchecked query"
+ (mt/with-test-user :rasta
+ (doseq [database-id [lib.schema.id/saved-questions-virtual-database-id 0]]
+ (let [query (test-util/unpermissionable-native-query database-id)]
+ (is (= {:query query :unchecked? true}
+ (shared.content-store/query-for-export query false))
+ (str database-id)))))))
+
(deftest native-query-needs-database-wide-native-access-test
(testing "a native query is withheld unless the user may write native queries against the whole database"
(mt/with-no-data-perms-for-all-users!
diff --git a/test/metabase/metabot/tools/transforms_test.clj b/test/metabase/metabot/tools/transforms_test.clj
index 75cebbd7f38..1c3c55dc44a 100644
--- a/test/metabase/metabot/tools/transforms_test.clj
+++ b/test/metabase/metabot/tools/transforms_test.clj
@@ -5,9 +5,14 @@
[clojure.test :refer :all]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.metabot.metadata-perms :as metabot.perms]
+ [metabase.metabot.query-export :as query-export]
[metabase.metabot.tools.transforms :as agent-transforms]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
- [metabase.util.json :as json]))
+ [metabase.util.json :as json]
+ [toucan2.core :as t2]))
;;; ----------------------------------- read tool integration tests ---------------------------------------------------
@@ -43,6 +48,107 @@
(is (not-any? #(and (map? %) (contains? % "lib/metadata"))
(tree-seq coll? seq exported)))))))))
+(defn- query-transform-details!
+ "`get_transform_details` output for a transform whose source is `query`, read as rasta with query
+ access to the whole database - so `transforms/get-transform` passes and whatever happens to the
+ source is the export gate's doing."
+ [query]
+ (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:name "Orders Rollup" :source {:type "query" :query query}}]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-current-user (mt/user->id :rasta)
+ (:output (agent-transforms/get-transform-details-tool {:transform_id transform-id})))))))
+
+(deftest get-transform-details-sandboxed-field-test
+ (testing "a source query naming a field the user's sandbox hides renders without its query, since
+ exporting it would resolve that field's id to a name"
+ (let [query (-> (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))
+ (lib/filter (lib/> (lib.metadata/field (mt/metadata-provider) (mt/id :venues :price)) 1)))]
+ (is (str/includes? (query-transform-details! query) "<query>"))
+ (mt/with-dynamic-fn-redefs [metabot.perms/sandbox-restricted-fields (fn [_table-ids] {(mt/id :venues) #{}})]
+ (is (not (str/includes? (query-transform-details! query) "<query>")))))))
+
+(deftest get-transform-details-unpermissionable-source-test
+ (testing "a source query whose permissions cannot be calculated at all still renders, since that is
+ not a refusal - it just renders unresolved, naming nothing the user may not see"
+ (let [output (query-transform-details! {:database (mt/id)
+ :type :query
+ :query {:source-table "card__13371337"}})]
+ (is (str/includes? output "<query>"))
+ (is (str/includes? output ":source-card 13371337")
+ "the card id is still a number, so nothing was resolved to a name"))))
+
+(defn- rendered-source
+ "The source query [[query-export/transform-with-exportable-source]] leaves on a transform whose
+ source is `query`, or nil when it withholds it."
+ [query]
+ (get-in (query-export/transform-with-exportable-source {:source {:type "query" :query query}})
+ [:source :query]))
+
+(deftest transform-source-withholds-native-sql-behind-a-later-stage-test
+ (testing "a native stage under an MBQL stage is still native, so a check that could not be made
+ withholds it instead of printing the SQL in the unresolved fallback"
+ (let [native-stage {:lib/type :mbql.stage/native
+ :native "SELECT * FROM {{snip}}"
+ :template-tags {"snip" {:type :snippet
+ :name "snip"
+ :display-name "snip"
+ :snippet-id Integer/MAX_VALUE}}}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (doseq [stages [[native-stage] [native-stage {:lib/type :mbql.stage/mbql}]]]
+ (is (nil? (rendered-source {:lib/type :mbql/query :database (mt/id) :stages stages}))
+ (str (count stages) " stage(s)")))))))
+
+(deftest transform-source-survives-a-deleted-source-database-test
+ (testing "an orphaned transform keeps its source, which is what an admin repairs it from: there
+ is no metadata left behind a deleted database to resolve anything against"
+ (mt/with-temp [:model/Database {db-id :id} {}]
+ (t2/delete! :model/Database :id db-id)
+ (mt/with-current-user (mt/user->id :crowberto)
+ (is (str/includes? (str (rendered-source {:database db-id
+ :type :native
+ :native {:query "SELECT 1"}}))
+ "SELECT 1"))))))
+
+(deftest get-transform-details-unpermissionable-native-source-test
+ (testing "native SQL stays out when the check could not run, since its table and column names are
+ already plain text and having no provider hides nothing"
+ (let [output (query-transform-details!
+ {:database (mt/id)
+ :type :native
+ :native {:query "SELECT * FROM {{snip}}"
+ :template-tags {"snip" {:type :snippet
+ :name "snip"
+ :display-name "snip"
+ :snippet-id Integer/MAX_VALUE}}}})]
+ (is (not (str/includes? output "<query>")))
+ (is (not (str/includes? output "SELECT"))))))
+
+(deftest get-transform-details-source-permission-test
+ (testing "transforms/get-transform refuses a transform whose stored query the user cannot run, even
+ with query access to another table in its database, so the tool never reaches the source"
+ (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:name "Orders Rollup"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (=? {:status-code 403 :output "You don't have permissions to do that."}
+ (agent-transforms/get-transform-details-tool {:transform_id transform-id}))))
+ (testing "and the query renders once the source table is granted"
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (str/includes? (:output (agent-transforms/get-transform-details-tool {:transform_id transform-id}))
+ "<query>"))))))))))
+
(deftest get-transform-details-python-source-test
(mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
(mt/with-current-user (mt/user->id :crowberto)