Skip to content

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

Optimize permission checks for Explorations summary updates (#85167)

John Swansonfa2af600bd76
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index ff293fca62d..434c5f2f3f5 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2292,7 +2292,7 @@
query-permissions
{:team "Security Engineering"
- :api #{metabase.query-permissions.core}
+ :api #{metabase.query-permissions.core metabase.query-permissions.schema}
:uses #{api
lib
lib.be
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/explorations_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/explorations_test.clj
index 033fa19fbe2..d6fe2d3dd76 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/explorations_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/explorations_test.clj
@@ -453,8 +453,8 @@
(data-access-token/routing-token-for-db (mt/id))))))))
(deftest resolved-table-ids-skip-viewer-routing-lens-test
- (testing "resolving a query's table footprint must not resolve the *viewer's* routing lens.
- Footprint resolution preprocesses as admin, but do-as-admin keeps the user's id and
+ (testing "resolving a query's source ids must not resolve the *viewer's* routing lens.
+ Resolving them preprocesses as admin, but do-as-admin keeps the user's id and
attributes, so without `preprocess-without-per-user-lens` the routing preprocess
middleware resolves this user's destination db and throws (no :database-routing
feature) — and the read gate would deny every same-lens viewer"
@@ -464,7 +464,7 @@
(sandbox.tu/with-user-attributes! :rasta {"db_name" "sr-dest-a"}
(mt/with-test-user :rasta
(is (= #{(mt/id :venues)}
- (query-perms/query->resolved-source-table-ids (venues-count-query))))))))))
+ (:table-ids (query-perms/query->resolved-source-ids (venues-count-query)))))))))))
(deftest routing-gate-end-to-end-test
(testing "the routing lens gates cached exploration results per destination database"
diff --git a/resources/migrations/2026/20261021_exploration_resolved_source_ids.yaml b/resources/migrations/2026/20261021_exploration_resolved_source_ids.yaml
new file mode 100644
index 00000000000..212dbe3ccb9
--- /dev/null
+++ b/resources/migrations/2026/20261021_exploration_resolved_source_ids.yaml
@@ -0,0 +1,30 @@
+databaseChangeLog:
+ - changeSet:
+ id: mc21hy
+ author: johnswanson
+ comment: Add exploration_query.resolved_source_ids
+ changes:
+ - addColumn:
+ tableName: exploration_query
+ columns:
+ - column:
+ name: resolved_source_ids
+ type: ${text.type}
+ remarks: JSON-encoded tables and cards the query reads, resolved when its data_access_token was stamped. NULL means resolve it from dataset_query.
+ constraints:
+ nullable: true
+
+ - changeSet:
+ id: t0u3ct
+ author: johnswanson
+ comment: Add exploration_thread.resolved_source_ids
+ changes:
+ - addColumn:
+ tableName: exploration_thread
+ columns:
+ - column:
+ name: resolved_source_ids
+ type: ${text.type}
+ remarks: JSON-encoded tables and cards the thread's metric card reads, resolved when its data_access_token was stamped. NULL means resolve it from the card.
+ constraints:
+ nullable: true
diff --git a/resources/migrations/2026/20261028_stored_result_resolved_source_ids.yaml b/resources/migrations/2026/20261028_stored_result_resolved_source_ids.yaml
new file mode 100644
index 00000000000..0f3058e3c46
--- /dev/null
+++ b/resources/migrations/2026/20261028_stored_result_resolved_source_ids.yaml
@@ -0,0 +1,16 @@
+databaseChangeLog:
+ - changeSet:
+ id: ir3rm3
+ author: johnswanson
+ comment: Add stored_result.resolved_source_ids
+ changes:
+ - addColumn:
+ tableName: stored_result
+ columns:
+ - column:
+ name: resolved_source_ids
+ type: ${text.type}
+ remarks: >-
+ JSON source ids resolved from dataset_query, with its digest. NULL means resolve live.
+ constraints:
+ nullable: true
diff --git a/src/metabase/documents/api/document.clj b/src/metabase/documents/api/document.clj
index 49a2a436cd9..588196a8393 100644
--- a/src/metabase/documents/api/document.clj
+++ b/src/metabase/documents/api/document.clj
@@ -55,6 +55,19 @@
[:cards {:optional true} [:maybe (cards-to-create-schema :int)]]
[:archived {:optional true} [:maybe :boolean]]])
+(defn- insert-card-embed!
+ "Persist an embed for `card-id` into document `document-id`, re-reading the document inside the
+ transaction so a concurrent edit cannot be overwritten. Returns the updated row, unchecked."
+ [document-id card-id position extra-attrs]
+ (t2/with-transaction [_conn]
+ (let [document (api/check-404 (documents.db/document document-id))
+ updated (prose-mirror/insert-card-embed document card-id position extra-attrs)
+ updates (cond-> (select-keys updated [:document])
+ (:is_placeholder document) (assoc :is_placeholder false))]
+ (documents.db/update-document! document-id updates)
+ (collections/check-for-remote-sync-update document)
+ (merge document updates))))
+
(defn add-card-to-document!
"Insert an embed for the already-created card with `card-id` into the prose-mirror ast of the
document with `document-id` and persist it. `position` is a 0-based index among the document's
@@ -66,17 +79,17 @@
Adding a card clears `:is_placeholder` when it was set. The caller is responsible for
write-checking the document first. The document is re-read inside the transaction so a
- concurrent edit cannot be overwritten. Returns the updated document."
+ concurrent edit cannot be overwritten. Returns the updated document, hydrated as by
+ [[m.document/get-document]]; [[add-card-to-document-row!]] skips the hydration."
[document-id card-id position & {:keys [extra-attrs]}]
- (t2/with-transaction [_conn]
- (let [document (api/check-404 (documents.db/document document-id))
- updated (prose-mirror/insert-card-embed document card-id position extra-attrs)
- updates (cond-> (select-keys updated [:document])
- (:is_placeholder document) (assoc :is_placeholder false))]
- (documents.db/update-document! document-id updates)
- (collections/check-for-remote-sync-update document)))
+ (insert-card-embed! document-id card-id position extra-attrs)
(m.document/get-document document-id :log-view? false))
+(defn add-card-to-document-row!
+ "[[add-card-to-document!]] for callers that don't need the hydrated document result."
+ [document-id card-id position & {:keys [extra-attrs]}]
+ (api/read-check (insert-card-embed! document-id card-id position extra-attrs)))
+
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
@@ -365,6 +378,12 @@
:pivot? pivot-results?
:js-int-to-string? false}))
+(defn- +content-gate-cache
+ [handler]
+ (fn [request respond raise]
+ (m.document/with-content-gate-cache
+ (handler request respond raise))))
+
(def ^{:arglists '([request respond raise])} routes
"`/api/document/` routes."
- (api.macros/ns-handler *ns* +auth))
+ (api.macros/ns-handler *ns* +auth +content-gate-cache))
diff --git a/src/metabase/documents/core.clj b/src/metabase/documents/core.clj
index 2ffc8b3bcd7..1f49aaca624 100644
--- a/src/metabase/documents/core.clj
+++ b/src/metabase/documents/core.clj
@@ -17,6 +17,7 @@
(p/import-vars
[metabase.documents.api.document
add-card-to-document!
+ add-card-to-document-row!
copy-document!]
[metabase.documents.markdown
contains-table?
diff --git a/src/metabase/documents/models/document.clj b/src/metabase/documents/models/document.clj
index b54e98662d8..354405200f4 100644
--- a/src/metabase/documents/models/document.clj
+++ b/src/metabase/documents/models/document.clj
@@ -83,7 +83,7 @@
#{})
#_{:clj-kondo/ignore [:metabase/discourage-dynamic-vars]}
-(def ^:dynamic *cache*
+(def ^:dynamic *content-gate-cache*
"Cache atom bound by [[with-content-gate-cache]], or nil to adjudicate on every call."
nil)
@@ -97,7 +97,7 @@
such a loop; everywhere else reads through."
{:style/indent 0}
[& body]
- `(binding [*cache* (or *cache* (atom {}))]
+ `(binding [*content-gate-cache* (or *content-gate-cache* (atom {}))]
~@body))
(defn content-visible?
@@ -114,11 +114,11 @@
false
(binding [*content-gate-pending* (cond-> *content-gate-pending* id (conj id))]
(boolean (@doc-content-visibility-fn document)))))]
- (if (and id *cache*)
- (if-some [cached (get @*cache* k)]
+ (if (and id *content-gate-cache*)
+ (if-some [cached (get @*content-gate-cache* k)]
cached
(let [verdict (adjudge)]
- (swap! *cache* assoc k verdict)
+ (swap! *content-gate-cache* assoc k verdict)
verdict))
(adjudge))))
diff --git a/src/metabase/explorations/api.clj b/src/metabase/explorations/api.clj
index 242bce23832..b4f999e0eef 100644
--- a/src/metabase/explorations/api.clj
+++ b/src/metabase/explorations/api.clj
@@ -146,6 +146,10 @@
(attach-thread-read-data thread blocks pages card-name-by-id)))
threads)))
+(defn- strip-lens-stamp
+ [m]
+ (dissoc m :data_access_token :resolved_source_ids))
+
(defn- gate-threads-derived-data
"Drop every thread's derived read-data — its queries, the block/page tree built from them, and the
thread name — when the current user's data-access lens isn't compatible with the creator's lens
@@ -163,9 +167,10 @@
;; but it still enumerates which tables the creator is sandboxed on and which databases
;; are impersonated or routed — and a response schema validates and encodes without
;; stripping (see [[redact-query-error]]), so it has to come off here.
- (dissoc (or (get enriched (:id thread))
- (assoc thread :queries [] :blocks [] :name nil :status "forbidden"))
- :data_access_token))
+ (-> (or (get enriched (:id thread))
+ (assoc thread :queries [] :blocks [] :name nil :status "forbidden"))
+ strip-lens-stamp
+ (u/update-if-exists :queries #(some->> % (mapv strip-lens-stamp)))))
threads)))
(defn- thread-status
@@ -654,20 +659,24 @@
{:object persisted :user-id api/*current-user-id*})
(hydrate-exploration persisted)))
-(defn- drill-thread-token
- "The lens stamp for the thread `explore-further` creates.
+(defn- drill-thread-stamp
+ "The lens stamp for the thread `explore-further` creates. Its `:resolved_source_ids` is the union of the block's
+ metric cards', so the gate holds a viewer to every table the token was computed over.
- Computed under the *creator's* identity rather than the caller's. The values this thread is named
+ The token is computed under the *creator's* identity rather than the caller's. The values this thread is named
for were read out of results the creator's lens produced, and its queries will execute under that
same identity (see [[metabase.explorations.runner]]), which is the lens `finalize-row!` stamps them
with."
[exploration cards]
- (request/with-current-user (:creator_id exploration)
- (perms/data-access-token
- {:database-id (:database_id (first cards))
- :table-ids (into #{}
- (mapcat #(query-perms/query->resolved-source-table-ids (:dataset_query %)))
- cards)})))
+ (let [resolved-source-ids (query-perms/merge-resolved-source-ids
+ (map #(query-perms/query->resolved-source-ids (:dataset_query %)) cards))]
+ {:data_access_token (request/with-current-user (:creator_id exploration)
+ (perms/data-access-token
+ {:database-id (:database_id (first cards))
+ :table-ids (:table-ids resolved-source-ids)}))
+ ;; The content gate checks the thread against the first card's query.
+ :resolved_source_ids (query-perms/stamped-resolved-source-ids (:dataset_query (first cards))
+ resolved-source-ids)}))
(api.macros/defendpoint :post "/:id/explore-further" :- ::HydratedExploration
"Start a follow-up investigation scoped to a clicked chart segment.
@@ -717,15 +726,15 @@
next-position (inc (or (explorations.db/last-thread-position id) 0))]
(t2/with-transaction [_]
(let [thread (explorations.db/insert-thread!
- {:exploration_id id
- :name (explore-further-thread-name card-name
- enriched-filters
- top-level-follow-up?)
- :data_access_token (drill-thread-token exploration cards)
- :position next-position
- ;; drill lineage — lets the sidebar nest this thread
- ;; under the one owning the drilled page
- :source_page_id page_id})
+ (merge {:exploration_id id
+ :name (explore-further-thread-name card-name
+ enriched-filters
+ top-level-follow-up?)
+ :position next-position
+ ;; drill lineage — lets the sidebar nest this thread
+ ;; under the one owning the drilled page
+ :source_page_id page_id}
+ (drill-thread-stamp exploration cards)))
tid (:id thread)]
(explorations.db/insert-blocks! {:exploration_thread_id tid
:metrics metrics'
@@ -1037,7 +1046,7 @@
"child_target_id" (str page-id)
"host_data" (cond-> {:query_ids exploration_query_ids}
explore-filters (assoc :explore_filters explore-filters))}]
- (documents/add-card-to-document!
+ (documents/add-card-to-document-row!
(:id doc) card-id nil
:extra-attrs extra-attrs)))
(document-summary (:id doc))))
diff --git a/src/metabase/explorations/db.clj b/src/metabase/explorations/db.clj
index 267d83e5b58..8de1e8ad6e2 100644
--- a/src/metabase/explorations/db.clj
+++ b/src/metabase/explorations/db.clj
@@ -170,9 +170,9 @@
(t2/select-pks-set :model/ExplorationThread :exploration_id exploration-id))
(mu/defn lens-stamped-threads
- "The ID and data-access token of the ExplorationThreads among `thread-ids` that carry a token."
+ "The ExplorationThreads among `thread-ids` that carry a data-access token, with what the content gate reads."
[thread-ids :- [:set ms/PositiveInt]]
- (t2/select [:model/ExplorationThread :id :data_access_token]
+ (t2/select [:model/ExplorationThread :id :data_access_token :resolved_source_ids]
:id [:in thread-ids]
:data_access_token [:not= nil]))
@@ -432,10 +432,11 @@
{:order-by [[:position :asc] [:id :asc]]}))
(mu/defn lens-stamped-queries
- "The ID, thread, Database, query, and data-access token of the ExplorationQueries with a query on the
- ExplorationThreads with `thread-ids`, in ID order."
+ "The ExplorationQueries with a query on the ExplorationThreads with `thread-ids`, with what the content gate reads,
+ in ID order."
[thread-ids :- [:set ms/PositiveInt]]
- (t2/select [:model/ExplorationQuery :id :exploration_thread_id :database_id :dataset_query :data_access_token]
+ (t2/select [:model/ExplorationQuery :id :exploration_thread_id :database_id :dataset_query :data_access_token
+ :resolved_source_ids]
:exploration_thread_id [:in thread-ids]
:dataset_query [:not= nil]
{:order-by [[:id :asc]]}))
@@ -500,7 +501,11 @@
(mu/defn update-query!
"Apply `changes` to the ExplorationQuery with `query-id`."
[query-id :- ms/PositiveInt
- changes :- (mut/select-keys ::explorations.schema/exploration-query.update [:dataset_query :name :data_access_token :status :started_at :finished_at])]
+ changes :- [:and
+ (mut/select-keys ::explorations.schema/exploration-query.update
+ [:dataset_query :name :data_access_token :resolved_source_ids
+ :status :started_at :finished_at])
+ ::explorations.schema/exploration-query.update.stamped-together]]
(t2/update! :model/ExplorationQuery :id query-id changes))
(mu/defn fail-pending-query!
diff --git a/src/metabase/explorations/derived_perms.clj b/src/metabase/explorations/derived_perms.clj
index 04827c11b17..d98dcd631b2 100644
--- a/src/metabase/explorations/derived_perms.clj
+++ b/src/metabase/explorations/derived_perms.clj
@@ -22,6 +22,7 @@
[metabase.api.common :as api]
[metabase.explorations.db :as explorations.db]
[metabase.lib-be.core :as lib-be]
+ [metabase.permissions.core :as perms]
[metabase.queries.core :as queries]
[metabase.query-permissions.core :as query-perms]
[metabase.util :as u]))
@@ -62,11 +63,13 @@
`resolve-source-ids` is the resolving half, passed in so a batch can share one memo — resolution is
a pure function of the query (it runs as admin with the per-user lens skipped), so repeats are free
to collapse."
- [resolve-source-ids {:keys [database_id dataset_query data_access_token]}]
+ [resolve-source-ids {:keys [database_id dataset_query data_access_token resolved_source_ids]}]
(let [source-ids (try
(some-> dataset_query query-perms/query->source-ids)
(catch Throwable _ ::unprojectable))]
- [database_id data_access_token (resolve-source-ids dataset_query)
+ [database_id data_access_token
+ (or (query-perms/verified-resolved-source-ids dataset_query resolved_source_ids)
+ (resolve-source-ids dataset_query))
(cond
(= ::unprojectable source-ids) ::unprojectable
(or (seq (:table-ids source-ids)) (seq (:card-ids source-ids))) source-ids
@@ -119,7 +122,8 @@
:exploration_thread_id thread-id
:database_id (:database_id card)
:dataset_query (:dataset_query card)
- :data_access_token (:data_access_token thread)}
+ :data_access_token (:data_access_token thread)
+ :resolved_source_ids (:resolved_source_ids thread)}
;; The Card is what the stamp was computed over, and it does not outlive a delete:
;; both `exploration_query.card_id` and `exploration_page.card_id` cascade, so
;; deleting it strips every other adjudicable row while the thread's name survives.
@@ -131,13 +135,13 @@
"The gate's verdict for one artifact.
An artifact marked [[::indeterminate]] has no query left to adjudicate, so the gate cannot be asked
- (it throws without a `:dataset_query`). It denies instead, leaving only the superuser exemption the
- gate itself would have applied — the same fail-closed choice [[source-ids-resolver]] makes for a
- query it cannot resolve."
- [artifact]
+ (it throws without a `:dataset_query`). It denies instead — the same fail-closed choice
+ [[source-ids-resolver]] makes for a query it cannot resolve. Callers exempt superusers before
+ reaching here; the superuser check keeps the gate's own answer if this is reached directly."
+ [data-access-token-reader artifact]
(if (::indeterminate artifact)
(boolean api/*is-superuser?*)
- (queries/viewer-can-view-cached-result? artifact)))
+ (queries/viewer-can-view-cached-result? artifact {:data-access-token-reader data-access-token-reader})))
(defn- blocked-thread-ids
"Thread ids with at least one artifact the current user may not see.
@@ -145,19 +149,20 @@
Artifacts sharing a [[visibility-key]] share a verdict by construction, so grouping by it lets
the expensive check run once per group rather than once per artifact."
[artifacts]
- (->> (group-by (partial visibility-key (source-ids-resolver)) artifacts)
- (remove (fn [[_key [representative]]]
- (artifact-visible? representative)))
- (mapcat val)
- (map :exploration_thread_id)
- set))
+ (let [data-access-token-reader (perms/data-access-token-reader)]
+ (->> (group-by (partial visibility-key (source-ids-resolver)) artifacts)
+ (remove (fn [[_key [representative]]]
+ (artifact-visible? data-access-token-reader representative)))
+ (mapcat val)
+ (map :exploration_thread_id)
+ set)))
(defn thread-ids-with-visible-derived-data
"The subset of `thread-ids` whose derived read-data the current user may see: every row carrying
text derived from warehouse values must pass the gate
([[metabase.queries.cached-result/viewer-can-view-cached-result?]]) for the *current* user — the
creator included. A thread carrying no such text yet stays visible, because there is nothing on it
- that was computed under anyone's lens. Returns a set.
+ that was computed under anyone's lens. A superuser sees every thread. Returns a set.
Held inside one metadata-provider cache: [[visibility-key]] preprocesses each artifact's query, and
a thread's charts are variants over the same card, so they read the same tables and fields over and
@@ -165,8 +170,10 @@
caller that doesn't."
[thread-ids]
(let [thread-ids (set thread-ids)]
- (if (empty? thread-ids)
- #{}
+ (cond
+ (empty? thread-ids) #{}
+ api/*is-superuser?* thread-ids
+ :else
(lib-be/with-metadata-provider-cache
(set/difference thread-ids
(blocked-thread-ids (concat (finalized-queries thread-ids)
@@ -175,7 +182,8 @@
(defn- exploration-content-visible?
"The gate's verdict for a document owned by `exploration-id` (nil => not a Summary, unaffected)."
[exploration-id]
- (if (nil? exploration-id)
+ ;; The superuser check only skips the thread-id read; the gate would pass them anyway.
+ (if (or (nil? exploration-id) api/*is-superuser?*)
true
(let [thread-ids (explorations.db/thread-ids-for-exploration exploration-id)]
(or (empty? thread-ids)
diff --git a/src/metabase/explorations/models/exploration_query.clj b/src/metabase/explorations/models/exploration_query.clj
index 3007ccbcf5c..8a05197d322 100644
--- a/src/metabase/explorations/models/exploration_query.clj
+++ b/src/metabase/explorations/models/exploration_query.clj
@@ -3,6 +3,7 @@
[metabase.explorations.db :as explorations.db]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
+ [metabase.query-permissions.core :as query-perms]
[metabase.util :as u]
[methodical.core :as methodical]
[toucan2.core :as t2]))
@@ -18,7 +19,8 @@
{:visualization_settings mi/transform-json
:dataset_query mi/transform-json
:params mi/transform-json
- :data_access_token perms/data-access-token-transform})
+ :data_access_token perms/data-access-token-transform
+ :resolved_source_ids query-perms/resolved-source-ids-transform})
(defmethod mi/can-read? :model/ExplorationQuery
([instance]
diff --git a/src/metabase/explorations/models/exploration_query_result.clj b/src/metabase/explorations/models/exploration_query_result.clj
index 42c3787d9f8..c16d38d62fa 100644
--- a/src/metabase/explorations/models/exploration_query_result.clj
+++ b/src/metabase/explorations/models/exploration_query_result.clj
@@ -96,6 +96,17 @@
{:status-code 400})))
(first tokens)))
+(defn- composite-resolved-source-ids
+ "The union of every source's resolved source ids, stamped from `dataset-query`, the query the composite row carries.
+ A source with no verified stamp is resolved live."
+ [dataset-query eq-results]
+ (query-perms/stamped-resolved-source-ids
+ dataset-query
+ (query-perms/merge-resolved-source-ids
+ (map (fn [{:keys [sr]}]
+ (query-perms/query->resolved-source-ids (:dataset_query sr) (:resolved_source_ids sr)))
+ eq-results))))
+
(defn create-ephemeral-card-for-exploration-queries!
"Materialize an ephemeral `report_card` that represents a *composite chart* — possibly
built from multiple `ExplorationQuery` snapshots combined into one — for a single document
@@ -158,7 +169,8 @@
:row_count (:row_count composite-qp)
;; Without this the read gate denies the composite to
;; every non-superuser. See [[composite-data-access-token]].
- :data_access_token (composite-data-access-token eq-results)}))
+ :data_access_token (composite-data-access-token eq-results)
+ :resolved_source_ids (composite-resolved-source-ids dataset-query eq-results)}))
card-id (:id (queries/create-card!
{:name (or (not-empty (:name first-eq))
(not-empty (:name src-card))
diff --git a/src/metabase/explorations/models/exploration_thread.clj b/src/metabase/explorations/models/exploration_thread.clj
index 4f9134ef25e..7702f7cb646 100644
--- a/src/metabase/explorations/models/exploration_thread.clj
+++ b/src/metabase/explorations/models/exploration_thread.clj
@@ -4,6 +4,7 @@
[metabase.explorations.schema :as explorations.schema]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
+ [metabase.query-permissions.core :as query-perms]
[methodical.core :as methodical]
[toucan2.core :as t2]))
@@ -17,7 +18,8 @@
(t2/deftransforms :model/ExplorationThread
{:query_plan_transcript (mi/transform-json-with-schema "exploration_thread.query_plan_transcript"
::explorations.schema/exploration-thread.query-plan-transcript)
- :data_access_token perms/data-access-token-transform})
+ :data_access_token perms/data-access-token-transform
+ :resolved_source_ids query-perms/resolved-source-ids-transform})
(defmethod mi/can-read? :model/ExplorationThread
([instance]
diff --git a/src/metabase/explorations/runner.clj b/src/metabase/explorations/runner.clj
index d860257179d..8fc7dd09184 100644
--- a/src/metabase/explorations/runner.clj
+++ b/src/metabase/explorations/runner.clj
@@ -84,13 +84,13 @@
(result-fn))))
(defn- compute-data-access-token
- "The creator's effective-data-access token for `dataset-query` — the sandbox/impersonation/routing
- fingerprint the query is computed under, stored on the `StoredResult` and on the
- `ExplorationQuery` itself, and compared against a viewer's token to gate reads. Must be called
- inside the creator's `with-current-user` (+ routing-on) binding."
- [dataset-query db-id]
+ "The creator's effective-data-access token for a query over Database `db-id` with `resolved-source-ids` (its
+ [[query-perms/query->resolved-source-ids]]) — the sandbox/impersonation/routing fingerprint the query is computed
+ under, stored on the `StoredResult` and on the `ExplorationQuery` itself, and compared against a viewer's token
+ to gate reads. Must be called inside the creator's `with-current-user` (+ routing-on) binding."
+ [db-id resolved-source-ids]
(perms/data-access-token {:database-id db-id
- :table-ids (query-perms/query->resolved-source-table-ids dataset-query)}))
+ :table-ids (:table-ids resolved-source-ids)}))
(defn- finalize-row!
"If `row` carries a nil `:dataset_query` (planner deferred the MBQL build),
@@ -113,9 +113,15 @@
(when (nil? dq)
(throw (ex-info "Could not build dataset_query for row (discovery returned no values?)"
{:row-id (:id row) :variant variant})))
- (let [token (compute-data-access-token dq (:database_id row))]
- (explorations.db/update-query! (:id row) {:dataset_query dq :name nm :data_access_token token})
- (assoc row :dataset_query dq :name nm :data_access_token token))))))
+ (let [resolved-source-ids (query-perms/query->resolved-source-ids dq)
+ stamp {:dataset_query dq
+ :name nm
+ :data_access_token (compute-data-access-token (:database_id row)
+ resolved-source-ids)
+ :resolved_source_ids (query-perms/stamped-resolved-source-ids dq
+ resolved-source-ids)}]
+ (explorations.db/update-query! (:id row) stamp)
+ (merge row stamp))))))
(defn- safe-chart-config
"Best-effort `qp-result->chart-config`. Returns nil on failure (>2 cols,
@@ -294,37 +300,40 @@
lens is captured as a `:data_access_token` so non-creator readers can be gated against it."
[row creator-id]
(let [db-id (:database_id row)
- qp-result (qp.variants/pin-other-last
- (:query_type row)
- (qp/process-query
- (qp/userland-query-with-default-constraints
- (:dataset_query row)
- {:context :exploration})))
- token (compute-data-access-token (:dataset_query row) db-id)
- chart-config (safe-chart-config row qp-result)
- stats (safe-deep-stats row chart-config)]
- {:creator-id creator-id
- :db-id db-id
- :bytes (serialize-result qp-result)
- :row-count (:row_count qp-result)
- :token token
- :stats stats
- :score (safe-score row chart-config stats)
- :ctx (safe-score+describe row chart-config stats creator-id)}))
+ qp-result (qp.variants/pin-other-last
+ (:query_type row)
+ (qp/process-query
+ (qp/userland-query-with-default-constraints
+ (:dataset_query row)
+ {:context :exploration})))
+ resolved-source-ids (query-perms/query->resolved-source-ids (:dataset_query row) (:resolved_source_ids row))
+ token (compute-data-access-token db-id resolved-source-ids)
+ chart-config (safe-chart-config row qp-result)
+ stats (safe-deep-stats row chart-config)]
+ {:creator-id creator-id
+ :db-id db-id
+ :bytes (serialize-result qp-result)
+ :row-count (:row_count qp-result)
+ :token token
+ :resolved-source-ids (query-perms/stamped-resolved-source-ids (:dataset_query row) resolved-source-ids)
+ :stats stats
+ :score (safe-score row chart-config stats)
+ :ctx (safe-score+describe row chart-config stats creator-id)}))
(defn- persist-query-result!
"Write what [[compute-query-result]] produced and flip the query to `done` transactionally.
Returns false when a peer delivery already persisted this query."
- [row ^OffsetDateTime started {:keys [creator-id db-id bytes row-count token stats score ctx]}]
+ [row ^OffsetDateTime started {:keys [creator-id db-id bytes row-count token resolved-source-ids stats score ctx]}]
(try
(t2/with-transaction [_conn]
- (let [sr-id (explorations.db/insert-stored-result! {:result_data bytes
- :creator_id creator-id
- :database_id db-id
- :dataset_query (:dataset_query row)
- :row_count row-count
- :data_access_token token})]
+ (let [sr-id (explorations.db/insert-stored-result! {:result_data bytes
+ :creator_id creator-id
+ :database_id db-id
+ :dataset_query (:dataset_query row)
+ :row_count row-count
+ :data_access_token token
+ :resolved_source_ids resolved-source-ids})]
(explorations.db/insert-query-result!
{:exploration_query_id (:id row)
:stored_result_id sr-id
diff --git a/src/metabase/explorations/schema.clj b/src/metabase/explorations/schema.clj
index cd87bc3a792..f79fcf8e62c 100644
--- a/src/metabase/explorations/schema.clj
+++ b/src/metabase/explorations/schema.clj
@@ -5,6 +5,7 @@
[metabase.lib.schema :as lib.schema]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.metrics.core :as metrics]
+ [metabase.query-permissions.core :as query-perms]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]))
@@ -118,6 +119,10 @@
"The `:data_access_token` column of a ExplorationQuery, decoded."
::exploration-thread.data-access-token)
+(mr/def ::exploration-query.resolved-source-ids
+ "The `:resolved_source_ids` column of an ExplorationQuery, decoded."
+ ::exploration-thread.resolved-source-ids)
+
(mr/def ::exploration-query
"A ExplorationQuery as selected from the app DB: every column of `:exploration_query`."
[:merge
@@ -148,7 +153,16 @@
[:entity_id {:optional true} [:maybe :string]]
[:created_at {:optional true} [:maybe ms/TemporalInstant]]
[:updated_at {:optional true} [:maybe ms/TemporalInstant]]
- [:data_access_token {:optional true} [:maybe ::exploration-query.data-access-token]]])
+ [:data_access_token {:optional true} [:maybe ::exploration-query.data-access-token]]
+ [:resolved_source_ids {:optional true} [:maybe ::exploration-query.resolved-source-ids]]])
+
+(mr/def ::exploration-query.update.stamped-together
+ "An ExplorationQuery update with its `dataset_query`, `data_access_token` and `resolved_source_ids` all or none."
+ [:fn {:error/message "dataset_query, data_access_token and resolved_source_ids go together or not at all"}
+ (fn [changes]
+ (let [ks [:dataset_query :data_access_token :resolved_source_ids]]
+ (or (not-any? #(contains? changes %) ks)
+ (every? #(contains? changes %) ks))))])
(mr/def ::exploration-query-result.chart-stats
"The `:chart_stats` column of a ExplorationQueryResult, decoded."
@@ -230,6 +244,10 @@
"The `:data_access_token` column of a ExplorationThread, decoded."
DataAccessToken)
+(mr/def ::exploration-thread.resolved-source-ids
+ "The `:resolved_source_ids` column of a ExplorationThread, decoded."
+ query-perms/ResolvedSourceIds)
+
(mr/def ::exploration-thread
"A ExplorationThread as selected from the app DB: every column of `:exploration_thread`."
[:merge
@@ -254,7 +272,8 @@
[:query_plan_started_at {:optional true} [:maybe ms/TemporalInstant]]
[:query_plan_transcript {:optional true} [:maybe ::exploration-thread.query-plan-transcript]]
[:canceled_at {:optional true} [:maybe ms/TemporalInstant]]
- [:data_access_token {:optional true} [:maybe ::exploration-thread.data-access-token]]])
+ [:data_access_token {:optional true} [:maybe ::exploration-thread.data-access-token]]
+ [:resolved_source_ids {:optional true} [:maybe ::exploration-thread.resolved-source-ids]]])
(mr/def ::exploration-thread-timeline
"A ExplorationThreadTimeline as selected from the app DB: every column of `:exploration_thread_timeline`."
diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.clj
index 1b6e538db6d..2c729bb0f09 100644
--- a/src/metabase/permissions/core.clj
+++ b/src/metabase/permissions/core.clj
@@ -168,8 +168,10 @@
(p/import-vars
[metabase.permissions.data-access-token
data-access-token
+ digest
data-access-compatible?
data-access-token-transform
+ data-access-token-reader
routing-token-for-db])
;;; import these vars with different names to make their purpose more obvious.
diff --git a/src/metabase/permissions/data_access_token.clj b/src/metabase/permissions/data_access_token.clj
index d9ec5ab4baa..6ec0b1d3a7e 100644
--- a/src/metabase/permissions/data_access_token.clj
+++ b/src/metabase/permissions/data_access_token.clj
@@ -34,6 +34,7 @@
(:require
[buddy.core.codecs :as codecs]
[buddy.core.hash :as buddy-hash]
+ [metabase.api.common :as api]
[metabase.models.interface :as mi]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.util.malli.registry :as mr]))
@@ -77,31 +78,54 @@
(sequential? x) (into [::seq] (map canonical) x)
:else x))
-(defn- digest
+(defn digest
"SHA-256 hex of `x`'s canonical printed form."
^String [x]
(codecs/bytes->hex (buddy-hash/sha256 (.getBytes (pr-str (canonical x)) "UTF-8"))))
-(defn data-access-token
- "Compute the current user's effective-data-access token over `table-ids` in `database-id`.
- See the namespace docstring for the shape. Empty dimensions are omitted; an entirely empty map
- means the user is unrestricted across all three dimensions for this target (the OSS case).
-
- Each contributor's raw value is [[digest]]ed here, so a raw sandbox attribute / role never reaches
- a caller and never lands in `stored_result.data_access_token`."
- [{:keys [database-id table-ids]}]
+(defn- token-with
+ "[[data-access-token]], reading each contributor through `(read-contributor k f)`."
+ [read-contributor {:keys [database-id table-ids]}]
(let [sandbox (into {}
(keep (fn [table-id]
- (when-let [t (sandbox-token-for-table table-id)]
+ (when-let [t (read-contributor [:sandbox table-id] #(sandbox-token-for-table table-id))]
[table-id (digest t)])))
table-ids)
- imp (when database-id (impersonation-token-for-db database-id))
- routing (when database-id (routing-token-for-db database-id))]
+ imp (when database-id
+ (read-contributor [:impersonation database-id] #(impersonation-token-for-db database-id)))
+ routing (when database-id
+ (read-contributor [:routing database-id] #(routing-token-for-db database-id)))]
(cond-> {}
(seq sandbox) (assoc :sandbox sandbox)
imp (assoc :impersonation {database-id (digest imp)})
routing (assoc :routing {database-id (digest routing)}))))
+(defn data-access-token
+ "Compute the current user's effective-data-access token over `table-ids` in `database-id`.
+ See the namespace docstring for the shape. Empty dimensions are omitted; an entirely empty map
+ means the user is unrestricted across all three dimensions for this target (the OSS case).
+
+ Each contributor's raw value is [[digest]]ed here, so a raw sandbox attribute / role never reaches
+ a caller and never lands in `stored_result.data_access_token`."
+ [target]
+ (token-with (fn [_k f] (f)) target))
+
+(defn data-access-token-reader
+ "Memoizing [[data-access-token]]: shares contributor reads across its calls. Build one per batch of checks; its
+ reads go stale when routing or the viewer's permissions change."
+ []
+ (let [memo (atom {})]
+ (fn [target]
+ (token-with (fn [k f]
+ (let [k [api/*current-user-id* k]]
+ (if-let [[_ v] (find @memo k)]
+ v
+ ;; a throw is not memoized: a later call throws again rather than reading as a value
+ (let [v (f)]
+ (swap! memo assoc k v)
+ v))))
+ target))))
+
(defn data-access-compatible?
"True when a viewer holding `viewer-token` may be served a blob computed under `creator-token`:
the lenses must be identical, dimension for dimension, target for target.
diff --git a/src/metabase/queries/cached_result.clj b/src/metabase/queries/cached_result.clj
index dc2bfefa63d..a40a4b9d4d6 100644
--- a/src/metabase/queries/cached_result.clj
+++ b/src/metabase/queries/cached_result.clj
@@ -30,6 +30,16 @@
validator, and the renderer agree."
#{"value_asc" "value_desc" "label_asc" "label_desc"})
+(defn- stored-result->resolved-source-ids
+ "The resolved source ids of `stored-result`: those stored with it, else its query's resolved live. nil when
+ resolution throws (a deleted card in the source chain) or resolves to nothing: the viewer is denied."
+ [{:keys [id dataset_query resolved_source_ids]}]
+ (try
+ (not-empty (query-perms/query->resolved-source-ids dataset_query resolved_source_ids))
+ (catch Exception e
+ (log/debugf e "Cached result %s: computing the resolved source ids threw; denying" id)
+ nil)))
+
(defn- viewer-lens-compatible?
"True when the current user's effective data-access lens (sandbox / impersonation / routing) is
compatible with the lens the `stored-result` blob was computed under — i.e. the viewer may be
@@ -43,13 +53,12 @@
rather than persist a token-less snapshot) or stored but unreadable (the model transform
decodes an unparseable token to nil; see [[metabase.queries.models.stored-result]]).
- token computation throwing (the viewer is missing a routing/impersonation attribute the
- snapshot's database requires, or the query's source-card chain can no longer be resolved to
- its underlying tables). An expected condition for some viewers — such a viewer could not run
- the query themselves either.
+ snapshot's database requires). An expected condition for some viewers — such a viewer could
+ not run the query themselves either.
A missing `:dataset_query` is not a degenerate case but a caller bug — the schema forbids NULL —
and [[cached-result-blocked-reason]] throws on it before we get here."
- [stored-result]
+ [stored-result resolved-source-ids data-access-token-reader]
(if (nil? (:data_access_token stored-result))
(do
(log/errorf "Cached result %s has no readable data_access_token (never captured, or failed to parse). Denying."
@@ -58,9 +67,8 @@
(try
(perms/data-access-compatible?
(:data_access_token stored-result)
- (perms/data-access-token {:database-id (:database_id stored-result)
- :table-ids (query-perms/query->resolved-source-table-ids
- (:dataset_query stored-result))}))
+ (data-access-token-reader {:database-id (:database_id stored-result)
+ :table-ids (:table-ids resolved-source-ids)}))
(catch Exception e
(log/debugf e "Cached result %s: computing the viewer's data-access lens threw; denying"
(:id stored-result))
@@ -70,12 +78,12 @@
"Whether the current user holds the data perms to run the snapshot's own query. Only ever reached
for non-superusers — [[cached-result-blocked-reason]] passes superusers before any check.
- `can-run-query?` absorbs the ordinary permission-denial `ExceptionInfo`s itself; anything else it
+ The check absorbs the ordinary permission-denial `ExceptionInfo`s itself; anything else it
throws — a stored query malformed enough to trip its `:- :map` schema, a source table that no
longer exists — must not escape an authorization gate as a 500: deny instead."
- [stored-result]
+ [stored-result resolved-source-ids]
(try
- (query-perms/can-run-query? (:dataset_query stored-result))
+ (query-perms/can-run-query-with-resolved-source-ids? (:dataset_query stored-result) resolved-source-ids)
(catch Exception e
(log/debugf e "Cached result %s: the data-perms check threw; denying" (:id stored-result))
false)))
@@ -94,25 +102,32 @@
`:no-data-perms` — current user lacks the data perms required to run the underlying query.
`:incompatible-context` — current user's sandbox/impersonation/routing lens differs from the
lens the snapshot was computed under."
- [stored-result]
- (when (nil? (:dataset_query stored-result))
- (throw (ex-info "stored-result is missing its dataset_query"
- {:stored-result-id (:id stored-result)})))
- (cond
- api/*is-superuser?*
- nil
-
- (not (viewer-can-run-underlying-query? stored-result))
- :no-data-perms
-
- (not (viewer-lens-compatible? stored-result))
- :incompatible-context))
+ ([stored-result]
+ (cached-result-blocked-reason stored-result {}))
+ ([stored-result {:keys [data-access-token-reader] :or {data-access-token-reader perms/data-access-token}}]
+ (when (nil? (:dataset_query stored-result))
+ (throw (ex-info "stored-result is missing its dataset_query"
+ {:stored-result-id (:id stored-result)})))
+ (if api/*is-superuser?*
+ nil
+ (let [resolved-source-ids (stored-result->resolved-source-ids stored-result)]
+ (cond
+ (nil? resolved-source-ids)
+ :no-data-perms
+
+ (not (viewer-can-run-underlying-query? stored-result resolved-source-ids))
+ :no-data-perms
+
+ (not (viewer-lens-compatible? stored-result resolved-source-ids data-access-token-reader))
+ :incompatible-context)))))
(defn viewer-can-view-cached-result?
"Boolean form of [[assert-can-view-cached-result!]]: true when the current user may be served the
blob for `stored-result`."
- [stored-result]
- (nil? (cached-result-blocked-reason stored-result)))
+ ([stored-result]
+ (viewer-can-view-cached-result? stored-result {}))
+ ([stored-result opts]
+ (nil? (cached-result-blocked-reason stored-result opts))))
(defn assert-can-view-cached-result!
"Throw a 403 if the current user must not see the cached blob for `stored-result`."
diff --git a/src/metabase/queries/db.clj b/src/metabase/queries/db.clj
index 074aac76219..e7372454142 100644
--- a/src/metabase/queries/db.clj
+++ b/src/metabase/queries/db.clj
@@ -138,6 +138,11 @@
:type :metric
{:order-by [[:name :asc]]}))
+(mu/defn document-collection-and-exploration
+ "The Document with `document-id` narrowed to `:id`, `:collection_id` and `:exploration_id`, or nil."
+ [document-id :- ms/PositiveInt]
+ (t2/select-one [:model/Document :id :collection_id :exploration_id] :id document-id))
+
(mu/defn document-card-ids
"The IDs of the Cards that belong to the Document with `document-id`."
[document-id :- ms/PositiveInt]
@@ -559,10 +564,15 @@
(t2/insert! :model/StoredResultUse {:stored_result_id stored-result-id
:card_id card-id}))
-(mu/defn stored-results-for-card
- "The StoredResults used by the Card with `card-id`."
+(mu/defn stored-result-gate-inputs-for-card
+ "The StoredResults used by the Card with `card-id`, narrowed to the columns the cached-result gate reads."
[card-id :- ::lib.schema.id/card]
- (t2/select :model/StoredResult
+ (t2/select [:model/StoredResult
+ :id
+ :database_id
+ :dataset_query
+ :data_access_token
+ :resolved_source_ids]
:id [:in ^:allow-subquery {:select [:stored_result_id]
:from [:stored_result_use]
:where [:= :card_id card-id]}]))
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.clj
index d2c0c329bf7..04738b53f28 100644
--- a/src/metabase/queries/models/card.clj
+++ b/src/metabase/queries/models/card.clj
@@ -34,6 +34,7 @@
[metabase.premium-features.core :refer [defenterprise]]
[metabase.public-sharing.core :as public-sharing]
[metabase.pulse.core :as pulse]
+ [metabase.queries.cached-result :as cached-result]
[metabase.queries.card-schema :as card-schema]
[metabase.queries.db :as queries.db]
[metabase.queries.models.card.metadata :as card.metadata]
@@ -155,6 +156,14 @@
(:id card) (queries.db/card-document-id (:id card))
:else ::not-adjudicable))
+(defn- summary-card-snapshots
+ "The snapshots linked to `card` when it is an ephemeral card of an exploration Summary (`document`), or nil when it
+ is not one or has none. Such a card renders only those snapshots — a composite links its own plus one per source —
+ so it is gated on their artifact verdicts rather than on the Summary's content gate over every thread."
+ [card document]
+ (when (and (:exploration_id document) (:id card))
+ (not-empty (queries.db/stored-result-gate-inputs-for-card (:id card)))))
+
(defn- parent-document-permits?
"Whether the parent Document of `card`, if it has one, grants `read-or-write` to the current user.
@@ -164,14 +173,23 @@
values discovered under the *creator's* data-access lens (see
[[metabase.explorations.derived-perms]]). Collection permissions alone cannot adjudicate that
material — withholding it is the entire reason the Document's content gate exists — so a Card
- that carries those values must be gated exactly as the document embedding it is."
+ that carries those values is gated by the document embedding it: on read, by its own snapshots
+ where it has any (see [[summary-card-snapshots]]) plus the Summary's collection-level read. A
+ Summary's cards are never writable, by anyone: each renders a stored snapshot, and editing the
+ card would desynchronise the two. Edits to a Summary chart save as new draft cards through the
+ document instead."
[card read-or-write]
(let [document-id (parent-document-id card)]
(if (or (= ::not-adjudicable document-id) (nil? document-id))
true
- (case read-or-write
- :read (mi/can-read? :model/Document document-id)
- :write (mi/can-write? :model/Document document-id)))))
+ (let [document (queries.db/document-collection-and-exploration document-id)]
+ (case read-or-write
+ :read (if-let [snapshots (summary-card-snapshots card document)]
+ (and (mi/current-user-has-full-permissions? :read document)
+ (every? cached-result/viewer-can-view-cached-result? snapshots))
+ (mi/can-read? document))
+ :write (and (nil? (:exploration_id document))
+ (mi/can-write? document)))))))
;; NOTE: deliberately a plain `defmethod` rather than `perms/define-collection-based-visibility!`.
;; That macro's contract is "read perms are determined *fully* by `:collection_id`", which stopped
diff --git a/src/metabase/queries/models/stored_result.clj b/src/metabase/queries/models/stored_result.clj
index 5945cd095d2..58a420c8fe3 100644
--- a/src/metabase/queries/models/stored_result.clj
+++ b/src/metabase/queries/models/stored_result.clj
@@ -9,15 +9,17 @@
(:require
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
+ [metabase.query-permissions.core :as query-perms]
[methodical.core :as methodical]
[toucan2.core :as t2]))
(methodical/defmethod t2/table-name :model/StoredResult [_model] :stored_result)
(t2/deftransforms :model/StoredResult
- {:result_data (mi/transform-secret-value "stored_result.result_data")
- :dataset_query mi/transform-json
- :data_access_token perms/data-access-token-transform})
+ {:result_data (mi/transform-secret-value "stored_result.result_data")
+ :dataset_query mi/transform-json
+ :data_access_token perms/data-access-token-transform
+ :resolved_source_ids query-perms/resolved-source-ids-transform})
(doto :model/StoredResult
(derive :metabase/model)
diff --git a/src/metabase/queries/models/stored_result_use.clj b/src/metabase/queries/models/stored_result_use.clj
index 604266fd2c9..a2420b5ef81 100644
--- a/src/metabase/queries/models/stored_result_use.clj
+++ b/src/metabase/queries/models/stored_result_use.clj
@@ -52,7 +52,7 @@
"Throw a 403 unless the current user may be served *every* `stored_result` Card `card-id` renders
from."
[card-id :- ms/PositiveInt]
- (let [snapshots (queries.db/stored-results-for-card card-id)]
+ (let [snapshots (queries.db/stored-result-gate-inputs-for-card card-id)]
(when (empty? snapshots)
(throw (ex-info (tru "This card has no cached results.") {:status-code 404})))
(doseq [snapshot snapshots]
diff --git a/src/metabase/queries/schema.clj b/src/metabase/queries/schema.clj
index f5191899bb9..e1518211d38 100644
--- a/src/metabase/queries/schema.clj
+++ b/src/metabase/queries/schema.clj
@@ -9,6 +9,7 @@
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.parameters.schema :as parameters.schema]
[metabase.permissions.schema :as permissions.schema]
+ [metabase.query-permissions.schema :as query-permissions.schema]
[metabase.util.log :as log]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
@@ -334,6 +335,9 @@
"The `:data_access_token` column of a StoredResult, decoded."
::permissions.schema/data-access-token)
+(mr/def ::stored-result.resolved-source-ids
+ ::query-permissions.schema/resolved-source-ids)
+
(mr/def ::stored-result
"A StoredResult as selected from the app DB: every column of `:stored_result`."
[:merge
@@ -344,14 +348,15 @@
(mr/def ::stored-result.update
"What an update (or insert) of a StoredResult accepts: every column of `:stored_result` except `id`, all optional."
[:map {:closed true}
- [:result_data {:optional true} [:maybe [:or bytes? :string]]]
- [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]
- [:database_id {:optional true} [:maybe ::lib.schema.id/database]]
- [:dataset_query {:optional true} [:maybe ::stored-result.dataset-query]]
- [:data_access_token {:optional true} [:maybe ::stored-result.data-access-token]]
- [:row_count {:optional true} [:maybe :int]]
- [:created_at {:optional true} [:maybe ms/TemporalInstant]]
- [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])
+ [:result_data {:optional true} [:maybe [:or bytes? :string]]]
+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]
+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]
+ [:dataset_query {:optional true} [:maybe ::stored-result.dataset-query]]
+ [:data_access_token {:optional true} [:maybe ::stored-result.data-access-token]]
+ [:resolved_source_ids {:optional true} [:maybe ::stored-result.resolved-source-ids]]
+ [:row_count {:optional true} [:maybe :int]]
+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]
+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])
(mr/def ::stored-result-use
"A StoredResultUse as selected from the app DB: every column of `:stored_result_use`."
diff --git a/src/metabase/query_permissions/core.clj b/src/metabase/query_permissions/core.clj
index 20524566f83..f5720e18dca 100644
--- a/src/metabase/query_permissions/core.clj
+++ b/src/metabase/query_permissions/core.clj
@@ -11,18 +11,23 @@
can-query-table?
check-parameter-field-permissions
can-run-query?
+ can-run-query-with-resolved-source-ids?
check-card-read-perms
check-card-result-metadata-data-perms
check-data-perms
check-result-metadata-data-perms
check-run-permissions-for-query
has-perm-for-query?
+ merge-resolved-source-ids
perms-exception
query->resolved-source-ids
- query->resolved-source-table-ids
query->source-ids
query->source-table-ids
- required-perms-for-query])
+ required-perms-for-query
+ resolved-source-ids-transform
+ ResolvedSourceIds
+ stamped-resolved-source-ids
+ verified-resolved-source-ids])
(defmacro with-card-instances
"Provide a map of Card ID -> instance to use instead of hitting a metadata provider for more efficient perms
diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.clj
index 14ce505289c..ed4f0e17634 100644
--- a/src/metabase/query_permissions/impl.clj
+++ b/src/metabase/query_permissions/impl.clj
@@ -17,6 +17,7 @@
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
[metabase.query-permissions.db :as query-permissions.db]
+ [metabase.query-permissions.schema :as query-permissions.schema]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.interface :as qp.i]
[metabase.query-processor.preprocess :as qp.preprocess]
@@ -26,6 +27,7 @@
[metabase.request.core :as request]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
+ [metabase.util.json :as json]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
@@ -203,7 +205,7 @@
lens. `do-as-admin` keeps the user's id (and thus their attributes), so database routing and
impersonation would still resolve the user's destination db / role — and throw when that
configuration exists without its premium feature. Those per-user dimensions are the lens callers
- are *comparing*, not part of the query's table footprint, so skip them.
+ are *comparing*, not part of the query's resolved source ids, so skip them.
[[qp.i/*skip-middleware-because-app-db-access*]] is a much bigger name than its blast radius:
despite \"middleware\" plural, the routing and impersonation preprocess middlewares are the only
@@ -213,29 +215,6 @@
(binding [qp.i/*skip-middleware-because-app-db-access* true]
(preprocess-query query)))
-(mu/defn query->resolved-source-ids :- [:maybe :map]
- "Like [[query->source-ids]], but resolves card-sourced queries (`:source-table \"card__N\"`,
- card-sourced joins, nested card-on-card chains) first, by preprocessing the query.
-
- The whole projection, not just its tables: preprocessing is also what surfaces `:card-ids` (via the
- `:qp/stage-is-from-source-card` annotation it adds), and those drive a read-permission check on
- each card's collection in [[required-perms-for-query]] — a requirement no projection of the *raw*
- query can express. Callers keying a permission verdict on this must keep the whole map, or two
- queries reading the same tables through different cards will look identical.
-
- Preprocessing runs as admin with per-user lens resolution skipped
- ([[preprocess-without-per-user-lens]]), so the result is identical for every user. THROWS when the
- query cannot be preprocessed — e.g. a card in the source chain has been deleted — so callers
- gating cached reads on it can fail closed instead of treating the query as touching nothing."
- [query :- ::query]
- (when (seq query)
- (query->source-ids (preprocess-without-per-user-lens query))))
-
-(mu/defn query->resolved-source-table-ids :- [:maybe [:set ::lib.schema.id/table]]
- "The Table IDs of [[query->resolved-source-ids]]. Throws on an unpreprocessable query, as it does."
- [query :- ::query]
- (:table-ids (query->resolved-source-ids query)))
-
(defn- referenced-card-ids
"Return the union of all the `:query-permissions/referenced-card-ids` sets anywhere in the query."
[query]
@@ -249,25 +228,109 @@
query)
(not-empty @all-ids)))
-(defn- native-query-perms
- [query]
+(def ResolvedSourceIds
+ "Everything [[required-perms-for-query]] and the data-access lens read from a preprocessed query."
+ [:ref ::query-permissions.schema/resolved-source-ids])
+
+(defn- preprocessed->resolved-source-ids
+ [preprocessed-query]
+ (let [referenced (referenced-card-ids preprocessed-query)]
+ (cond-> (or (query->source-ids preprocessed-query) {})
+ referenced (assoc :referenced-card-ids referenced))))
+
+(defn- query-digest
+ "Digest of `query` as it reads back from JSON."
+ ^String [query]
+ (perms/digest (json/decode (json/encode query))))
+
+(mu/defn stamped-resolved-source-ids :- [:maybe ResolvedSourceIds]
+ "`resolved-source-ids` tagged with a digest of `query`, the query they came from."
+ [query :- ::query
+ resolved-source-ids :- [:maybe ResolvedSourceIds]]
+ (some-> resolved-source-ids (assoc :query-digest (query-digest query))))
+
+(mu/defn verified-resolved-source-ids :- [:maybe ResolvedSourceIds]
+ "`stored` without its digest, or nil unless it was stamped from `query`."
+ [query :- [:maybe ::query]
+ stored :- [:maybe ResolvedSourceIds]]
+ (when-let [stored-digest (:query-digest stored)]
+ (when (= stored-digest (query-digest query))
+ (dissoc stored :query-digest))))
+
+(mu/defn query->resolved-source-ids :- [:maybe ResolvedSourceIds]
+ "The [[ResolvedSourceIds]] of `query`, preprocessed as admin with per-user lens resolution skipped, so it is the same
+ for every user. Throws on an unpreprocessable query. `stored` resolved source ids, when given, are returned instead
+ when stamped from `query`."
+ ([query :- ::query]
+ (when (seq query)
+ (preprocessed->resolved-source-ids (preprocess-without-per-user-lens query))))
+ ([query :- ::query
+ stored :- [:maybe ResolvedSourceIds]]
+ (or (verified-resolved-source-ids query stored)
+ (query->resolved-source-ids query))))
+
+(defn- merge-resolved-source-ids*
+ [a {:keys [table-ids table-query-ids card-ids native? referenced-card-ids]}]
+ (cond-> a
+ table-ids (update :table-ids (fnil set/union #{}) table-ids)
+ table-query-ids (update :table-query-ids (fnil set/union #{}) table-query-ids)
+ card-ids (update :card-ids (fnil set/union #{}) card-ids)
+ (some? native?) (update :native? (fnil #(or %1 %2) false) native?)
+ referenced-card-ids (update :referenced-card-ids (fnil set/union #{}) referenced-card-ids)))
+
+(mu/defn merge-resolved-source-ids :- [:maybe ResolvedSourceIds]
+ "The [[ResolvedSourceIds]] of a query reading everything `id-maps` read. nil when none is given."
+ [id-maps :- [:sequential [:maybe ResolvedSourceIds]]]
+ (when-let [id-maps (seq (remove nil? id-maps))]
+ (reduce merge-resolved-source-ids* {} id-maps)))
+
+(def resolved-source-ids-transform
+ "Toucan transform for a persisted [[query->resolved-source-ids]]. An unreadable blob reads as `nil`, like a row that
+ was never stamped, so the query is resolved live."
+ (let [{:keys [in out]} (mi/transform-json-with-schema "resolved_source_ids" ResolvedSourceIds {:log-level :error})]
+ {:in in
+ :out (fn [s]
+ (let [resolved-source-ids (out s)]
+ (if (or (nil? resolved-source-ids) (mr/validate ResolvedSourceIds resolved-source-ids))
+ resolved-source-ids
+ (do (log/errorf "Unreadable resolved_source_ids %s; resolving live" (pr-str s))
+ nil))))}))
+
+(defn- native-perms
+ [card-ids]
(merge
{:perms/create-queries :query-builder-and-native
:perms/view-data :unrestricted}
- (when-let [card-ids (referenced-card-ids query)]
+ (when (seq card-ids)
{:card-ids card-ids
:paths (into #{}
(mapcat (fn [card-id]
(mi/perms-objects-set (card-instance card-id) :read)))
card-ids)})))
+(defn- native-query-perms
+ [query]
+ (native-perms (referenced-card-ids query)))
+
+(defn- resolved-source-ids->required-perms
+ [{:keys [table-ids table-query-ids card-ids native? referenced-card-ids]}]
+ (merge
+ (when-let [card-ids (not-empty (into (set card-ids) referenced-card-ids))]
+ {:card-ids card-ids})
+ (when (seq table-ids)
+ {:perms/view-data (zipmap table-ids (repeat :unrestricted))})
+ (when (seq table-query-ids)
+ {:perms/create-queries (zipmap table-query-ids (repeat :query-builder))})
+ (when native?
+ (native-perms referenced-card-ids))))
+
(defn- legacy-mbql-required-perms
([query options]
(legacy-mbql-required-perms nil query options))
([metadata-provider
query
- {:keys [throw-exceptions? already-preprocessed?]}]
+ {:keys [throw-exceptions? already-preprocessed? resolved-source-ids]}]
(try
(let [metadata-provider (or metadata-provider
(when (qp.store/initialized?)
@@ -280,18 +343,10 @@
lib/primary-source-card-id)]
{:paths (source-card-read-perms source-card-id)}
;; otherwise if there's no source card then calculate perms based on the Tables referenced in the query
- (let [query (cond-> query
- (not already-preprocessed?) preprocess-query)
- {:keys [table-ids table-query-ids card-ids native?]} (query->source-ids query)]
- (merge
- (when-let [card-ids (not-empty (into (set card-ids) (referenced-card-ids query)))]
- {:card-ids card-ids})
- (when (seq table-ids)
- {:perms/view-data (zipmap table-ids (repeat :unrestricted))})
- (when (seq table-query-ids)
- {:perms/create-queries (zipmap table-query-ids (repeat :query-builder))})
- (when native?
- (native-query-perms query))))))
+ (resolved-source-ids->required-perms
+ (or resolved-source-ids
+ (preprocessed->resolved-source-ids (cond-> query
+ (not already-preprocessed?) preprocess-query))))))
;; if for some reason we can't expand the Card (i.e. it's an invalid legacy card) just return a set of permissions
;; that means no one will ever get to see it
(catch Throwable e
@@ -317,7 +372,9 @@
(defn required-perms-for-query
"Returns a map representing the permissions required to run `query`. The map has the optional keys
- :paths (containing legacy permission paths), :card-ids, :perms/view-data, and :perms/create-queries."
+ :paths (containing legacy permission paths), :card-ids, :perms/view-data, and :perms/create-queries.
+
+ `:resolved-source-ids` stands in for preprocessing."
[query & {:as perms-opts}]
(if (empty? query)
{}
@@ -457,6 +514,20 @@
(throw e)
false))))
+(defn- can-run-query*
+ [{database-id :database :as query} {:keys [throw-exceptions?] :as perms-opts}]
+ (try
+ (let [required-perms (required-perms-for-query query perms-opts)]
+ (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
+ (if (and throw-exceptions? (not (:permissions-error? (ex-data e))))
+ (throw e)
+ false))))
+
(mu/defn can-run-query?
"Return `true` if the current user has sufficient permissions to run `query`, and `false` otherwise.
@@ -471,22 +542,23 @@
already-preprocessed? :- :boolean]
(can-run-query? query already-preprocessed? false))
- ([{database-id :database :as query} :- ::query
- already-preprocessed? :- :boolean
- throw-non-permission-errors? :- :boolean]
- (try
- (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
- (if (and throw-non-permission-errors? (not (:permissions-error? (ex-data e))))
- (throw e)
- false)))))
+ ([query :- ::query
+ already-preprocessed? :- :boolean
+ throw-non-permission-errors? :- :boolean]
+ (can-run-query* query
+ {:already-preprocessed? already-preprocessed?
+ :throw-exceptions? throw-non-permission-errors?})))
+
+(mu/defn can-run-query-with-resolved-source-ids? :- :boolean
+ "[[can-run-query?]] for a query whose [[query->resolved-source-ids]] is already known, without preprocessing it.
+ Denies on empty `resolved-source-ids`, which only a failed resolution produces."
+ [query :- ::query
+ resolved-source-ids :- ResolvedSourceIds]
+ (if (empty? resolved-source-ids)
+ (do (log/debug "Empty resolved source ids; denying")
+ false)
+ (can-run-query* query {:resolved-source-ids resolved-source-ids
+ :throw-exceptions? 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/src/metabase/query_permissions/schema.clj b/src/metabase/query_permissions/schema.clj
new file mode 100644
index 00000000000..9c3219376a9
--- /dev/null
+++ b/src/metabase/query_permissions/schema.clj
@@ -0,0 +1,15 @@
+(ns metabase.query-permissions.schema
+ (:require
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.util.malli.registry :as mr]))
+
+(mr/def ::resolved-source-ids
+ "Everything [[metabase.query-permissions.impl/required-perms-for-query]] and the data-access lens read from a
+ preprocessed query."
+ [:map {:closed true}
+ [:table-ids {:optional true} [:set ::lib.schema.id/table]]
+ [:table-query-ids {:optional true} [:set ::lib.schema.id/table]]
+ [:card-ids {:optional true} [:set ::lib.schema.id/card]]
+ [:native? {:optional true} :boolean]
+ [:referenced-card-ids {:optional true} [:set ::lib.schema.id/card]]
+ [:query-digest {:optional true} :string]])
diff --git a/test/metabase/documents/api/document_test.clj b/test/metabase/documents/api/document_test.clj
index dab883198dd..85fd721d6fa 100644
--- a/test/metabase/documents/api/document_test.clj
+++ b/test/metabase/documents/api/document_test.clj
@@ -5,6 +5,8 @@
[clojure.test :refer :all]
[medley.core :as m]
[metabase.collections.models.collection :as collection]
+ [metabase.documents.api.document :as api.document]
+ [metabase.documents.models.document :as m.document]
[metabase.documents.prose-mirror :as prose-mirror]
[metabase.documents.test-util :as documents.test-util]
[metabase.events.core :as events]
@@ -125,6 +127,22 @@
(mt/user-http-request :crowberto
:get 404 "document/99999")))))
+(deftest get-document-runs-content-gate-once-test
+ (testing "one GET /api/document/:id adjudicates the document's content once"
+ (mt/with-temp [:model/Collection {coll-id :id} {}
+ :model/Document {document-id :id} {:name "Test Document"
+ :collection_id coll-id
+ :document (documents.test-util/text->prose-mirror-ast "Doc 1")}]
+ (let [gate @#'m.document/doc-content-visibility-fn
+ orig @gate
+ calls (atom 0)]
+ (try
+ (reset! gate (fn [_doc] (swap! calls inc) true))
+ (mt/user-http-request :rasta :get 200 (format "document/%s" document-id))
+ (is (= 1 @calls))
+ (finally
+ (reset! gate orig)))))))
+
(defn- recent-view
"The RecentViews row, if any, for `user-id` viewing document `document-id`. Document reads record a recent
view (asynchronously via grouper, so these tests force synchronous batching), giving us a reliable signal
@@ -2879,3 +2897,28 @@
:collection_id coll-id
:document (card-embed-ast mbql-card-id)})]
(is (t2/exists? :model/Card :document_id (:id result))))))))))
+
+(defn- embedded-card-ids [ast]
+ (->> (tree-seq map? :content ast)
+ (filter #(= prose-mirror/card-embed-type (:type %)))
+ (map #(get-in % [:attrs "id"]))))
+
+(deftest add-card-to-document-test
+ (mt/with-temp [:model/Document {document-id :id} {:name "Test Document"
+ :document (documents.test-util/text->prose-mirror-ast "Doc")
+ :is_placeholder true}
+ :model/Card {card-1 :id} {:document_id document-id}
+ :model/Card {card-2 :id} {:document_id document-id}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (testing "add-card-to-document! returns the hydrated document"
+ (let [result (api.document/add-card-to-document! document-id card-1 nil)]
+ (is (=? {:id document-id :is_placeholder false :can_write true :creator map?} result))
+ (is (= [card-1] (embedded-card-ids (:document result))))))
+ (testing "add-card-to-document-row! returns the plain row, without hydrating it"
+ (let [result (api.document/add-card-to-document-row! document-id card-2 nil
+ :extra-attrs {"chart_href" "/x"})]
+ (is (=? {:id document-id :is_placeholder false} result))
+ (is (not-any? #(contains? result %) [:can_write :creator]))
+ (is (= [card-1 card-2]
+ (embedded-card-ids (:document result))
+ (embedded-card-ids (t2/select-one-fn :document :model/Document :id document-id)))))))))
diff --git a/test/metabase/explorations/api_test.clj b/test/metabase/explorations/api_test.clj
index cd8a58a6e0b..f543c4e13c0 100644
--- a/test/metabase/explorations/api_test.clj
+++ b/test/metabase/explorations/api_test.clj
@@ -110,7 +110,7 @@
{:dataset_query dq
:data_access_token (perms/data-access-token
{:database-id (:database_id q)
- :table-ids (query-perms/query->resolved-source-table-ids dq)})})))))
+ :table-ids (:table-ids (query-perms/query->resolved-source-ids dq))})})))))
(defn- vectorize-clauses
"`mt/user-http-request` decodes JSON arrays as lists, but MBQL normalization (and
@@ -1242,6 +1242,31 @@
"the raw driver error must not reach anyone who can read the exploration")
(is (= "This query failed to run." (:error_message q)))))))))
+(deftest exploration-lens-stamps-never-reach-the-client-test
+ (testing "GET /:id strips the lens stamp and resolved source ids from every thread and every query"
+ (mt/with-temp [:model/User u {:email "[email protected]"}
+ :model/Card metric (valid-metric-card (:id u))]
+ (let [resp (create-exploration! u
+ {:name "stamped"
+ :metrics [{:card_id (:id metric)
+ :dimension_mappings [{:dimension_id (duid "d1")
+ :table_id 1
+ :target ["field" 1 nil]}]}]
+ :dimensions [{:dimension_id (duid "d1")}]})
+ thread-id (-> resp :threads first :id)
+ qid (-> resp :threads first :queries first :id)
+ stamp {:data_access_token {} :resolved_source_ids {:table-ids #{(mt/id :venues)}}}]
+ (store-fake-result! qid {:status :completed :data {:cols [{:name "x"}] :rows [[1]]} :row_count 1})
+ (t2/update! :model/ExplorationQuery :id qid (assoc stamp :status "done"))
+ (t2/update! :model/ExplorationThread :id thread-id stamp)
+ (let [body (mt/user-http-request u :get 200 (str "exploration/" (:id resp)))
+ keys-in-body (atom #{})]
+ (is (= [qid] (->> body :threads (mapcat :queries) (map :id)))
+ "sanity: queries are present")
+ (walk/postwalk (fn [x] (when (map? x) (swap! keys-in-body into (keys x))) x) body)
+ (is (not (contains? @keys-in-body :data_access_token)))
+ (is (not (contains? @keys-in-body :resolved_source_ids))))))))
+
(deftest exploration-query-result-permissions-test
(testing "GET /query/:id enforces the parent exploration's read check"
(mt/with-temp [:model/User owner {:email "[email protected]"}
@@ -1360,6 +1385,44 @@
(mt/with-temp [:model/User u {:email "[email protected]"}]
(mt/user-http-request u :put 404 "exploration/pages/hidden" {:page_ids [9999999] :hidden true}))))
+(deftest exploration-explore-further-stamps-union-resolved-source-ids-test
+ (testing "a block with two metric cards stamps its drill thread with every table either card reads, the same tables
+ its token is computed over"
+ (mt/with-temp [:model/User u {:email "[email protected]"}
+ :model/Card venues-metric (venues-metric-card (:id u))
+ :model/Card products-metric {:type :metric
+ :creator_id (:id u)
+ :dataset_query (lib/->legacy-MBQL
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ (lib/aggregate (lib/count)))))}]
+ (let [body {:name "union drill"
+ :metrics [{:card_id (:id venues-metric) :dimension_mappings (venues-dimension-mappings)}
+ {:card_id (:id products-metric)
+ :dimension_mappings [{:dimension_id (duid "price")
+ :table_id (mt/id :products)
+ :target ["field" (mt/id :products :price) nil]}]}]
+ :dimensions [{:dimension_id (duid "price") :display_name "Price"}]}
+ created (create-exploration! u body)
+ page-id (some :id (filter #(str/includes? (:name %) "Price")
+ (-> created :threads first :blocks first :pages)))
+ _ (mt/user-http-request u :post 200 (format "exploration/%d/explore-further" (:id created))
+ {:page_id page-id
+ :explore_filters [{:operator "="
+ :field_ref ["field" (mt/id :venues :price) nil]
+ :value 2
+ :display_value "2"}]})
+ new (t2/select-one :model/ExplorationThread :exploration_id (:id created) :position 1)]
+ (is (= {:table-ids #{(mt/id :venues) (mt/id :products)}
+ :table-query-ids #{(mt/id :venues) (mt/id :products)}}
+ (dissoc (:resolved_source_ids new) :query-digest)))
+ (testing "stamped from the first card's query as it reads back, which is what the content gate adjudicates"
+ (is (= {:table-ids #{(mt/id :venues) (mt/id :products)}
+ :table-query-ids #{(mt/id :venues) (mt/id :products)}}
+ (query-perms/verified-resolved-source-ids
+ (t2/select-one-fn :dataset_query :model/Card :id (:id venues-metric))
+ (:resolved_source_ids new)))))))))
+
(deftest exploration-explore-further-creates-filtered-thread-test
(testing "POST /:id/explore-further copies the clicked block into a new filtered thread"
(mt/with-temp [:model/User u {:email "[email protected]"}
@@ -1393,6 +1456,11 @@
"explore-further does not create a second Summary document")
(is (= (:id orig-thread) (:id orig)))
(is (= 1 (:position new)))
+ (testing "the drill thread's lens stamp carries the resolved source ids of the metric it drills, which never
+ reaches the client"
+ (is (= {:table-ids #{(mt/id :venues)} :table-query-ids #{(mt/id :venues)}}
+ (dissoc (t2/select-one-fn :resolved_source_ids :model/ExplorationThread :id (:id new)) :query-digest)))
+ (is (not-any? #(contains? % :resolved_source_ids) (cons new new-queries))))
(testing "the drill thread records the page it was drilled from (sidebar nesting)"
(is (= page-id (:source_page_id new)))
(is (nil? (:source_page_id orig))))
@@ -2901,6 +2969,39 @@
(t2/select-one-fn :data_access_token :model/StoredResult :id (:stored-result-id result)))
"the composite carries the same lens its sources were computed under")))))
+(deftest composite-snapshot-stamps-every-sources-resolved-source-ids-test
+ (testing "A multi-query composite is stamped with the union of its sources' resolved source ids, under its own
+ query's digest, so the read gate holds a viewer to every source's tables without a live preprocess"
+ (mt/with-temp [:model/User u {:email "[email protected]"}
+ :model/Card metric (valid-metric-card (:id u))]
+ (let [{:keys [document-id query-ids]} (two-ready-queries! u metric)
+ doc (t2/select-one :model/Document :id document-id)
+ [sr-1 sr-2] (map #(t2/select-one :model/StoredResult
+ :id (t2/select-one-fn :stored_result_id :model/ExplorationQueryResult
+ :exploration_query_id %))
+ query-ids)
+ checkins-q (lib/->legacy-MBQL (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :checkins)))
+ (lib/aggregate (lib/count)))))]
+ ;; The first source carries a verified stamp naming an extra table, so only a read of the stamp (not a
+ ;; live resolve) puts `products` in the union; the second has no stamp and reads a different table.
+ (t2/update! :model/StoredResult :id (:id sr-1)
+ {:resolved_source_ids (query-perms/stamped-resolved-source-ids
+ (:dataset_query sr-1)
+ {:table-ids #{(mt/id :venues) (mt/id :products)}})})
+ (t2/update! :model/StoredResult :id (:id sr-2) {:dataset_query checkins-q :resolved_source_ids nil})
+ (let [result (combine-with-perms-stubbed!
+ query-ids document-id (:collection_id doc) u
+ {:display "bar" :visualization-settings {}})
+ composite (t2/select-one :model/StoredResult :id (:stored-result-id result))
+ verified (query-perms/verified-resolved-source-ids (:dataset_query composite)
+ (:resolved_source_ids composite))]
+ (is (some? verified)
+ "the stamp verifies against the composite row's own query")
+ (is (= #{(mt/id :venues) (mt/id :products) (mt/id :checkins)}
+ (:table-ids verified))
+ "and covers the first source's stamped tables plus the unstamped second source's resolved table"))))))
+
(deftest composite-refuses-to-blend-different-data-access-lenses-test
(testing "Sources computed under different lenses have no single honest stamp, so the combine is refused
rather than persisted under one of them"
diff --git a/test/metabase/explorations/db_test.clj b/test/metabase/explorations/db_test.clj
new file mode 100644
index 00000000000..85bca544d98
--- /dev/null
+++ b/test/metabase/explorations/db_test.clj
@@ -0,0 +1,23 @@
+(ns metabase.explorations.db-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.explorations.db :as explorations.db]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.test :as mt]))
+
+(deftest update-query!-writes-a-query-and-its-stamp-together-test
+ (let [mp (mt/metadata-provider)
+ stamp {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ :data_access_token {}
+ :resolved_source_ids {:table-ids #{(mt/id :venues)}}}]
+ (doseq [k (keys stamp)]
+ (testing (str "rejects " k " without the rest of the stamp")
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"go together"
+ (explorations.db/update-query! Integer/MAX_VALUE (select-keys stamp [k])))))
+ (testing (str "rejects the stamp without " k)
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"go together"
+ (explorations.db/update-query! Integer/MAX_VALUE (dissoc stamp k))))))
+ (testing "accepts the whole stamp, or none of it"
+ (is (= 0 (explorations.db/update-query! Integer/MAX_VALUE stamp)))
+ (is (= 0 (explorations.db/update-query! Integer/MAX_VALUE {:status "done"}))))))
diff --git a/test/metabase/explorations/derived_perms_test.clj b/test/metabase/explorations/derived_perms_test.clj
index 28dc05f8804..b3b70a2c2e9 100644
--- a/test/metabase/explorations/derived_perms_test.clj
+++ b/test/metabase/explorations/derived_perms_test.clj
@@ -6,13 +6,17 @@
(:require
[clojure.test :refer :all]
[metabase.documents.core :as documents]
+ [metabase.documents.models.document :as m.document]
+ [metabase.explorations.db :as explorations.db]
[metabase.explorations.derived-perms :as derived-perms]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
+ [metabase.permissions.data-access-token :as data-access-token]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.queries.core :as queries]
+ [metabase.query-permissions.core :as query-perms]
[metabase.request.core :as request]
[metabase.test :as mt]
[metabase.test.util.dynamic-redefs :as dynamic-redefs]
@@ -275,7 +279,7 @@
(let [thread (thread-with-snapshots! (specs n))
calls (atom 0)]
(dynamic-redefs/with-dynamic-fn-redefs
- [queries/viewer-can-view-cached-result? (fn [_] (swap! calls inc) true)]
+ [queries/viewer-can-view-cached-result? (fn [& _] (swap! calls inc) true)]
(request/with-current-user (mt/user->id :rasta)
(derived-perms/thread-ids-with-visible-derived-data [thread])))
@calls))]
@@ -283,10 +287,25 @@
(is (= 1 (verdicts 25))
"one verdict for the group — before batching, 25 charts cost 25 of them"))))
+(deftest groups-sharing-a-table-read-its-sandbox-once-test
+ (testing "charts over one table land in different groups when their lenses differ, and each group's verdict
+ computes the viewer's lens over that table. The batch shares those reads, so the table's sandbox is
+ read once however many groups there are"
+ (let [venues (mt/id :venues)
+ thread (thread-with-snapshots! (for [token [{} {:sandbox {venues "d-a"}} {:sandbox {venues "d-b"}}]]
+ {:creator-id (mt/user->id :lucky) :table :venues :token token}))
+ reads (atom 0)]
+ (dynamic-redefs/with-dynamic-fn-redefs
+ [data-access-token/sandbox-token-for-table (fn [table-id]
+ (when (= venues table-id) (swap! reads inc))
+ nil)]
+ (is (false? (visible? thread :rasta)) "the sandboxed creators' charts block an unsandboxed viewer"))
+ (is (= 1 @reads)))))
+
(deftest verdict-cost-grows-only-with-distinct-chart-shapes-test
(testing "grouping cannot be free: [[visibility-key]] resolves each chart's query, because the
- lens half of the verdict compares *resolved* tables and two charts can share a raw
- footprint while reading different ones. That resolution is per chart and mostly metadata
+ lens half of the verdict compares *resolved* tables and two charts can share raw
+ source ids while reading different ones. That resolution is per chart and mostly metadata
reads, so cost grows — but it must stay far below re-running the verdict itself, which
would be ~13 queries a chart (~325 for 25)."
(let [specs (fn [n] (repeat n {:creator-id (mt/user->id :lucky) :table :venues :token {}}))
@@ -301,6 +320,30 @@
(is (< large (* 2 small))
(format "resolution must stay cheap (2 charts: %d queries, 25 charts: %d)" small large)))))
+(deftest stored-resolved-source-ids-key-the-verdict-only-for-their-own-query-test
+ (let [thread (thread-with-snapshots! [{:creator-id (mt/user->id :lucky) :table :venues :token {}}])
+ q-id (t2/select-one-pk :model/ExplorationQuery :exploration_thread_id thread)
+ stamp! (fn [stamped-from]
+ (t2/update! :model/ExplorationQuery :id q-id
+ {:resolved_source_ids (query-perms/stamped-resolved-source-ids
+ (count-query stamped-from)
+ {:table-ids #{(mt/id stamped-from)}})}))
+ resolves (fn []
+ (let [calls (atom 0)
+ orig (dynamic-redefs/original-fn #'query-perms/query->resolved-source-ids)]
+ (dynamic-redefs/with-dynamic-fn-redefs
+ [queries/viewer-can-view-cached-result? (constantly true)
+ query-perms/query->resolved-source-ids (fn [& args] (swap! calls inc) (apply orig args))]
+ (request/with-current-user (mt/user->id :rasta)
+ (derived-perms/thread-ids-with-visible-derived-data [thread])))
+ @calls))]
+ (testing "a value stamped from the row's own query stands in for resolving it"
+ (stamp! :venues)
+ (is (zero? (resolves))))
+ (testing "a value stamped from another query is ignored, and the row's query is resolved live"
+ (stamp! :checkins)
+ (is (= 1 (resolves))))))
+
;;; --------------------------------- Summary-document content gate ---------------------------------
(defn- doc-content-visible? [document user-kw]
@@ -325,6 +368,27 @@
(is (true? (boolean (doc-content-visible? (t2/select-one :model/Document :id doc-id) :rasta))))
(is (true? (boolean (doc-content-visible? (t2/select-one [:model/Document :id :name] :id doc-id) :rasta)))))))
+(deftest superuser-skips-content-gate-test
+ (testing "a superuser passes the content gate without it loading anything to adjudicate"
+ (let [thread (thread-with-snapshots! [{:creator-id (mt/user->id :lucky) :table :venues :token nil}])
+ expl (t2/select-one-fn :exploration_id :model/ExplorationThread :id thread)
+ loads (atom 0)
+ counted (fn [f] (fn [& args] (swap! loads inc) (apply f args)))]
+ (mt/with-temp [:model/Document {doc-id :id} {:exploration_id expl :creator_id (mt/user->id :lucky)}]
+ (let [document (t2/select-one :model/Document :id doc-id)]
+ (dynamic-redefs/with-dynamic-fn-redefs
+ [explorations.db/thread-ids-for-exploration
+ (counted (dynamic-redefs/original-fn #'explorations.db/thread-ids-for-exploration))
+ explorations.db/lens-stamped-queries
+ (counted (dynamic-redefs/original-fn #'explorations.db/lens-stamped-queries))]
+ (testing "the gate denies everyone else here"
+ (is (false? (visible? thread :rasta)))
+ (is (false? (boolean (doc-content-visible? document :rasta))))
+ (reset! loads 0))
+ (is (true? (visible? thread :crowberto)))
+ (is (true? (boolean (doc-content-visible? document :crowberto))))
+ (is (zero? @loads))))))))
+
;;; --------------------------- Cards scoped to a gated Summary document ---------------------------
(defn- card-readable? [card-id user-kw]
@@ -416,9 +480,8 @@
(deftest content-gate-verdict-is-memoized-within-a-scope-test
(testing "every Card in a Summary document shares one document, and therefore one verdict. Without
- memoization each card re-runs the whole rollup — ~20 app-DB queries apiece — so a
- document's cards cost N times what one costs. Bulk read paths scope the cache
- themselves; see the callers of `with-content-gate-cache`."
+ memoization each card re-runs the whole rollup — ~20 app-DB queries apiece. Bulk read
+ paths scope the cache themselves; see the callers of `with-content-gate-cache`."
(let [thread (thread-with-snapshots! (repeat 5 {:creator-id (mt/user->id :lucky)
:table :venues :token {}}))
expl-id (t2/select-one-fn :exploration_id :model/ExplorationThread :id thread)]
@@ -435,14 +498,16 @@
:display "table" :visualization_settings {}
:collection_id coll-id :document_id doc-id})))]
(request/with-current-user (mt/user->id :rasta)
+ ;; Count calls to the registered gate rather than app-DB queries: each Card still reads
+ ;; its own document and snapshots, and the rollup's cost differs between editions.
(let [cards (t2/select :model/Card :id [:in card-ids])
- measure (fn [instances]
- (documents/with-content-gate-cache
- (t2/with-call-count [call-count]
- (doseq [card instances] (mi/can-read? card))
- (call-count))))
- one (measure [(first cards)])
- ten (measure cards)]
- (is (< ten (* 2 one))
- (format "one verdict should serve every card (1 card: %d queries, 10 cards: %d)"
- one ten)))))))))
+ gate @#'m.document/doc-content-visibility-fn
+ orig @gate
+ calls (atom 0)]
+ (try
+ (reset! gate (fn [doc] (swap! calls inc) (orig doc)))
+ (documents/with-content-gate-cache
+ (is (every? mi/can-read? cards)))
+ (is (= 1 @calls) "one verdict should serve every card")
+ (finally
+ (reset! gate orig))))))))))
diff --git a/test/metabase/explorations/runner_test.clj b/test/metabase/explorations/runner_test.clj
index cbb75e02dec..563069bc675 100644
--- a/test/metabase/explorations/runner_test.clj
+++ b/test/metabase/explorations/runner_test.clj
@@ -15,7 +15,11 @@
[metabase.metabot.self.claude]
[metabase.metabot.usage]
[metabase.permissions.core :as perms]
+ [metabase.queries.core :as queries]
+ [metabase.query-permissions.core :as query-perms]
[metabase.query-processor.core :as qp]
+ [metabase.query-processor.preprocess :as qp.preprocess]
+ [metabase.request.core :as request]
[metabase.test :as mt]
[metabase.test.util.dynamic-redefs :as dynamic-redefs]
[toucan2.core :as t2])
@@ -76,6 +80,14 @@
[eq-id]
(eqr/stored-results eq-id))
+(defn- preprocess-count
+ "Calls `thunk`, returning `[result number-of-preprocesses]`."
+ [thunk]
+ (let [calls (atom 0)
+ orig (dynamic-redefs/original-fn #'qp.preprocess/preprocess)]
+ (dynamic-redefs/with-dynamic-fn-redefs [qp.preprocess/preprocess (fn [& args] (swap! calls inc) (apply orig args))]
+ [(thunk) @calls])))
+
(deftest run-one-iteration-happy-path-test
(testing "A pending row gets executed, the linked stored_result holds result_data, and status flips to done"
(mt/with-temp [:model/User u {:email "[email protected]"}
@@ -100,6 +112,26 @@
(is (= 1 (:row_count sr))
"the QP's top-level :row_count (1 row for an unbucketed count) is persisted on the snapshot")))))
+(deftest run-one-iteration-stores-resolved-source-ids-on-the-stored-result-test
+ (let [venues-count (fn []
+ (lib/->legacy-MBQL
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count))))))]
+ (mt/with-temp [:model/User u {:email "[email protected]"}
+ :model/Card card {:type :metric :creator_id (:id u) :dataset_query (venues-count)}]
+ (let [thread (temp-thread! (:id u))
+ row (pending-query! (:id thread) (:id card) (venues-count))
+ _ (drain-until-terminal! (:id row))
+ sr (stored-result-for (:id row))]
+ (testing "the stored result carries the resolved source ids its token was computed over"
+ (is (= {:table-ids #{(mt/id :venues)} :table-query-ids #{(mt/id :venues)}}
+ (dissoc (:resolved_source_ids sr) :query-digest))))
+ (testing "the cached-result gate uses them rather than preprocessing the stored query"
+ (is (= [true 0]
+ (request/with-current-user (mt/user->id :rasta)
+ (preprocess-count #(queries/viewer-can-view-cached-result? sr))))))))))
+
(deftest run-one-iteration-records-stored-result-use-test
(testing "Running a query records a stored_result_use row tying the snapshot to the exploration"
(mt/with-temp [:model/User u {:email "[email protected]"}
@@ -382,6 +414,67 @@
(is (= (:id u) @seen)
"discovery must run as the exploration's creator, not with no user bound")))))
+(deftest finalize-row-stores-the-resolved-source-ids-test
+ (testing "finalizing a deferred row stores the resolved source ids its lens stamp was computed over beside the stamp"
+ (mt/with-temp [:model/User u {:email "[email protected]"}
+ :model/Card card {:type :metric
+ :creator_id (:id u)
+ :dataset_query (lib/->legacy-MBQL
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count)))))}]
+ (let [thread (temp-thread! (:id u))
+ row (deferred-query! (:id thread) (:id card))]
+ (with-redefs [qp.context/build-row-context (constantly {::stub true})
+ qp.variants/dataset-query (fn [_variant _ctx]
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :checkins)))
+ (lib/aggregate (lib/count)))))
+ qp.variants/query-name (constantly "stub name")]
+ (runner/run-query! (:id row)))
+ (is (= {:table-ids #{(mt/id :checkins)}
+ :table-query-ids #{(mt/id :checkins)}}
+ (-> (t2/select-one-fn :resolved_source_ids :model/ExplorationQuery :id (:id row))
+ (dissoc :query-digest))))))))
+
+(deftest stored-resolved-source-ids-stand-in-only-for-their-own-query-test
+ (mt/with-temp [:model/User u {:email "[email protected]"}
+ :model/Card card {:type :metric
+ :creator_id (:id u)
+ :dataset_query (lib/->legacy-MBQL
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count)))))}]
+ (let [thread (temp-thread! (:id u))
+ row (deferred-query! (:id thread) (:id card))
+ stored {:table-ids #{(mt/id :checkins)} :table-query-ids #{(mt/id :checkins)}}
+ resolve-reloaded (fn []
+ (let [reloaded (t2/select-one :model/ExplorationQuery :id (:id row))]
+ (preprocess-count
+ #(query-perms/query->resolved-source-ids (:dataset_query reloaded)
+ (:resolved_source_ids reloaded)))))]
+ (with-redefs [qp.context/build-row-context (constantly {::stub true})
+ qp.variants/dataset-query (fn [_variant _ctx]
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :checkins)))
+ (lib/aggregate (lib/count)))))
+ qp.variants/query-name (constantly "stub name")]
+ (runner/run-query! (:id row)))
+ (testing "the stored value is used for the query it was stamped from, as it reads back from the app DB"
+ (is (= [stored 0] (resolve-reloaded))))
+ (testing "once the row's query changes, the query is resolved live"
+ (t2/update! :model/ExplorationQuery :id (:id row)
+ {:dataset_query (lib/->legacy-MBQL
+ (let [mp (mt/metadata-provider)]
+ (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count)))))})
+ (let [[resolved preprocesses] (resolve-reloaded)]
+ (is (= #{(mt/id :venues)} (:table-ids resolved)))
+ (is (pos? preprocesses))))
+ (testing "a stored value without a digest is not used"
+ (t2/update! :model/ExplorationQuery :id (:id row) {:resolved_source_ids stored})
+ (is (= #{(mt/id :venues)} (:table-ids (first (resolve-reloaded)))))))))
+
(defn- store-fake-result!
[query-id qp-result]
(let [bytes (qp/do-with-serialization
diff --git a/test/metabase/permissions/data_access_token_test.clj b/test/metabase/permissions/data_access_token_test.clj
index 45f8b6e7347..b5db6b6014c 100644
--- a/test/metabase/permissions/data_access_token_test.clj
+++ b/test/metabase/permissions/data_access_token_test.clj
@@ -2,6 +2,7 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.api.common :as api]
[metabase.permissions.data-access-token :as data-access-token]
[metabase.test.util.dynamic-redefs :as dynamic-redefs]))
@@ -184,3 +185,52 @@
(deftest data-access-token-unparseable-reads-as-nil-test
(testing "an unparseable blob fails closed"
(is (nil? (transform-out "{not json ][")))))
+
+(deftest data-access-token-reader-reads-each-contributor-once-test
+ (testing "a reader shares contributor reads across its calls, and its tokens match [[data-access-token]]'s"
+ (let [reads (atom [])]
+ (dynamic-redefs/with-dynamic-fn-redefs
+ [data-access-token/sandbox-token-for-table (fn [table-id]
+ (swap! reads conj [:sandbox table-id])
+ [1 table-id])
+ data-access-token/impersonation-token-for-db (fn [db-id]
+ (swap! reads conj [:impersonation db-id])
+ {:role "ro"})
+ data-access-token/routing-token-for-db (fn [db-id] (swap! reads conj [:routing db-id]) nil)]
+ (let [reader (data-access-token/data-access-token-reader)
+ expected (data-access-token/data-access-token {:database-id 5 :table-ids #{10 20}})]
+ (reset! reads [])
+ (is (= expected (reader {:database-id 5 :table-ids #{10 20}})))
+ (is (= expected (reader {:database-id 5 :table-ids #{20 10}})))
+ (reader {:database-id 5 :table-ids #{10}})
+ (is (= {[:sandbox 10] 1 [:sandbox 20] 1 [:impersonation 5] 1 [:routing 5] 1}
+ (frequencies @reads))
+ "a nil contributor read is shared too")
+ (testing "a second reader shares nothing with the first"
+ (reset! reads [])
+ ((data-access-token/data-access-token-reader) {:database-id 5 :table-ids #{10}})
+ (is (= 3 (count @reads)))))))))
+
+(deftest data-access-token-reader-does-not-memoize-a-throw-test
+ (testing "a contributor that throws throws again on the next call rather than reading as a value"
+ (let [calls (atom 0)]
+ (dynamic-redefs/with-dynamic-fn-redefs
+ [data-access-token/sandbox-token-for-table (fn [_] nil)
+ data-access-token/impersonation-token-for-db (fn [_] nil)
+ data-access-token/routing-token-for-db (fn [_] (swap! calls inc) (throw (ex-info "no attribute" {})))]
+ (let [reader (data-access-token/data-access-token-reader)]
+ (is (thrown? clojure.lang.ExceptionInfo (reader {:database-id 5 :table-ids #{}})))
+ (is (thrown? clojure.lang.ExceptionInfo (reader {:database-id 5 :table-ids #{}})))
+ (is (= 2 @calls)))))))
+
+(deftest data-access-token-reader-keys-by-current-user-test
+ (testing "a reader called under a different user binding reads that user's lens rather than reusing another's"
+ (dynamic-redefs/with-dynamic-fn-redefs
+ [data-access-token/sandbox-token-for-table (fn [_] [api/*current-user-id*])
+ data-access-token/impersonation-token-for-db (fn [_] nil)
+ data-access-token/routing-token-for-db (fn [_] nil)]
+ (let [reader (data-access-token/data-access-token-reader)
+ token (fn [user-id] (binding [api/*current-user-id* user-id]
+ (reader {:database-id 5 :table-ids #{10}})))]
+ (is (not= (token 1) (token 2)))
+ (is (= (token 1) (token 1)))))))
diff --git a/test/metabase/queries/cached_result_test.clj b/test/metabase/queries/cached_result_test.clj
index d6af8efc8ec..3e11d548a5b 100644
--- a/test/metabase/queries/cached_result_test.clj
+++ b/test/metabase/queries/cached_result_test.clj
@@ -58,8 +58,8 @@
(testing "superusers may see every exploration — a deliberate product exemption, applied before
any lens comparison, so even a superuser whose own computed lens mismatches the
snapshot's (e.g. one who routed themselves to a destination db) still streams it"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly true)
- query-perms/query->resolved-source-table-ids (constantly #{1})
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly true)
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})
perms/data-access-token (constantly a-different-sandbox-token)]
(let [sr {:id 1 :database_id 1 :data_access_token a-sandbox-token :dataset_query a-query}]
(binding [api/*is-superuser?* true]
@@ -72,7 +72,8 @@
(testing "a snapshot with no token can't be compared against any viewer lens — and the write path
never persists one, so it is a bug, logged at ERROR. Non-admins are denied; a superuser
still streams it via the bypass"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly true)]
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly true)
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})]
(let [sr {:id 1 :database_id 1 :data_access_token nil :dataset_query a-query}]
(binding [api/*is-superuser?* true]
(is (true? (cached-result/viewer-can-view-cached-result? sr))))
@@ -86,26 +87,26 @@
resolves to tables (deleted card). Either throw denies the (non-admin) viewer — never a
500 out of an authorization gate; superusers never reach the check (bypass)"
(let [assert-superuser-only!
- (fn []
+ (fn [reason]
(let [sr {:id 1 :database_id 1 :data_access_token a-sandbox-token :dataset_query a-query}]
(binding [api/*is-superuser?* true]
(is (true? (cached-result/viewer-can-view-cached-result? sr))))
(binding [api/*is-superuser?* false]
(is (false? (cached-result/viewer-can-view-cached-result? sr)))
- (is (= {:status-code 403 :reason :incompatible-context :stored-result-id 1}
+ (is (= {:status-code 403 :reason reason :stored-result-id 1}
(denial! sr))))))]
;; both throw sites are exercised: each redef below shadows the working one installed outside.
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly true)
- query-perms/query->resolved-source-table-ids (constantly #{1})
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly true)
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})
perms/data-access-token (constantly a-sandbox-token)]
- (testing "resolving the query's source tables throws (deleted source card)"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/query->resolved-source-table-ids
- (fn [_] (throw (ex-info "Card 7 does not exist." {})))]
- (assert-superuser-only!)))
+ (testing "resolving the query's source ids throws (deleted source card)"
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/query->resolved-source-ids
+ (fn [& _] (throw (ex-info "Card 7 does not exist." {})))]
+ (assert-superuser-only! :no-data-perms)))
(testing "computing the viewer's token throws (missing routing/impersonation attribute)"
(dynamic-redefs/with-dynamic-fn-redefs [perms/data-access-token
(fn [_] (throw (ex-info "Required user attribute is missing" {})))]
- (assert-superuser-only!)))))))
+ (assert-superuser-only! :incompatible-context)))))))
(deftest data-perms-check-throwing-denies-non-admins-test
(testing "the data-perms dimension must fail the same controlled way the lens dimension does. A
@@ -116,8 +117,9 @@
(doseq [thrown [(NullPointerException. "boom")
(ex-info "malformed query" {})]]
(testing (str "threw " (class thrown))
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (fn [_] (throw thrown))
- query-perms/query->resolved-source-table-ids (constantly #{1})
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids?
+ (fn [& _] (throw thrown))
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})
perms/data-access-token (constantly a-sandbox-token)]
(let [sr {:id 1 :database_id 1 :data_access_token a-sandbox-token :dataset_query a-query}]
(binding [api/*is-superuser?* true]
@@ -126,18 +128,35 @@
(is (false? (cached-result/viewer-can-view-cached-result? sr)))
(is (= :no-data-perms (:reason (denial! sr)))))))))))
+(deftest empty-resolved-source-ids-deny-non-admins-test
+ (testing "resolved source ids that resolve to nothing deny like ones that failed to resolve, before the
+ data-perms check is consulted"
+ (doseq [[label stored live] [["stored" {} {:table-ids #{1}}]
+ ["resolved live" nil {}]]]
+ (testing label
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly true)
+ query-perms/query->resolved-source-ids
+ (fn [_ stored'] (or stored' live))
+ perms/data-access-token (constantly a-sandbox-token)]
+ (let [sr {:id 1 :database_id 1 :data_access_token a-sandbox-token :dataset_query a-query
+ :resolved_source_ids stored}]
+ (binding [api/*is-superuser?* false]
+ (is (= :no-data-perms (:reason (denial! sr)))))
+ (binding [api/*is-superuser?* true]
+ (is (true? (cached-result/viewer-can-view-cached-result? sr))))))))))
+
(deftest denial-reasons-are-distinct-test
(testing "the two denial reasons are reported distinctly, so they can't be collapsed into one
message by a refactor: the caller and the i18n string both key off `:reason`"
(binding [api/*is-superuser?* false]
(testing "lacking data perms on the underlying query"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly false)]
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly false)]
(is (= {:status-code 403 :reason :no-data-perms :stored-result-id 7}
(denial! {:id 7 :database_id 1 :data_access_token a-sandbox-token
:dataset_query a-query})))))
(testing "holding the perms but under an incompatible lens"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly true)
- query-perms/query->resolved-source-table-ids (constantly #{1})
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly true)
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})
perms/data-access-token (constantly a-different-sandbox-token)]
(is (= {:status-code 403 :reason :incompatible-context :stored-result-id 7}
(denial! {:id 7 :database_id 1 :data_access_token a-sandbox-token
@@ -147,8 +166,8 @@
(testing "reason priority is part of the contract: a viewer who fails both checks is told they lack
data perms rather than being handed the lens-mismatch message, which would imply the data
is theirs to see under a different lens"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly false)
- query-perms/query->resolved-source-table-ids (constantly #{1})
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly false)
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})
perms/data-access-token (constantly a-different-sandbox-token)]
(binding [api/*is-superuser?* false]
(is (= :no-data-perms
@@ -158,8 +177,8 @@
(deftest compatible-viewer-is-allowed-test
(testing "control: perms held and lenses matching means the blob streams — the gate must not be
trivially satisfiable by denying everything"
- (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query? (constantly true)
- query-perms/query->resolved-source-table-ids (constantly #{1})
+ (dynamic-redefs/with-dynamic-fn-redefs [query-perms/can-run-query-with-resolved-source-ids? (constantly true)
+ query-perms/query->resolved-source-ids (constantly {:table-ids #{1}})
perms/data-access-token (constantly a-sandbox-token)]
(binding [api/*is-superuser?* false]
(let [sr {:id 1 :database_id 1 :data_access_token a-sandbox-token :dataset_query a-query}]
diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.clj
index b202d542ec1..d000cc605cc 100644
--- a/test/metabase/queries/models/card_test.clj
+++ b/test/metabase/queries/models/card_test.clj
@@ -2162,6 +2162,32 @@
"a card belonging to an exploration Summary is never exported — its name and dataset_query
carry values discovered under the creator's lens, and its parent document is excluded")))))
+(deftest exploration-summary-cards-are-never-writable-test
+ (testing "a Summary card is writable by nobody, superusers included"
+ (mt/with-temp [:model/Collection {coll-id :id} {}
+ :model/Exploration {expl-id :id} {:name "Explo" :creator_id (mt/user->id :rasta)}
+ :model/Document {summary-id :id} {:name "Summary"
+ :creator_id (mt/user->id :rasta)
+ :collection_id coll-id
+ :exploration_id expl-id}
+ :model/Document {plain-doc :id} {:name "Plain doc" :creator_id (mt/user->id :rasta)
+ :collection_id coll-id}
+ :model/Card {doc-card :id} {:collection_id coll-id :document_id plain-doc
+ :creator_id (mt/user->id :rasta)}
+ :model/Card {summary-card :id} {:collection_id coll-id :document_id summary-id
+ :creator_id (mt/user->id :rasta)}]
+ (doseq [user-kw [:rasta :crowberto]]
+ (testing user-kw
+ (testing "control: a card in an ordinary document in the same collection is writable"
+ (is (true? (mt/with-current-user (mt/user->id user-kw)
+ (mi/can-write? (t2/select-one :model/Card :id doc-card))))))
+ (testing "through mi/can-write?"
+ (is (false? (mt/with-current-user (mt/user->id user-kw)
+ (mi/can-write? (t2/select-one :model/Card :id summary-card))))))
+ (testing "through PUT /api/card/:id"
+ (mt/user-http-request user-kw :put 403 (str "card/" summary-card) {:name "Renamed"})
+ (is (not= "Renamed" (t2/select-one-fn :name :model/Card :id summary-card)))))))))
+
(deftest before-update-schema-upgrade-uses-incoming-query-test
(testing "a query edit to a legacy metric persists dimensions derived from the new query, not the old one"
(mt/with-temp [:model/Card {card-id :id} {:type :metric
diff --git a/test/metabase/queries/models/stored_result_use_test.clj b/test/metabase/queries/models/stored_result_use_test.clj
index d3cc61eb8a9..588289373ba 100644
--- a/test/metabase/queries/models/stored_result_use_test.clj
+++ b/test/metabase/queries/models/stored_result_use_test.clj
@@ -4,6 +4,7 @@
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.queries.core :as queries]
+ [metabase.queries.db :as queries.db]
[metabase.request.core :as request]
[metabase.test :as mt]
[toucan2.core :as t2]))
@@ -37,6 +38,14 @@
:card_id (:id card)
:exploration_id (:id expl)}))))))))
+(deftest stored-result-gate-inputs-for-card-test
+ (testing "a card's snapshots are read without their result blob"
+ (mt/with-temp [:model/Card card {}]
+ (let [sr-id (stored-result!)]
+ (t2/insert! :model/StoredResultUse {:stored_result_id sr-id :card_id (:id card)})
+ (is (= [#{:id :database_id :dataset_query :data_access_token :resolved_source_ids}]
+ (map (comp set keys) (queries.db/stored-result-gate-inputs-for-card (:id card)))))))))
+
(deftest exploration-reference-test
(testing "exploration_id only succeeds"
(mt/with-temp [:model/User u {}
diff --git a/test/metabase/query_permissions/impl_test.clj b/test/metabase/query_permissions/impl_test.clj
index b47e07b06c2..491c22e6b35 100644
--- a/test/metabase/query_permissions/impl_test.clj
+++ b/test/metabase/query_permissions/impl_test.clj
@@ -12,7 +12,8 @@
[metabase.query-processor.preprocess :as qp.preprocess]
[metabase.query-processor.test-util :as qp.test-util]
[metabase.test :as mt]
- [metabase.util :as u]))
+ [metabase.util :as u]
+ [metabase.util.log.capture :as log.capture]))
;;; ---------------------------------------------- Permissions Checking ----------------------------------------------
@@ -244,18 +245,18 @@
(qp.preprocess/preprocess
query)))))))))
-(deftest ^:parallel query->resolved-source-table-ids-test
+(deftest ^:parallel query->resolved-source-ids-test
(testing "table-sourced queries behave like query->source-table-ids"
(is (= #{(mt/id :venues)}
- (query-perms/query->resolved-source-table-ids (mt/mbql-query venues)))))
+ (:table-ids (query-perms/query->resolved-source-ids (mt/mbql-query venues))))))
(testing "card-sourced queries resolve to the card's underlying source table"
(mt/with-temp [:model/Card card {:dataset_query {:database (mt/id)
:type :query
:query {:source-table (mt/id :venues)}}}]
(is (= #{(mt/id :venues)}
- (query-perms/query->resolved-source-table-ids (query-with-source-card card)))))))
+ (:table-ids (query-perms/query->resolved-source-ids (query-with-source-card card))))))))
-(deftest ^:parallel query->resolved-source-table-ids-nested-cards-test
+(deftest ^:parallel query->resolved-source-ids-nested-cards-test
(testing "card-on-card chains resolve recursively down to the physical table"
(mt/with-temp [:model/Card {card-a-id :id} {:dataset_query {:database (mt/id)
:type :query
@@ -264,30 +265,41 @@
:type :query
:query {:source-table (str "card__" card-a-id)}}}]
(is (= #{(mt/id :venues)}
- (query-perms/query->resolved-source-table-ids (query-with-source-card card-b)))))))
+ (:table-ids (query-perms/query->resolved-source-ids (query-with-source-card card-b))))))))
-(deftest ^:parallel query->resolved-source-table-ids-join-test
+(deftest ^:parallel query->resolved-source-ids-join-test
(testing "a card-sourced JOIN contributes the card's underlying table too"
(mt/with-temp [:model/Card {card-id :id} (qp.test-util/card-with-source-metadata-for-query
(mt/mbql-query venues
{:aggregation [[:count]]
:breakout [$id]}))]
- (is (= #{(mt/id :checkins) (mt/id :venues)}
- (query-perms/query->resolved-source-table-ids
- (mt/mbql-query checkins
- {:joins [{:fields :all
- :alias "v"
- :source-table (str "card__" card-id)
- :condition [:= $venue_id [:field "ID" {:base-type :type/Integer, :join-alias "v"}]]}]})))))))
+ (let [query (mt/mbql-query checkins
+ {:joins [{:fields :all
+ :alias "v"
+ :source-table (str "card__" card-id)
+ :condition [:= $venue_id [:field "ID" {:base-type :type/Integer, :join-alias "v"}]]}]})]
+ (is (= #{(mt/id :checkins) (mt/id :venues)}
+ (:table-ids (query-perms/query->resolved-source-ids query))))))))
-(deftest ^:parallel query->resolved-source-table-ids-missing-card-test
+(deftest ^:parallel query->resolved-source-ids-missing-card-test
(testing "an unresolvable source-card chain THROWS (fail closed) rather than yielding no tables"
(is (thrown? Exception
- (query-perms/query->resolved-source-table-ids
+ (query-perms/query->resolved-source-ids
{:database (mt/id)
:type :query
:query {:source-table (str "card__" Integer/MAX_VALUE)}})))))
+(deftest empty-resolved-source-ids-deny-test
+ (testing "empty resolved source ids deny before the data-perms check"
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [query (mt/mbql-query venues)]
+ (is (true? (query-perms/can-run-query-with-resolved-source-ids?
+ query (query-perms/query->resolved-source-ids query))))
+ (log.capture/with-log-messages-for-level [messages [metabase.query-permissions.impl :debug]]
+ (is (false? (query-perms/can-run-query-with-resolved-source-ids? query {})))
+ (is (some #(= "Empty resolved source ids; denying" (:message %)) (messages))
+ "denied as empty, not by a later check tripping over the absent ids"))))))
+
(deftest ^:parallel mbql5-query-test
(testing "Should be able to calculate permissions for a MBQL 5 query (#39024)"
(let [metadata-provider (mt/metadata-provider)
@@ -397,3 +409,17 @@
clojure.lang.ExceptionInfo
#"You do not have permission to view data of table 1595 in result_metadata\."
(query-perms/check-result-metadata-data-perms (mt/id) [{:name "NAME", :table_id 1595}])))))))
+
+(deftest ^:parallel merge-resolved-source-ids-test
+ (is (nil? (query-perms/merge-resolved-source-ids [])))
+ (is (nil? (query-perms/merge-resolved-source-ids [nil])))
+ (is (= {} (query-perms/merge-resolved-source-ids [{} nil])))
+ (is (= {:table-ids #{1 2 3}
+ :table-query-ids #{1 3}
+ :card-ids #{10}
+ :native? true
+ :referenced-card-ids #{20 21}}
+ (query-perms/merge-resolved-source-ids
+ [{:table-ids #{1 2} :table-query-ids #{1} :native? false :referenced-card-ids #{20}}
+ nil
+ {:table-ids #{3} :table-query-ids #{3} :card-ids #{10} :native? true :referenced-card-ids #{21}}]))))