Skip to content

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

BOT-1956: Fix permission-check gaps in transform/query export (#79877)

Andrei Simionescu062d2811920b
diff --git a/src/metabase/agent_lib/representations/resolve.clj b/src/metabase/agent_lib/representations/resolve.clj
index 6b71ee2ae9d..096d1e37c01 100644
--- a/src/metabase/agent_lib/representations/resolve.clj
+++ b/src/metabase/agent_lib/representations/resolve.clj
@@ -250,14 +250,13 @@
an unusual or partially-broken existing `dataset_query` should gracefully drop out of the
payload rather than break the whole tool response.
- Like [[export-query]], the 3-arity form accepts an explicit `content-store`; agent callers
- pass `metabase.metabot.tools.shared.content-store/default-store` for read-checking."
- ([metadata-provider pmbql-query]
- (try-export-query metadata-provider pmbql-query resolve.mp/unchecked-app-db-content-store))
- ([metadata-provider pmbql-query content-store]
- (when (and metadata-provider (map? pmbql-query) (seq pmbql-query))
- (try
- (export-query metadata-provider pmbql-query content-store)
- (catch Exception e
- (log/warnf "Failed to export MBQL 5 query to portable representations; omitting from LLM payload: %s" (ex-message e))
- nil)))))
+ `content-store` is required: every caller is an LLM context-building path, and falling
+ through to the unchecked app-DB store would skip the permission check without saying so.
+ Agent callers pass `metabase.metabot.tools.shared.content-store/default-store`."
+ [metadata-provider pmbql-query content-store]
+ (when (and metadata-provider (map? pmbql-query) (seq pmbql-query))
+ (try
+ (export-query metadata-provider pmbql-query content-store)
+ (catch Exception e
+ (log/warnf "Failed to export MBQL 5 query to portable representations; omitting from LLM payload: %s" (ex-message e))
+ nil))))
diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.clj
index 2f15996ee43..94c3f697fb2 100644
--- a/src/metabase/metabot/agent/core.clj
+++ b/src/metabase/metabot/agent/core.clj
@@ -441,6 +441,18 @@
state
(:user_is_viewing context)))
+(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."
+ [context]
+ (let [seeded (-> {} (seed-state context) (seed-charts context))]
+ (into (set (keys (:queries seeded)))
+ (map str)
+ (keys (:charts seeded)))))
+
;;; Main loop
(def ^:private profile-id->required-permission
@@ -479,7 +491,8 @@
(seed-chart-configs context)
(seed-charts context))
memory (assoc (memory/initialize messages seeded context)
- :conversation-id conversation-id)
+ :conversation-id conversation-id
+ :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/user_context.clj b/src/metabase/metabot/agent/user_context.clj
index 73ea6cc0270..7946076be57 100644
--- a/src/metabase/metabot/agent/user_context.clj
+++ b/src/metabase/metabot/agent/user_context.clj
@@ -7,14 +7,12 @@
[clojure.string :as str]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
- [metabase.metabot.metadata-perms :as metabot.perms]
- [metabase.metabot.query-analyzer :as query-analyzer]
[metabase.metabot.tmpl :as te]
[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.metabot.util :as metabot.u]
- [metabase.models.interface :as mi]
[metabase.util :as u]
[metabase.util.log :as log])
(:import
@@ -250,16 +248,13 @@
;;; Viewing Context Formatting
-(defn- query-if-database-readable
- "The client-supplied adhoc query, only when the current user can read its database.
- Exporting resolves table/field ids to names through an unfiltered metadata provider,
- so gate it like the metabase://chart|query resources do. Queries with no :database
- only ever pprint (no name resolution), so they pass through."
+(defn- exported-query-text
+ "The client-supplied query rendered for the LLM, only when the current user can read its
+ 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]
- (let [database-id (and (map? query) (:database query))]
- (when (or (not database-id)
- (mi/can-read? :model/Database database-id))
- 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)))
;; Format adhoc query (notebook editor) viewing context.
(defmethod format-entity "adhoc"
@@ -269,99 +264,19 @@
(te/lines "The user is currently in the notebook editor viewing a query."
(te/field "Query ID" (:id item))
(te/field "Database ID" (get-in item [:query :database]))
- (te/field "Query" (some-> (:query item) query-if-database-readable llm-shape/export-query-for-llm))
+ (te/field "Query" (exported-query-text (:query item)))
(when-let [config-ids (format-chart-config-ids item)]
(te/field "Chart Config IDs (for analyze_chart tool)" config-ids))
(te/field "Tables used" (some->> (:used_tables item)
(map format-entity)
te/lines)))))
-(def ^:private exported-table-id-keys
- [:source-table :source_table])
-
-(def ^:private exported-card-id-keys
- [:source-card :source_card :card-id :card_id])
-
-(def ^:private exported-field-id-keys
- [:source-field :metabase.models.visualization-settings/param-mapping-source])
-
-(defn- exported-entity-ids
- [normalized]
- (let [ids (fn [ks node] (into #{} (comp (map #(get node %)) (filter pos-int?)) ks))]
- (reduce
- (fn [acc node]
- (cond
- (map? node)
- (-> acc
- (update :table into (ids exported-table-id-keys node))
- (update :card into (ids exported-card-id-keys node))
- (update :field into (ids exported-field-id-keys node)))
-
- (and (vector? node) (not (map-entry? node)))
- (case (keyword (first node))
- (:field :field-id) (update acc :field into (filter pos-int?) [(nth node 1 nil) (nth node 2 nil)])
- :metric (update acc :card into (filter pos-int?) [(nth node 1 nil) (nth node 2 nil)])
- acc)
-
- :else acc))
- {:table #{} :card #{} :field #{}}
- (tree-seq coll? seq normalized))))
-
-(defn- native-stage?
- [normalized]
- (boolean (some #(and (map? %) (= :mbql.stage/native (:lib/type %)))
- (tree-seq coll? seq normalized))))
-
-(defn- native-sql-table-ids
- [normalized]
- (try
- (into #{}
- (comp (keep #(or (:table-id %) (:id %))) (filter pos-int?))
- (:tables (query-analyzer/tables-for-native normalized :all-drivers-trusted? true)))
- (catch Exception e
- (log/debugf "Could not analyze a viewing-context native query for permission gating: %s"
- (ex-message e))
- #{})))
-
-(defn- sandbox-visible-fields?
- [field-id->table-id]
- (let [restricted (metabot.perms/sandbox-restricted-fields (set (vals field-id->table-id)))]
- (every? (fn [[field-id table-id]]
- (if-let [allowed (get restricted table-id)]
- (contains? allowed field-id)
- true))
- field-id->table-id)))
-
-(defn- queryable-normalized-query
- [query]
- (let [raw-database-id (and (map? query) (:database query))]
- (when (pos-int? raw-database-id)
- (try
- (let [normalized (lib-be/normalize-query query)
- database-id (:database normalized)]
- (when (and (pos-int? database-id)
- (mi/can-query? :model/Database database-id))
- (let [{:keys [table card field]} (exported-entity-ids normalized)
- field-table (metabot.perms/field-id->table-id field)
- table-ids (cond-> (into (set table) (vals field-table))
- (native-stage? normalized) (into (native-sql-table-ids normalized)))]
- (when (and (= table-ids (metabot.perms/queryable-table-ids table-ids))
- (sandbox-visible-fields? field-table)
- (every? #(mi/can-read? :model/Card %) card))
- [normalized (lib-be/application-database-metadata-provider database-id)]))))
- (catch Exception e
- (log/debugf "Omitting a viewing-context query that could not be permission-checked: %s"
- (ex-message e))
- nil)))))
-
(defn- transform-query-source-text
"Format a transform's `:query` source for the LLM; the rendering and fallback contract
- lives in [[llm-shape/export-query-for-llm]]."
+ lives in [[llm-shape/export-query-for-llm]]. The source arrives inline in the viewing context,
+ as client-supplied as the adhoc query above, so it gets the same audited gate and store."
[source]
- (let [query (:query source)]
- (when (or (not (and (map? query) (:database query)))
- (queryable-normalized-query query))
- (llm-shape/export-query-for-llm query))))
+ (exported-query-text (:query source)))
(defn- transform-source-type
[source]
diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.clj
index 9fd6aae77d7..c628ec1bf9e 100644
--- a/src/metabase/metabot/tools/construct.clj
+++ b/src/metabase/metabot/tools/construct.clj
@@ -179,9 +179,9 @@
(def ^:private permission-aware-content-store
"ContentStore for agent query construction. Alias for
- [[shared.content-store/default-store]] — the chokepoint wrapper applies `api/read-check` to
- every lookup whenever `api/*current-user-id*` is bound, symmetrically across all six
- ContentStore methods. The unchecked underlying store rejects non-NanoID entity-id values."
+ [[shared.content-store/default-store]], which permission-checks every lookup whenever
+ `api/*current-user-id*` is bound; see that namespace for the audited/unaudited split between the
+ two lookup directions. The unchecked underlying store rejects non-NanoID entity-id values."
shared.content-store/default-store)
(defn- portable-field-fk-table
@@ -403,7 +403,11 @@
;; etc.). Wrap the whole rest of the pipeline in a single `:agent-error?` relay so any of
;; them reach the tool wrapper with the flag set.
(try
- (let [repaired (repr.repair/repair mp parsed permission-aware-content-store)
+ (let [;; Repair's mini-resolve lookups are best-effort - a failure is logged at debug and the
+ ;; clause left alone - and `resolve-query` below resolves the query for real. A refusal
+ ;; in there reaches nobody, so it must not go into the audit trail as an access attempt.
+ repaired (binding [resolve.mp/*audit-refusals?* false]
+ (repr.repair/repair mp parsed permission-aware-content-store))
_perms (check-source-table-query-permissions! mp repaired checked)
_validated (repr/validate-query repaired)
pmbql-query (repr.resolve/resolve-query mp repaired permission-aware-content-store)
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.clj
index 85b8a39cf54..5d9a825c531 100644
--- a/src/metabase/metabot/tools/resources.clj
+++ b/src/metabase/metabot/tools/resources.clj
@@ -77,6 +77,7 @@
[metabase.metabot.tools.entity-details :as entity-details]
[metabase.metabot.tools.field-stats :as field-stats]
[metabase.metabot.tools.shared :as shared]
+ [metabase.metabot.tools.shared.content-store :as shared.content-store]
[metabase.metabot.tools.shared.instructions :as instructions]
[metabase.metabot.tools.shared.llm-shape :as llm-shape]
[metabase.models.interface :as mi]
@@ -721,19 +722,33 @@
(str/join "\n" (map-indexed document-block-line blocks)))
"The document is empty.")})))
+(def ^:private query-withheld-message
+ "Names no particular grant: the body is withheld both for an unreadable database and for a
+ 403 on a card, measure or segment inside the query."
+ "The query is hidden because it references content the user cannot read.")
+
+(defn- export-state-query
+ "Export a conversation-state query for presentation, nil when the user may not see it.
+ `query-id` picks the treatment: an id seeded from the client's viewing context carries
+ ids that are the caller's own, so its refusals are audited; one the agent's tools wrote
+ 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)))))
+
(defn- fetch-conversation-query
"Present a query stored in this conversation's agent state (created by tools or pasted
- as a chart mention). Read-checks the query's database before exporting it with resolved
- table/field names."
+ as a chart mention). A query the user may not see is withheld."
[query-id]
(if-let [query (get (shared/current-queries-state) query-id)]
- (do
- (when-let [database-id (and (map? query) (:database query))]
- (api/read-check :model/Database database-id))
- (entity-result
- {:type "conversation-query"
- :id query-id
- :description (llm-shape/export-query-for-llm query)}))
+ (entity-result
+ {:type "conversation-query"
+ :id query-id
+ :description (or (export-state-query query-id query)
+ query-withheld-message)})
{:status-code 404
:output (str "No chart or query with id '" query-id "' exists in this conversation. "
"It may belong to another conversation; ask the user to paste or recreate it here.")}))
@@ -743,18 +758,23 @@
pasted as a mention). Falls back to the queries state when the id is actually a query id."
[chart-id]
(if-let [chart (get (shared/current-charts-state) chart-id)]
- (let [query (or (first (:queries chart))
- (get (shared/current-queries-state) (:query_id chart)))]
- (when-let [database-id (and (map? query) (:database query))]
- (api/read-check :model/Database database-id))
+ (let [[query-id query] (if-let [q (first (:queries chart))]
+ ;; create_chart mints a new chart id for a client-supplied query and
+ ;; keeps the original under :query_id, so the audit polarity has to
+ ;; follow the query rather than the chart
+ [(or (:query_id chart) chart-id) q]
+ [(:query_id chart) (get (shared/current-queries-state) (:query_id chart))])
+ query-text (when query (export-state-query query-id query))]
(entity-result
{:type "conversation-chart"
:id chart-id
:description (str "Chart type: "
(or (some-> (get-in chart [:visualization_settings :chart_type]) name)
"table")
- "\nQuery:\n"
- (llm-shape/export-query-for-llm query))}))
+ (cond
+ (nil? query) "\nNo query is attached to this chart."
+ query-text (str "\nQuery:\n" query-text)
+ :else (str "\n" query-withheld-message)))}))
(fetch-conversation-query chart-id)))
;; ----- Dispatch -----
diff --git a/src/metabase/metabot/tools/shared.clj b/src/metabase/metabot/tools/shared.clj
index 2da651b9f71..8ba0ea306a9 100644
--- a/src/metabase/metabot/tools/shared.clj
+++ b/src/metabase/metabot/tools/shared.clj
@@ -50,6 +50,14 @@
[]
(get-in (current-memory) [:state :charts] {}))
+(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."
+ []
+ (get (current-memory) :client-ids #{}))
+
(defn current-chart-configs-state
"Returns the current chart-configs state map from agent memory.
Chart configs contain pre-materialized series data from the frontend viewing context."
diff --git a/src/metabase/metabot/tools/shared/content_store.clj b/src/metabase/metabot/tools/shared/content_store.clj
index e2dc18c9cd4..e9fa063b262 100644
--- a/src/metabase/metabot/tools/shared/content_store.clj
+++ b/src/metabase/metabot/tools/shared/content_store.clj
@@ -1,26 +1,31 @@
(ns metabase.metabot.tools.shared.content-store
- "Permission-aware wrapper around `metabase.models.serialization.resolve.mp/ContentStore`.
-
- The metadata-provider-backed resolver in `resolve.mp` is deliberately permission-agnostic so
- that serdes import / background tasks can use it without an authenticated user. HTTP and
- agent-tool paths run under a current user and therefore must layer in `api/read-check` —
- this namespace is the chokepoint.
-
- `read-checked` wraps any `ContentStore` so every lookup that returns a Card / Measure /
- Segment row is gated by `api/read-check` whenever `api/*current-user-id*` is bound. When
- the var is unbound (serdes, REPL, background tasks, tests without auth context), rows pass
- through unchanged.
-
- `default-store` is the standard agent / tool-path store — `unchecked-app-db-content-store`
- wrapped with `read-checked`. Use this as the content store for any agent or tool path that
- runs under an authenticated request. The symmetry across all six methods (both
- `-by-entity-id` import-direction and `-by-id` export-direction) is intentional: the export
- direction may surface entity_ids of Cards / Measures / Segments referenced inside an
- exported query body, and a missing check on those branches is exactly the N1 ACL gap that
- motivated this namespace."
+ "Permission-aware wrappers around [[resolve.mp/ContentStore]].
+
+ The resolver in `resolve.mp` is permission-agnostic so serdes import and background tasks can
+ use it with no authenticated user. HTTP and agent-tool paths run under a current user and must
+ layer a check on top; this namespace is that chokepoint.
+
+ [[read-checked]] gates all six methods, both lookup directions: the export direction can
+ surface entity_ids of Cards / Measures / Segments referenced inside an exported query body,
+ which is the N1 ACL gap this namespace closes. A row the current user cannot read never reaches
+ the caller, and rows pass through unchanged when `api/*current-user-id*` is unbound (serdes,
+ REPL, background tasks, tests with no auth context).
+
+ What varies is whether a refusal is audited: [[api/read-check]] leaves an ERROR log line and an
+ `:event/read-permission-failure`, [[api/check-403]] throws the same bare 403 without them.
+ [[default-store]] audits the `-by-entity-id` methods only, [[audited-store]] audits all six,
+ and [[resolve.mp/*audit-refusals?*]] suppresses auditing for a caller that discards the
+ refusal."
(:require
[metabase.api.common :as api]
- [metabase.models.serialization.resolve.mp :as resolve.mp]))
+ [metabase.lib-be.core :as lib-be]
+ [metabase.metabot.db :as metabot.db]
+ [metabase.metabot.metadata-perms :as metabot.perms]
+ [metabase.metabot.tools.shared :as shared]
+ [metabase.models.interface :as mi]
+ [metabase.models.serialization.resolve.mp :as resolve.mp]
+ [metabase.query-permissions.core :as query-perms]
+ [metabase.util.log :as log]))
(set! *warn-on-reflection* true)
@@ -34,21 +39,174 @@
api/*current-user-id* (api/read-check row)
:else row))
+(defn- maybe-check-403
+ "Like [[maybe-read-check]], but throws a bare 403 rather than an audited one."
+ [row]
+ (cond
+ (nil? row) nil
+ api/*current-user-id* (do (api/check-403 (mi/can-read? row)) row)
+ :else row))
+
+(defn- checked
+ "Permission-check `row`, auditing a refusal only when `audited?` and
+ [[resolve.mp/*audit-refusals?*]] both hold."
+ [audited? row]
+ (if (and audited? resolve.mp/*audit-refusals?*)
+ (maybe-read-check row)
+ (maybe-check-403 row)))
+
(defn read-checked
- "Wrap `store` so every lookup applies `api/read-check` when `api/*current-user-id*` is
- bound. Symmetric across all six `ContentStore` methods."
- [store]
- (reify resolve.mp/ContentStore
- (card-by-entity-id [_ eid] (maybe-read-check (resolve.mp/card-by-entity-id store eid)))
- (measure-by-entity-id [_ eid] (maybe-read-check (resolve.mp/measure-by-entity-id store eid)))
- (segment-by-entity-id [_ eid] (maybe-read-check (resolve.mp/segment-by-entity-id store eid)))
- (card-by-id [_ id] (maybe-read-check (resolve.mp/card-by-id store id)))
- (measure-by-id [_ id] (maybe-read-check (resolve.mp/measure-by-id store id)))
- (segment-by-id [_ id] (maybe-read-check (resolve.mp/segment-by-id store id)))))
+ "Wrap `store` so every lookup permission-checks its row when `api/*current-user-id*` is bound,
+ throwing a 403 when the current user cannot read it. Refusals on the `-by-entity-id` methods are
+ audited; `audited-by-id?` audits the `-by-id` methods too."
+ ([store] (read-checked store false))
+ ([store audited-by-id?]
+ (reify resolve.mp/ContentStore
+ (card-by-entity-id [_ eid] (checked true (resolve.mp/card-by-entity-id store eid)))
+ (measure-by-entity-id [_ eid] (checked true (resolve.mp/measure-by-entity-id store eid)))
+ (segment-by-entity-id [_ eid] (checked true (resolve.mp/segment-by-entity-id store eid)))
+ (card-by-id [_ id] (checked audited-by-id? (resolve.mp/card-by-id store id)))
+ (measure-by-id [_ id] (checked audited-by-id? (resolve.mp/measure-by-id store id)))
+ (segment-by-id [_ id] (checked audited-by-id? (resolve.mp/segment-by-id store id))))))
(def default-store
- "The standard agent / tool-path content store: `unchecked-app-db-content-store` wrapped with
- `read-checked`. Pass this to `repr.resolve/resolve-query`, `repr.resolve/export-query`,
- `repr.resolve/try-export-query`, `repair/repair`, or any other resolver entry-point that
- runs under an authenticated request."
+ "[[resolve.mp/unchecked-app-db-content-store]] under [[read-checked]]. The store for any agent
+ or tool path running under an authenticated request, over a query loaded from the app DB."
(read-checked resolve.mp/unchecked-app-db-content-store))
+
+(def audited-store
+ "[[default-store]] with `-by-id` refusals audited too. For a query the client supplied rather
+ than one loaded from the app DB, where the numeric ids in it are the caller's own."
+ (read-checked resolve.mp/unchecked-app-db-content-store true))
+
+;;; The stores gate the content *inside* an export. Exporting also resolves table and field ids
+;;; to names through an unfiltered provider, so the query's database and referenced tables need
+;;; their own gate first. Same quiet/audited split as the stores, for the same reason.
+
+(defn- resolve-effective-database
+ "`query` with `:database` resolved to the id the export really uses: the virtual id `-1337`
+ resolves through the source card, so gating on the raw value would check an id the export
+ never touches. Nil when it can't be resolved."
+ [query]
+ (when (map? query)
+ (try
+ (lib-be/resolve-database query)
+ (catch Exception _ nil))))
+
+(def ^:private exported-table-id-keys
+ [:source-table :source_table])
+
+(def ^:private exported-card-id-keys
+ [:source-card :source_card :card-id :card_id])
+
+(def ^:private exported-field-id-keys
+ [:source-field :metabase.models.visualization-settings/param-mapping-source])
+
+(defn- exported-entity-ids
+ [normalized]
+ (let [ids (fn [ks node] (into #{} (comp (map #(get node %)) (filter pos-int?)) ks))]
+ (reduce
+ (fn [acc node]
+ (cond
+ (map? node)
+ (-> acc
+ (update :table into (ids exported-table-id-keys node))
+ (update :card into (ids exported-card-id-keys node))
+ (update :field into (ids exported-field-id-keys node)))
+
+ (and (vector? node) (not (map-entry? node)))
+ (case (keyword (first node))
+ (:field :field-id) (update acc :field into (filter pos-int?) [(nth node 1 nil) (nth node 2 nil)])
+ :metric (update acc :card into (filter pos-int?) [(nth node 1 nil) (nth node 2 nil)])
+ acc)
+
+ :else acc))
+ {:table #{} :card #{} :field #{}}
+ (tree-seq coll? seq normalized))))
+
+(defn- sandbox-visible-fields?
+ [field-id->table-id]
+ (let [restricted (metabot.perms/sandbox-restricted-fields (set (vals field-id->table-id)))]
+ (every? (fn [[field-id table-id]]
+ (if-let [allowed (get restricted table-id)]
+ (contains? allowed field-id)
+ true))
+ field-id->table-id)))
+
+(defn- readable?
+ "Whether the current user can read the row; with `audited?` a refusal leaves the
+ [[api/read-check]] audit trail."
+ [audited? model id]
+ (if audited?
+ (try
+ (api/read-check model id)
+ true
+ (catch clojure.lang.ExceptionInfo e
+ (if (= 403 (:status-code (ex-data e)))
+ false
+ (throw e))))
+ (mi/can-read? model id)))
+
+(defn- runnable-normalized-query
+ "`resolved` normalized, when the current user may run it and see every table and field it
+ names: the query processor's own run check (a saved question authorizes through its collection, native
+ SQL through database-wide native access), then a per-table check on the ids the export
+ resolves to names and a column-sandbox check on its field refs. The saved questions the
+ query reads from are checked first with the caller's audit polarity, since the run check
+ 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."
+ [audited? resolved]
+ (try
+ (let [normalized (lib-be/normalize-query resolved)
+ 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
+ (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))]
+ (when (and (= table-ids (metabot.perms/queryable-table-ids table-ids))
+ (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)))
+
+(defn- cached-pass
+ "Memoize an allowed gate result on the agent's memory for the rest of the turn: the check
+ preprocesses the query in full, and the same conversation query is usually read more than once.
+ Refusals are not kept - that is where the audit trail lives, and it has to fire on every read.
+ Outside an agent run there is nowhere to cache, so the check just runs."
+ [cache-key f]
+ (if-let [memory-atom shared/*memory-atom*]
+ (or (get-in @memory-atom [:query-gate-cache cache-key])
+ (let [result (f)]
+ (when result
+ (swap! memory-atom assoc-in [:query-gate-cache cache-key] result))
+ result))
+ (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."
+ [query audited?]
+ (if-not (and (map? query) (:database query))
+ [query nil]
+ (cached-pass
+ [query audited?]
+ (fn []
+ (when-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]))))))
diff --git a/src/metabase/metabot/tools/shared/llm_shape.clj b/src/metabase/metabot/tools/shared/llm_shape.clj
index 6a3179106b5..0f3884ed2e2 100644
--- a/src/metabase/metabot/tools/shared/llm_shape.clj
+++ b/src/metabase/metabot/tools/shared/llm_shape.clj
@@ -87,25 +87,36 @@
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."
- [query]
- (cond
- (string? query) query
- (string? (:query-content query)) (:query-content query)
- (and (map? query) (:database query))
- (try
- (let [normalized (lib-be/normalize-query query)
- mp (lib-be/application-database-metadata-provider (:database normalized))
- exported (repr.resolve/export-query mp normalized shared.content-store/default-store)]
- (or (repr-data->llm-block exported)
- (query-edn-fallback normalized)))
- (catch Exception e
- (when-not (= 403 (:status-code (ex-data e)))
- (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)))
+ export renders nothing at all rather than the fallback.
+
+ `store` gates the Card / Measure / Segment lookups and every caller names one: the unaudited
+ [[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))
+ (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
+ (log/debugf "Failed to export query for LLM: %s" (ex-message e))
+ (when-not (= 403 (:status-code (ex-data 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))))
(defn transform-query->text
"Render a transform source query for model context: native SQL verbatim, anything else
@@ -113,7 +124,7 @@
fence remains valid when the result is interpolated inside an XML `<query>` element."
[query]
(or (when (map? query) (metabot.u/extract-sql-content query))
- (when-let [text (export-query-for-llm query)]
+ (when-let [text (export-query-for-llm query shared.content-store/default-store)]
(if (str/starts-with? text "```json\n")
(format "\n%s\n" text)
text))))
diff --git a/src/metabase/models/serialization/resolve/mp.clj b/src/metabase/models/serialization/resolve/mp.clj
index c7574feb82a..32d92c61628 100644
--- a/src/metabase/models/serialization/resolve/mp.clj
+++ b/src/metabase/models/serialization/resolve/mp.clj
@@ -277,6 +277,16 @@
;;; Content store - Metabase asset lookups by portable entity id or numeric id
;;; ============================================================
+(def ^:dynamic *audit-refusals?*
+ "Whether a permission-aware [[ContentStore]] audits a refusal, i.e. leaves the ERROR log line
+ and `:event/read-permission-failure` that `api/read-check` does, rather than throwing a bare
+ 403. Bind to false around a lookup whose refusal you catch and discard.
+
+ The permission check itself is unaffected; an unreadable row never reaches the caller either
+ way. The stores defined below are permission-agnostic and ignore this var. It is honoured by
+ `metabase.metabot.tools.shared.content-store/read-checked`."
+ true)
+
(p.types/defprotocol+ ContentStore
"Lookup of Metabase content (\"assets\") by portable entity id or numeric id.
diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.clj
index f4c784a48e6..46facd4396f 100644
--- a/src/metabase/query_permissions/impl.clj
+++ b/src/metabase/query_permissions/impl.clj
@@ -431,21 +431,35 @@
false))))
(mu/defn can-run-query?
- "Return `true` if the current user has sufficient permissions to run `query`, and `false` otherwise."
+ "Return `true` if the current user has sufficient permissions to run `query`, and `false` otherwise.
+
+ With `throw-non-permission-errors?`, anything that is not a permission denial throws rather than
+ being answered as one: a failure to work out which permissions `query` needs (otherwise logged at
+ error and folded into the answer), and any other error the checks raise, such as a missing Card in
+ [[check-card-read-perms]]. A denial still returns `false`."
([query]
(can-run-query? query false))
+ ([query :- :map
+ already-preprocessed? :- :boolean]
+ (can-run-query? query already-preprocessed? false))
+
([{database-id :database :as query} :- :map
- already-preprocessed? :- :boolean]
+ already-preprocessed? :- :boolean
+ throw-non-permission-errors? :- :boolean]
(try
- (let [required-perms (required-perms-for-query query :already-preprocessed? already-preprocessed?)]
+ (let [required-perms (required-perms-for-query query
+ :already-preprocessed? already-preprocessed?
+ :throw-exceptions? throw-non-permission-errors?)]
(check-data-perms query required-perms)
;; Check card read permissions for any cards referenced in subqueries!
(doseq [card-id (:card-ids required-perms)]
(check-card-read-perms database-id card-id))
true)
- (catch clojure.lang.ExceptionInfo _e
- false))))
+ (catch clojure.lang.ExceptionInfo e
+ (if (and throw-non-permission-errors? (not (:permissions-error? (ex-data e))))
+ (throw e)
+ false)))))
(mu/defn can-query-table?
"Does the current user have permissions to run an ad-hoc query against the Table with `table-id`?"
diff --git a/test/metabase/agent_lib/representations/resolve_test.clj b/test/metabase/agent_lib/representations/resolve_test.clj
index 6abe5556bd5..29b9398bc7b 100644
--- a/test/metabase/agent_lib/representations/resolve_test.clj
+++ b/test/metabase/agent_lib/representations/resolve_test.clj
@@ -565,7 +565,7 @@
"breakout" [["field" {"temporal-unit" "month"}
["Sample" "PUBLIC" "ORDERS" "CREATED_AT"]]]}]}
q (repr.resolve/resolve-query mp parsed)
- exported (repr.resolve/try-export-query mp q)]
+ exported (repr.resolve/try-export-query mp q resolve.mp/unchecked-app-db-content-store)]
(is (map? exported))
(is (= "mbql/query" (get exported "lib/type")))
(is (= "Sample" (get exported "database")))
@@ -577,9 +577,10 @@
(deftest try-export-query-nil-and-error-fallback-test
(testing "nil / blank input returns nil so the caller can omit the field"
- (is (nil? (repr.resolve/try-export-query mp nil)))
- (is (nil? (repr.resolve/try-export-query mp {})))
- (is (nil? (repr.resolve/try-export-query nil {:lib/type :mbql/query :database 1 :stages []}))))
+ (is (nil? (repr.resolve/try-export-query mp nil resolve.mp/unchecked-app-db-content-store)))
+ (is (nil? (repr.resolve/try-export-query mp {} resolve.mp/unchecked-app-db-content-store)))
+ (is (nil? (repr.resolve/try-export-query nil {:lib/type :mbql/query :database 1 :stages []}
+ resolve.mp/unchecked-app-db-content-store))))
(testing "export errors are swallowed and surface as nil (the caller falls back gracefully)"
;; A query map with a numeric field id that doesn't exist in the metadata-provider
;; will throw inside `export-mbql`; we want the helper to return nil rather than
@@ -588,7 +589,7 @@
:database 1
:stages [{:lib/type :mbql.stage/mbql
:source-table 99999}]}]
- (is (nil? (repr.resolve/try-export-query mp bogus))))))
+ (is (nil? (repr.resolve/try-export-query mp bogus resolve.mp/unchecked-app-db-content-store))))))
(def ^:private source-card-export-query
{:lib/type :mbql/query
diff --git a/test/metabase/metabot/agent/user_context_test.clj b/test/metabase/metabot/agent/user_context_test.clj
index 42a31c8234a..64051772ac0 100644
--- a/test/metabase/metabot/agent/user_context_test.clj
+++ b/test/metabase/metabot/agent/user_context_test.clj
@@ -8,8 +8,10 @@
[metabase.metabot.agent.user-context :as user-context]
[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.test :as mt]
[toucan2.core :as t2]))
@@ -560,6 +562,80 @@
(is (str/includes? out "notebook editor"))
(is (not (str/includes? out "source-table"))))))))))
+(deftest adhoc-viewing-context-virtual-database-id-gates-real-database-test
+ (let [viewing (fn [card-id]
+ {:user_is_viewing [{:type "adhoc"
+ :query {:database -1337
+ :type "query"
+ :query {:source-table (str "card__" card-id)}}}]})]
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id}
+ :model/Card {card-id :id} {:database_id db-id
+ :dataset_query {:database db-id
+ :type :query
+ :query {:source-table table-id}}}]
+ (testing "the -1337 virtual database id is gated on the source card's real database"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-test-user :rasta
+ (let [out (user-context/format-viewing-context (viewing card-id))]
+ (is (str/includes? out "notebook editor"))
+ (is (not (str/includes? out (str "card__" card-id))))))))
+ (testing "and still renders for a user who can read that database"
+ (mt/with-test-user :crowberto
+ (let [out (user-context/format-viewing-context (viewing card-id))]
+ (is (str/includes? out "notebook editor"))
+ (is (re-find #"source-card|card__" out))))))))
+
+(deftest format-transform-source-denied-database-withholds-query-test
+ (testing "a transform source over a database the user cannot read renders no query body"
+ (mt/with-temp [:model/Database {db-id :id} {}]
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-test-user :rasta
+ (let [source {:type "query"
+ :query {:database db-id
+ :type :native
+ :native {:query "SELECT secret FROM t"}}}
+ text (user-context/format-transform-source
+ (assoc source :transform-source-type :native))]
+ (is (not (str/includes? (str text) "SELECT secret")))))))))
+
+(defn- refusing-store
+ "A ContentStore that records `tag` and refuses, the way the real stores do for a row the
+ current user cannot read. Swapped in for both so a test can tell which one a caller picked."
+ [tag recorded]
+ (let [refuse (fn [] (swap! recorded conj tag) (throw (ex-info "Forbidden" {:status-code 403})))]
+ (reify resolve.mp/ContentStore
+ (card-by-entity-id [_ _] (refuse))
+ (measure-by-entity-id [_ _] (refuse))
+ (segment-by-entity-id [_ _] (refuse))
+ (card-by-id [_ _] (refuse))
+ (measure-by-id [_ _] (refuse))
+ (segment-by-id [_ _] (refuse)))))
+
+(deftest adhoc-viewing-context-exports-through-the-audited-store-test
+ (testing "a client-supplied query is exported through the audited store, and a refusal inside it withholds the query"
+ (let [mp (mt/metadata-provider)
+ definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :venues :price)) 1)))]
+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :venues)
+ :definition definition}]
+ ;; The gate never looks at segments, so the query clears it and the segment ref is
+ ;; resolved by whichever store the caller handed the export - the choice under test.
+ ;; A source-card query would be refused by the gate first, whichever store was passed.
+ (let [used (atom [])]
+ (with-redefs [shared.content-store/audited-store (refusing-store :audited used)
+ shared.content-store/default-store (refusing-store :default used)]
+ (mt/with-test-user :rasta
+ (let [out (user-context/format-viewing-context
+ {:user_is_viewing [{:type "adhoc"
+ :query {:database (mt/id)
+ :type :query
+ :query {:source-table (mt/id :venues)
+ :filter [:segment segment-id]}}}]})]
+ (is (= [:audited] (distinct @used)))
+ (is (str/includes? out "notebook editor"))
+ (is (not (str/includes? out "Query")))))))))))
+
(deftest ^:parallel enrich-context-omits-research-plan-test
(testing "the draft Research plan is an explorations-only, system-prompt concern, so it must not
leak into the generic user-message injection context. message_injection.selmer has no
diff --git a/test/metabase/metabot/tools/construct_representations_test.clj b/test/metabase/metabot/tools/construct_representations_test.clj
index d8a64b5e1e4..0da10894974 100644
--- a/test/metabase/metabot/tools/construct_representations_test.clj
+++ b/test/metabase/metabot/tools/construct_representations_test.clj
@@ -10,6 +10,7 @@
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[clojure.walk :as walk]
+ [metabase.agent-lib.representations.repair :as repr.repair]
[metabase.api.common :as api]
[metabase.lib-be.core :as lib-be]
[metabase.lib.convert :as lib.convert]
@@ -1319,3 +1320,22 @@
(is (= 1 (count (get-in structured [:query :stages 0 :joins]))))
(is (some #(= 301 (:field_id %)) (:result-columns structured))
"CAMPAIGNS.NAME resolves from the surfaced artifacts"))))))))
+
+(deftest repair-runs-with-refusal-auditing-off-test
+ (testing "repair's source-card lookups are best-effort and the query is resolved for real afterwards,
+ so a refusal in there must not be audited as an access attempt"
+ (let [seen (atom ::never-called)
+ repair repr.repair/repair]
+ (with-redefs [repr.repair/repair (fn [& args]
+ (reset! seen resolve.mp/*audit-refusals?*)
+ (apply repair args))]
+ (with-mp-and-stubs!
+ (fn []
+ (construct/execute-representations-query
+ (query-data
+ {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-table" ["Sample" "PUBLIC" "ORDERS"]
+ "aggregation" [["count" {}]]}]})))))
+ (is (false? @seen)))))
diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.clj
index 8a77029a387..9b16bcb77fe 100644
--- a/test/metabase/metabot/tools/resources_test.clj
+++ b/test/metabase/metabot/tools/resources_test.clj
@@ -10,8 +10,10 @@
[metabase.metabot.tools :as metabot.tools]
[metabase.metabot.tools.resources :as read-resource]
[metabase.metabot.tools.shared :as tools.shared]
+ [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.query-processor :as qp]
[metabase.test :as mt]
[metabase.transforms.core :as transforms.core]
@@ -312,7 +314,10 @@
:charts {"chart-1" {:chart_id "chart-1"
:query_id "q-1"
:queries [query]
- :visualization_settings {:chart_type "line"}}}}})]
+ :visualization_settings {:chart_type "line"}}
+ "chart-2" {:chart_id "chart-2"
+ :queries [nil]
+ :visualization_settings {:chart_type "bar"}}}}})]
(testing "resolves a conversation chart to its chart type and exported query"
(let [result (read-resource/read-resource {:uris ["metabase://chart/chart-1"]})]
(is (=? {:resources [{:content {:structured-output map?}}]}
@@ -320,6 +325,11 @@
(is (str/includes? (:output result) "conversation-chart"))
(is (str/includes? (:output result) "Chart type: line"))
(is (str/includes? (:output result) "ORDERS"))))
+ (testing "a chart with no query says so instead of claiming a permission denial"
+ (let [result (read-resource/read-resource {:uris ["metabase://chart/chart-2"]})]
+ (is (str/includes? (:output result) "Chart type: bar"))
+ (is (str/includes? (:output result) "No query is attached to this chart."))
+ (is (not (str/includes? (:output result) "cannot read")))))
(testing "falls back to the queries state when the id is a query id"
(let [result (read-resource/read-resource {:uris ["metabase://chart/q-1"]})]
(is (str/includes? (:output result) "conversation-query"))))
@@ -331,6 +341,70 @@
(is (=? {:resources [{:error #"No chart or query with id 'nope'.*"}]}
(read-resource/read-resource {:uris ["metabase://chart/nope"]}))))))))
+(defn- refusing-store
+ "A ContentStore that records `tag` and refuses, the way the real stores do for a row the
+ current user cannot read. Swapped in for both so a test can tell which one a caller picked."
+ [tag recorded]
+ (let [refuse (fn [] (swap! recorded conj tag) (throw (ex-info "Forbidden" {:status-code 403})))]
+ (reify resolve.mp/ContentStore
+ (card-by-entity-id [_ _] (refuse))
+ (measure-by-entity-id [_ _] (refuse))
+ (segment-by-entity-id [_ _] (refuse))
+ (card-by-id [_ _] (refuse))
+ (measure-by-id [_ _] (refuse))
+ (segment-by-id [_ _] (refuse)))))
+
+(deftest read-conversation-chart-provenance-picks-audit-test
+ (let [mp (mt/metadata-provider)
+ definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :venues :price)) 1)))]
+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :venues)
+ :definition definition}]
+ (let [query (fn [] {:database (mt/id)
+ :type "query"
+ :query {:source-table (mt/id :venues)
+ :filter [:segment segment-id]}})
+ chart (fn [chart-id query-id] {:chart_id chart-id
+ :query_id query-id
+ :queries [(query)]
+ :visualization_settings {:chart_type "line"}})
+ ;; The gate never looks at segments, so the query clears it and the segment ref is
+ ;; resolved by whichever store the caller handed the export - the choice under test.
+ ;; A source-card query would be refused by the gate first, whichever store was passed.
+ store-used (fn [uri]
+ (let [used (atom [])]
+ (with-redefs [shared.content-store/audited-store (refusing-store :audited used)
+ shared.content-store/default-store (refusing-store :default used)]
+ (let [result (read-resource/read-resource {:uris [uri]})]
+ (is (str/includes? (:output result)
+ "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")}}})]
+ (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"]]
+ (is (= [:audited] (store-used uri)) uri)))
+ (testing "so does a chart create_chart minted for a client-supplied query, whose own id was never seeded"
+ (is (= [:audited] (store-used "metabase://chart/created-chart"))))
+ (testing "a tool-written chart refuses without an audit trail"
+ (is (= [:default] (store-used "metabase://chart/tool-chart"))))))))))
+
+(deftest read-conversation-query-deleted-database-still-renders-test
+ (testing "a state query whose database no longer exists renders its fallback instead of claiming a permission problem"
+ (binding [tools.shared/*memory-atom*
+ (atom {:state {:queries {"q-gone" {:database 999999999
+ :type "query"
+ :query {:source-table 1}}}}})]
+ (mt/with-test-user :rasta
+ (let [result (read-resource/read-resource {:uris ["metabase://query/q-gone"]})]
+ (is (not (str/includes? (:output result) "cannot read")))
+ (is (str/includes? (:output result) "source-table")))))))
+
(deftest read-transform-resource-test
(mt/with-premium-features #{:transforms-basic :hosting}
(mt/with-current-user (mt/user->id :crowberto)
diff --git a/test/metabase/metabot/tools/shared/content_store_test.clj b/test/metabase/metabot/tools/shared/content_store_test.clj
index 2cc9857cb39..cae67b24e64 100644
--- a/test/metabase/metabot/tools/shared/content_store_test.clj
+++ b/test/metabase/metabot/tools/shared/content_store_test.clj
@@ -1,27 +1,40 @@
(ns metabase.metabot.tools.shared.content-store-test
- "Unit tests for the read-checked ContentStore wrapper.
+ "Unit tests for the read-checked ContentStore wrapper and the query database/table gates.
- These tests pin two properties:
+ These tests pin four properties:
- 1. **Pass-through when no user is bound** — serdes import / background tasks / REPL runs
- must keep working without an authenticated user. The wrapper short-circuits
- `api/read-check` and returns the inner store's row unchanged.
+ 1. **Pass-through when no user is bound**: serdes import / background tasks / REPL runs
+ must keep working without an authenticated user. The wrapper short-circuits the
+ permission check and returns the inner store's row unchanged.
- 2. **Read-checked when a user is bound** — all six methods (symmetric across import- and
- export-direction lookups) route through `api/read-check`. A user without read
- perms gets a 403; the underlying store is consulted for the existence check, and
- unknown / `nil` returns pass through cleanly so the per-model resolver can emit its
- `:unknown-…` agent error."
+ 2. **Every method is checked when a user is bound**: a row the user cannot read never
+ reaches the caller, whichever direction it was looked up by. Unknown / `nil` returns pass
+ through cleanly either way, so the per-model resolver can emit its `:unknown-…` agent
+ error.
+
+ 3. **Only the audit trail varies**: the `-by-entity-id` methods use the audited
+ [[api/read-check]], the `-by-id` methods an unaudited [[mi/can-read?]] +
+ [[api/check-403]] unless `audited-by-id?` is set, and binding
+ [[resolve.mp/*audit-refusals?*]] to false suppresses auditing on all six.
+
+ 4. **`repair` binds that suppression on**: the `source-card` refusals its mini-resolve passes
+ catch and discard stay out of the audit trail."
(:require
[clojure.test :refer :all]
+ [metabase.agent-lib.representations.repair :as repr.repair]
[metabase.api.common :as api]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.test-util :as lib.tu]
[metabase.metabot.tools.shared.content-store :as shared.content-store]
+ [metabase.models.interface :as mi]
[metabase.models.serialization.resolve :as resolve]
[metabase.models.serialization.resolve.mp :as resolve.mp]
- [metabase.test :as mt]))
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.permissions-group :as perms-group]
+ [metabase.test :as mt]
+ [toucan2.protocols :as t2.protocols]))
(defn- record-store
"A fake ContentStore that returns the configured row for each of its six methods."
@@ -34,6 +47,31 @@
(measure-by-id [_ _id] measure-id)
(segment-by-id [_ _id] segment-id)))
+(def ^:dynamic ^:private *stub-can-read?*
+ "Controls the [[mi/can-read?]] answer for [[stub-row]]s below."
+ true)
+
+(defmethod mi/can-read? ::stub-model
+ ([_row] *stub-can-read?*)
+ ([_model _pk] *stub-can-read?*))
+
+(defn- stub-row
+ "Tag `row` so it dispatches to the [[::stub-model]] `can-read?` method above, instead of the
+ real per-model permission logic; `can-read?` is a multimethod, so it can't be redefined with
+ `mt/with-dynamic-fn-redefs`."
+ [row]
+ (with-meta row {`t2.protocols/model (fn [_] ::stub-model)
+ `t2.protocols/dispatch-value (fn [_] ::stub-model)}))
+
+(def ^:private all-lookups
+ "Every `ContentStore` method, paired with an argument of the right shape."
+ [[resolve.mp/card-by-entity-id "x"]
+ [resolve.mp/measure-by-entity-id "x"]
+ [resolve.mp/segment-by-entity-id "x"]
+ [resolve.mp/card-by-id 1]
+ [resolve.mp/measure-by-id 1]
+ [resolve.mp/segment-by-id 1]])
+
;;; ============================================================
;;; Pass-through: no user bound
;;; ============================================================
@@ -55,14 +93,12 @@
(is (= row (resolve.mp/segment-by-id gated 1))))))
(deftest nil-from-inner-store-passes-through-unchanged-test
- (testing "when the inner store returns nil (unknown id), the wrapper returns nil — never throws"
+ (testing "when the inner store returns nil (unknown id), the wrapper returns nil, never throws"
(let [empty-store (record-store {})
gated (shared.content-store/read-checked empty-store)]
- ;; nil result short-circuits the read-check branch in maybe-read-check, regardless of
- ;; whether a user is bound. This is critical: the per-model resolver functions
- ;; (`import-card-by-entity-id` etc.) translate `nil` into a clean `:unknown-card` /
- ;; `:unknown-measure` / `:unknown-segment` agent error, and a stray throw here would
- ;; turn that friendly diagnostic into a 500.
+ ;; The per-model resolver functions turn `nil` into a clean `:unknown-card` /
+ ;; `:unknown-measure` / `:unknown-segment` agent error, so a throw here would turn that
+ ;; diagnostic into a 500.
(binding [api/*current-user-id* 1]
(is (nil? (resolve.mp/card-by-entity-id gated "x")))
(is (nil? (resolve.mp/measure-by-entity-id gated "x")))
@@ -76,14 +112,10 @@
;;; ============================================================
(deftest applies-read-check-when-user-bound-test
- (testing (str "with api/*current-user-id* bound, all six methods route the inner row through "
- "`api/read-check`. Using a row that doesn't satisfy `can-read?` proves the "
- "check fires on each branch — we stub `api/read-check` to a recording fn so "
- "we don't need a real auth setup, and assert it was called with the row.")
+ (testing "with a user bound, the `-by-entity-id` methods route the row through `api/read-check`"
(let [calls (atom [])
row {:opaque :marker}
- store (record-store {:card row :measure-eid row :segment-eid row
- :card-id row :measure-id row :segment-id row})
+ store (record-store {:card row :measure-eid row :segment-eid row})
gated (shared.content-store/read-checked store)]
(mt/with-dynamic-fn-redefs [api/read-check (fn [obj] (swap! calls conj obj) obj)]
(binding [api/*current-user-id* 1]
@@ -96,39 +128,185 @@
(testing "segment-by-entity-id"
(is (= row (resolve.mp/segment-by-entity-id gated "x")))
(is (= row (last @calls))))
+ (testing "all three entity-id methods invoke read-check exactly once"
+ (is (= 3 (count @calls)))))))))
+
+(deftest applies-check-403-when-user-bound-test
+ (testing (str "with a user bound, the `-by-id` methods route the row through the unaudited "
+ "`mi/can-read?` + `api/check-403` pair instead, never `api/read-check`")
+ (let [row (stub-row {:opaque :marker})
+ store (record-store {:card-id row :measure-id row :segment-id row})
+ gated (shared.content-store/read-checked store)]
+ (mt/with-dynamic-fn-redefs [api/read-check (fn [& _]
+ (is false "api/read-check ran on a -by-id lookup"))]
+ (binding [api/*current-user-id* 1
+ *stub-can-read?* true]
(testing "card-by-id"
- (is (= row (resolve.mp/card-by-id gated 1)))
- (is (= row (last @calls))))
+ (is (= row (resolve.mp/card-by-id gated 1))))
(testing "measure-by-id"
- (is (= row (resolve.mp/measure-by-id gated 1)))
- (is (= row (last @calls))))
+ (is (= row (resolve.mp/measure-by-id gated 1))))
(testing "segment-by-id"
- (is (= row (resolve.mp/segment-by-id gated 1)))
- (is (= row (last @calls))))
- (testing "all six methods invoke read-check exactly once"
- (is (= 6 (count @calls)))))))))
+ (is (= row (resolve.mp/segment-by-id gated 1)))))))))
+
+(deftest audited-by-id-applies-read-check-test
+ (testing (str "an `audited-by-id?` wrapper routes the `-by-id` methods through "
+ "`api/read-check` too, for lookups driven by client-supplied queries")
+ (let [calls (atom [])
+ row {:opaque :marker}
+ store (record-store {:card-id row :measure-id row :segment-id row})
+ gated (shared.content-store/read-checked store true)]
+ (mt/with-dynamic-fn-redefs [api/read-check (fn [obj] (swap! calls conj obj) obj)]
+ (binding [api/*current-user-id* 1]
+ (is (= row (resolve.mp/card-by-id gated 1)))
+ (is (= row (resolve.mp/measure-by-id gated 1)))
+ (is (= row (resolve.mp/segment-by-id gated 1)))
+ (is (= 3 (count @calls))))))))
+
+(deftest suppressing-the-audit-trail-still-checks-every-method-test
+ (testing (str "with `*audit-refusals?*` bound off, every method (including the audited "
+ "`-by-entity-id` ones and an `audited-by-id?` store) still admits a readable "
+ "row and still 403s an unreadable one, without going through `api/read-check`")
+ (let [row (stub-row {:opaque :marker})]
+ (doseq [audited-by-id? [false true]]
+ (let [store (record-store {:card row :measure-eid row :segment-eid row
+ :card-id row :measure-id row :segment-id row})
+ gated (shared.content-store/read-checked store audited-by-id?)]
+ (mt/with-dynamic-fn-redefs [api/read-check (fn [& _]
+ (is false "api/read-check ran with auditing suppressed"))]
+ (binding [api/*current-user-id* 1
+ resolve.mp/*audit-refusals?* false]
+ (doseq [[lookup arg] all-lookups]
+ (binding [*stub-can-read?* true]
+ (is (= row (lookup gated arg))))
+ (binding [*stub-can-read?* false]
+ (try
+ (lookup gated arg)
+ (is false "expected throw")
+ (catch clojure.lang.ExceptionInfo e
+ (is (= 403 (:status-code (ex-data e)))))))))))))))
(deftest propagates-read-check-403-test
- (testing "if read-check throws (403), the wrapper propagates the exception unchanged"
+ (testing "if read-check throws (403), the entity-id (import-direction) branch propagates the exception unchanged"
(let [row {:opaque :marker}
- store (record-store {:card row :card-id row :measure-id row})
+ store (record-store {:card row})
gated (shared.content-store/read-checked store)]
(mt/with-dynamic-fn-redefs [api/read-check (fn [_]
(throw (ex-info "Forbidden" {:status-code 403})))]
(binding [api/*current-user-id* 1]
- (testing "import-direction branch"
+ (try
+ (resolve.mp/card-by-entity-id gated "x")
+ (is false "expected throw")
+ (catch clojure.lang.ExceptionInfo e
+ (is (= 403 (:status-code (ex-data e))))))))))
+ (testing "if can-read? is false, the by-id (export-direction) branch throws a 403 via check-403"
+ (let [row (stub-row {:opaque :marker})
+ store (record-store {:card-id row :measure-id row :segment-id row})
+ gated (shared.content-store/read-checked store)]
+ (mt/with-dynamic-fn-redefs [api/read-check (fn [& _]
+ (is false "api/read-check ran on a -by-id lookup"))]
+ (binding [api/*current-user-id* 1
+ *stub-can-read?* false]
+ (doseq [lookup [resolve.mp/card-by-id resolve.mp/measure-by-id resolve.mp/segment-by-id]]
(try
- (resolve.mp/card-by-entity-id gated "x")
+ (lookup gated 1)
(is false "expected throw")
(catch clojure.lang.ExceptionInfo e
- (is (= 403 (:status-code (ex-data e)))))))
- (testing "export-direction lookups"
- (doseq [lookup [resolve.mp/card-by-id resolve.mp/measure-by-id]]
- (try
- (lookup gated 1)
- (is false "expected throw")
- (catch clojure.lang.ExceptionInfo e
- (is (= 403 (:status-code (ex-data e)))))))))))))
+ (is (= 403 (:status-code (ex-data e))))))))))))
+
+;;; ============================================================
+;;; Suppression at the repair call site
+;;; ============================================================
+
+(def ^:private source-card-entity-id
+ "A syntactically valid entity_id, so the resolver reaches the store instead of short-circuiting."
+ "GRLHTBIcE5nGFVJoxGr5D")
+
+(deftest suppressed-refusals-during-repair-leave-no-audit-trail-test
+ (testing "repair resolves a source-card ref through the store, and with auditing suppressed - as
+ metabase.metabot.tools.construct suppresses it around the repair call - a 403 there
+ leaves no trail"
+ (let [lookups (atom 0)
+ row (stub-row {:id 1 :database_id 1 :entity_id source-card-entity-id})
+ store (reify resolve.mp/ContentStore
+ (card-by-entity-id [_ _eid] (swap! lookups inc) row)
+ (measure-by-entity-id [_ _eid] nil)
+ (segment-by-entity-id [_ _eid] nil)
+ (card-by-id [_ _id] nil)
+ (measure-by-id [_ _id] nil)
+ (segment-by-id [_ _id] nil))
+ mp (lib.tu/mock-metadata-provider {:database {:id 1 :name "Sample"}})
+ query {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-card" source-card-entity-id}]}]
+ (mt/with-dynamic-fn-redefs [api/read-check (fn [& _]
+ (is false "api/read-check ran on a refusal repair discards"))]
+ (binding [api/*current-user-id* 1
+ *stub-can-read?* false
+ resolve.mp/*audit-refusals?* false]
+ (let [repaired (repr.repair/repair mp query (shared.content-store/read-checked store))]
+ (is (pos? @lookups) "precondition: repair looked the source card up")
+ (is (= source-card-entity-id (get-in repaired ["stages" 0 "source-card"]))
+ "the refused lookup is skipped, not fatal")))))))
+
+;;; ============================================================
+;;; Database / table gates
+;;; ============================================================
+
+(deftest query-gates-are-table-granular-test
+ (testing "a query whose database is readable is still withheld when it references a table the user cannot query"
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
+ (mt/with-test-user :rasta
+ (let [query-on (fn [table-id]
+ {:database (mt/id)
+ :type :query
+ :query {:source-table table-id}})]
+ (is (mi/can-read? :model/Database (mt/id))
+ "precondition: query access to one table makes the whole database readable")
+ (doseq [audited? [true false]]
+ (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 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!
+ (perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
+ (let [native-query {:database (mt/id)
+ :type :native
+ :native {:query "SELECT * FROM orders"}}]
+ (mt/with-test-user :rasta
+ (doseq [audited? [true false]]
+ (is (nil? (shared.content-store/query-for-export native-query audited?)))))
+ (perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)
+ (mt/with-test-user :rasta
+ (doseq [audited? [true false]]
+ (is (some? (shared.content-store/query-for-export native-query audited?)))))))))
+
+(deftest saved-question-source-authorizes-through-its-collection-test
+ (testing "a query on a saved question the user can read is exported without query access to its database"
+ (mt/with-temp [:model/Card {card-id :id}
+ {:dataset_query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))}]
+ (let [query {:database (mt/id)
+ :type :query
+ :query {:source-table (str "card__" card-id)}}]
+ (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)))
+ "precondition: nothing grants query access to the database")
+ (doseq [audited? [true false]]
+ (is (some? (shared.content-store/query-for-export query audited?))))))
+ (testing "and with no permission on that database at all, since running the card is what authorizes it"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-test-user :rasta
+ (is (not (mi/can-read? :model/Database (mt/id)))
+ "precondition: the database is not even readable")
+ (doseq [audited? [true false]]
+ (is (some? (shared.content-store/query-for-export query audited?)))))))))))
;;; ============================================================
;;; default-store integration shape
diff --git a/test/metabase/query_permissions/impl_test.clj b/test/metabase/query_permissions/impl_test.clj
index 3167ef540ea..7da4d82ea86 100644
--- a/test/metabase/query_permissions/impl_test.clj
+++ b/test/metabase/query_permissions/impl_test.clj
@@ -368,3 +368,20 @@
:perms/create-queries {(mt/id :products) :query-builder}
:perms/view-data {(mt/id :products) :unrestricted}}
(query-perms/required-perms-for-query query :already-preprocessed? true)))))))
+
+(deftest can-run-query?-non-permission-errors-test
+ (let [broken {:database (mt/id) :type :query :query {:source-table "card__13371337"}}]
+ (testing "a query whose permissions cannot be calculated throws when the caller asks for it"
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Error calculating permissions"
+ (query-perms/can-run-query? broken false true)))))
+ (testing "without the flag the calculation failure stays folded into the permission answer"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (false? (query-perms/can-run-query? broken)))))))
+ (testing "a real permission denial reads as false in either mode"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [query (mt/mbql-query orders)]
+ (is (false? (query-perms/can-run-query? query)))
+ (is (false? (query-perms/can-run-query? query false true))))))))