The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Show unreadable stored series cards as ids in dry runs (#86169)
Chris Truter64b5913c7504
diff --git a/src/metabase/mcp/v2/dashboard_errors.clj b/src/metabase/mcp/v2/dashboard_errors.cljindex 8832fd1f4c3..75f384e4d9d 100644--- a/src/metabase/mcp/v2/dashboard_errors.clj+++ b/src/metabase/mcp/v2/dashboard_errors.clj@@ -14,8 +14,8 @@(def ^:private details"For each error code in [[dashboard-ops/errors]], a function from the error data to the message explaining it.The message has no op prefix."- {;; MCP's registry checks the same op schema first, so MCP's own calls never reach this. It is worded for any- ;; caller of the toolkit that skips that check, and because every code needs a wording.+ {;; MCP's registry checks the op schema first, so for MCP this comes from the compiler's own checks, such as a+ ;; patched mapping the save would refuse. A caller that skips the registry also gets it for a malformed op.:invalid-op(fn [{:keys [explanation]}](message/msg ["invalid op: %s — see the tool description for each op's arguments."]diff --git a/src/metabase/mcp/v2/redaction.clj b/src/metabase/mcp/v2/redaction.cljindex fb5b1821743..c5a5300d11e 100644--- a/src/metabase/mcp/v2/redaction.clj+++ b/src/metabase/mcp/v2/redaction.clj@@ -17,7 +17,8 @@[metabase.models.interface :as mi][metabase.notification.models :as models.notification][metabase.permissions.core :as perms]- [metabase.pulse.core :as pulse]))+ [metabase.pulse.core :as pulse]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -29,33 +30,42 @@Layout keys (sizing, container styling) describe the dashboard itself and survive redaction."[:card.title :card.description :column_settings :series_settings :click_behavior])+(defn- readable-card?+ "Whether `card`, a dashcard's `:card` or an entry in its `:series`, is a card row the current user can read."+ [card]+ ;; A compiled dry run leaves a card as nil or a bare `{:id}` when it has no row for it, as for a card the user+ ;; cannot read that the dashboard references only as a parameter's values source. A bare map is not a model+ ;; instance, so `mi/can-read?` would throw on it.+ (and (t2/instance-of? :model/Card card) (mi/can-read? card)))+(defn- redact-dashcard[dashcard]- (cond-> dashcard- ;; The projection reads an absent `:card` the same way it reads an unhydrated one — as an id- ;; with no name — so removing it is the whole redaction *for the projection*. The `layout`- ;; include reads this same dashcard directly, though, and `:visualization_settings` /- ;; `:parameter_mappings` are stored snapshots that describe the card: the title, the warehouse- ;; column names it renders, and the field ids its filters are wired to. Leaving them behind- ;; hands a caller who was just denied the card exactly what the denial withheld, so they go- ;; with it.- (not (some-> (:card dashcard) mi/can-read?))- ;; `:parameter_mappings` is emptied rather than removed: it is a required key on a DashboardCard,- ;; and `dashboard->resolved-params` — which the parameters summary runs over the same row — fails- ;; its schema when the key is absent.- (-> (dissoc :card)- (assoc :parameter_mappings []))-- ;; Guarded on there being something to strip rather than on the key's presence: a dashcard read- ;; from the app db is a Toucan instance, where `update` on an absent key still writes it back as- ;; nil and the projection then reports a key the dashcard never had.- (and (not (some-> (:card dashcard) mi/can-read?))- (seq (:visualization_settings dashcard)))- (assoc :visualization_settings- (not-empty (apply dissoc (:visualization_settings dashcard) card-describing-viz-keys)))-- (seq (:series dashcard))- (update :series (partial mapv #(cond-> % (not (mi/can-read? %)) (select-keys [:id]))))))+ ;; A text, heading or link card has no card, so its mappings and settings are dashboard data and stay.+ (let [hidden-card? (and (:card_id dashcard) (not (readable-card? (:card dashcard))))]+ (cond-> dashcard+ ;; The projection reads an absent `:card` the same way it reads an unhydrated one — as an id+ ;; with no name — so removing it is the whole redaction *for the projection*. The `layout`+ ;; include reads this same dashcard directly, though, and `:visualization_settings` /+ ;; `:parameter_mappings` are stored snapshots that describe the card: the title, the warehouse+ ;; column names it renders, and the field ids its filters are wired to. Leaving them behind+ ;; hands a caller who was just denied the card exactly what the denial withheld, so they go+ ;; with it.+ hidden-card?+ ;; `:parameter_mappings` is emptied rather than removed: it is a required key on a DashboardCard,+ ;; and `dashboard->resolved-params` — which the parameters summary runs over the same row — fails+ ;; its schema when the key is absent.+ (-> (dissoc :card)+ (assoc :parameter_mappings []))++ ;; Guarded on there being something to strip rather than on the key's presence: a dashcard read+ ;; from the app db is a Toucan instance, where `update` on an absent key still writes it back as+ ;; nil and the projection then reports a key the dashcard never had.+ (and hidden-card? (seq (:visualization_settings dashcard)))+ (assoc :visualization_settings+ (not-empty (apply dissoc (:visualization_settings dashcard) card-describing-viz-keys)))++ (seq (:series dashcard))+ (update :series (partial mapv #(cond-> % (not (readable-card? %)) (select-keys [:id])))))))(defn redact-dashboard"`dash`, with its dashcards' `:card` and `:series` populated — by hydration on a saved dashboard,diff --git a/src/metabase/metabot/toolkit/dashboard_ops.clj b/src/metabase/metabot/toolkit/dashboard_ops.cljindex bfc1be2307a..d7ff1d5991c 100644--- a/src/metabase/metabot/toolkit/dashboard_ops.clj+++ b/src/metabase/metabot/toolkit/dashboard_ops.clj@@ -452,11 +452,14 @@(nil? (:card_id mapping)) (assoc :card_id (:card_id dc))))(defn- check-patched-mapping!- "Throws unless `mapping`, from [[patched-mapping]], is a mapping the save accepts, for a parameter on the dashboard- and a card `dc` shows."- [state dc mapping]+ "Throws unless `mapping`, the patch's mapping at `idx` as [[patched-mapping]] shapes it, is one the save accepts,+ for a parameter on the dashboard and a card `dc` shows."+ [state dc idx mapping](when-let [explanation (mr/explain ::parameters.schema/parameter-mapping mapping)]- (fail! :invalid-op {:explanation (me/humanize explanation)}))+ ;; Placed at the mapping's path in the op, as the op schema's own errors are, so a patch with several mappings+ ;; says which one failed.+ (fail! :invalid-op {:explanation {:patch {:parameter_mappings (assoc (vec (repeat idx nil))+ idx (me/humanize explanation))}}}))(resolve-parameter! state (:parameter_id mapping));; The save would otherwise filter the primary card for a mapping that names a card the dashcard doesn't show.(when (and (:card_id mapping)@@ -478,9 +481,9 @@stored (into #{} (map (partial patched-mapping dc)) (:parameter_mappings dc))];; Only new or changed mappings are checked. The patch replaces the whole list, so a stale stored mapping sent;; back unchanged would otherwise block an unrelated edit.- (doseq [m mappings- :when (not (contains? stored m))]- (check-patched-mapping! state dc m))+ (doseq [[idx m] (map-indexed vector mappings)+ :when (not (contains? stored m))]+ (check-patched-mapping! state dc idx m))(update-row state :dashcards dashcard_id(fn [dc](cond-> (merge dc (dissoc patch :visualization_settings :parameter_mappings))diff --git a/src/metabase/metabot/toolkit/dashboard_write.clj b/src/metabase/metabot/toolkit/dashboard_write.cljindex ac2372b451d..270548b6a00 100644--- a/src/metabase/metabot/toolkit/dashboard_write.clj+++ b/src/metabase/metabot/toolkit/dashboard_write.clj@@ -107,8 +107,15 @@(defn- dry-run-dashboard"The dashboard saving `attrs` and the compiled `payload` to `dash` would produce, shaped like a saved one."[dash attrs payload cards]- ;; The payload's series carry ids only, so the rows the dashboard was hydrated with stand in for the stored ones.- (let [cards (merge (into {} (comp (mapcat :series) (map (juxt :id identity))) (:dashcards dash)) cards)]+ ;; The payload's series carry ids only, so the card rows the dashboard was hydrated with stand in for the stored+ ;; ones. A stored card the user cannot read is never fetched, so one the dashboard doesn't show stays a bare+ ;; `{:id}`, such as a parameter's values source. MCP's redaction reads that as unreadable.+ (let [stored (into {}+ (comp (mapcat #(cons (:card %) (:series %)))+ (remove nil?)+ (map (juxt :id identity)))+ (:dashcards dash))+ cards (merge stored cards)](-> (merge dash attrs payload)(update :dashcards (partial mapv (partial attach-card-metadata cards))))))@@ -139,13 +146,13 @@It checks write permission on the dashboard, or create permission on the collection, first after the ops' shape.A dry run skips the save's other permission checks, such as moving to a collection and per-field mapping checks.Ops edit the layout and parameters, so `attrs` must not carry `:dashcards`, `:tabs` or `:parameters`.- A nil `ops` means none were sent; anything else must be a list of ops.+ A nil `attrs` or `ops` means none were sent; `ops` otherwise must be a list of ops.Throws an error from [[dashboard-ops/errors]] when an op is invalid or the save would be rejected."[{:keys [dashboard validate-only?] :as opts}];; Prepared before any branch reads them, so a malformed `ops` fails as :invalid-op rather than crashing a `seq`;; or passing as no ops.(let [ops (dashboard-ops/prepare-ops (:ops opts))- attrs (:attrs opts)+ attrs (or (:attrs opts) {})_ (check-permission! dashboard attrs);; Sent as attributes, `:dashcards` would replace the layout behind the ops' back, and `:tabs` would be;; dropped silently. Compiled against the parameters in attrs, a source card in them would count as alreadydiff --git a/test/metabase/mcp/v2/redaction_test.clj b/test/metabase/mcp/v2/redaction_test.cljindex 729e88f75ee..61990ffb5a0 100644--- a/test/metabase/mcp/v2/redaction_test.clj+++ b/test/metabase/mcp/v2/redaction_test.clj@@ -255,7 +255,7 @@:col 0:size_x 4:size_y 4- :card {:id 12 :name "Executive Payroll"}+ :card (t2/instance :model/Card {:id 12 :name "Executive Payroll"}):visualization_settings {:card.title "Executive Payroll by Employee":column_settings {"[\"name\",\"base_salary\"]"{:column_title "Salary"}}@@ -284,4 +284,22 @@(testing "readable: nothing is stripped"(let [dc (redact true)](is (= "Executive Payroll by Employee" (get-in dc [:visualization_settings :card.title])))- (is (some? (:parameter_mappings dc))))))))+ (is (some? (:parameter_mappings dc)))))+ (testing "a bare map in place of a card row is redacted, without asking `mi/can-read?`"+ (let [dc (with-redefs [mi/can-read? (fn [_] (throw (ex-info "can-read? called" {})))]+ (first (:dashcards (redaction/redact-dashboard+ {:dashcards [(assoc dashcard+ :card {:id 12}+ :series [{:id 99, :name "Hidden"}])]}))))]+ (is (nil? (:card dc)))+ (is (= [] (:parameter_mappings dc)))+ (is (= [{:id 99}] (:series dc))))))))++(deftest virtual-dashcard-keeps-its-mappings-test+ (testing "a text card has no card to hide, so its filter wiring and settings stay"+ (let [dashcard {:id 2+ :card_id nil+ :card nil+ :visualization_settings {:text "{{x}}", :click_behavior {:type "link"}}+ :parameter_mappings [{:parameter_id "p", :card_id nil, :target [:text-tag "x"]}]}]+ (is (= dashcard (first (:dashcards (redaction/redact-dashboard {:dashcards [dashcard]}))))))))diff --git a/test/metabase/mcp/v2/tools/dashboard_test.clj b/test/metabase/mcp/v2/tools/dashboard_test.cljindex 3a9f5523847..7d50e2a54ec 100644--- a/test/metabase/mcp/v2/tools/dashboard_test.clj+++ b/test/metabase/mcp/v2/tools/dashboard_test.clj@@ -295,6 +295,32 @@(is (= {:id hidden-card} (:card dc))"the unreadable card comes back as an id with no name"))))))+(deftest set-series-with-an-unreadable-stored-card-test+ ;; The save leaves stored card references alone, so a writer can overlay a card the dashboard already holds even+ ;; when they cannot read it. The dry run fetches no row for that card.+ (testing "a dry run overlays an unreadable stored card as an id, and the save goes through"+ (mt/with-temp [:model/Collection {open-id :id} {}+ :model/Collection {locked-id :id} {}+ :model/Dashboard {dash-id :id} {:name "Sales" :collection_id open-id}+ :model/Card {open-card :id} {:name "Revenue" :collection_id open-id}+ :model/Card {hidden-card :id} {:name "CONFIDENTIAL" :collection_id locked-id}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id hidden-card :row 0 :col 0}+ :model/DashboardCard {open-dc :id} {:dashboard_id dash-id :card_id open-card :row 4 :col 0}]+ (mt/with-non-admin-groups-no-collection-perms locked-id+ (doseq [validate-only? [true false]]+ (testing (str "validate_only " validate-only?)+ (let [result (tool-result (call-tool! :rasta v2.tu/all-scopes "dashboard_write"+ (wire {:method "update"+ :id dash-id+ :validate_only validate-only?+ :ops [{:op "set_series"+ :dashcard_id open-dc+ :card_ids [hidden-card]}]})))]+ (is (= [{:id hidden-card}]+ (:series (first (filter #(= open-dc (:id %)) (:dashcards result)))))))))+ (is (= [hidden-card] (t2/select-fn-vec :card_id :model/DashboardCardSeries :dashboardcard_id open-dc))+ "the save went through")))))+(deftest parameter-ops-accept-json-shapes-test(testing "GHY-4147: a parameter's JSON-shaped properties are coerced to the shape the REST save stores"(mt/with-temp [:model/Dashboard dash {:name "Sales"}]diff --git a/test/metabase/metabot/toolkit/dashboard_ops_test.clj b/test/metabase/metabot/toolkit/dashboard_ops_test.cljindex 4c6f6e5f6e5..9bc59c97257 100644--- a/test/metabase/metabot/toolkit/dashboard_ops_test.clj+++ b/test/metabase/metabot/toolkit/dashboard_ops_test.clj@@ -518,10 +518,13 @@(is (=? {::dashboard-ops/error :unknown-parameter, :parameter-id "nope", :op-index 0}(op-error (p1-dash a-dashcard)[(patch-mappings [{:parameter_id "nope" :target ["dimension" ["field" 1 nil]]}])]))))- (testing "a new mapping the save would refuse fails in the op, with its index"- (is (=? {::dashboard-ops/error :invalid-op, :op-index 0, :explanation {:target some?}}+ (testing "a new mapping the save would refuse fails in the op, explained at the mapping's index in the patch"+ (is (=? {::dashboard-ops/error :invalid-op+ :op-index 0+ :explanation {:patch {:parameter_mappings [nil {:target some?}]}}}(op-error (p1-dash a-dashcard)- [(patch-mappings [{:parameter_id "p1" :target ["nonsense"]}])]))))+ [(patch-mappings [{:parameter_id "p1" :target ["dimension" ["field" 1 nil]]}+ {:parameter_id "p1" :target ["nonsense"]}])]))))(testing "a mapping takes only its three keys"(is (=? {::dashboard-ops/error :invalid-op, :op-index 0}(op-error (p1-dash a-dashcard)diff --git a/test/metabase/metabot/toolkit/dashboard_write_test.clj b/test/metabase/metabot/toolkit/dashboard_write_test.cljindex f5509ec5567..919750a3525 100644--- a/test/metabase/metabot/toolkit/dashboard_write_test.clj+++ b/test/metabase/metabot/toolkit/dashboard_write_test.clj@@ -7,6 +7,7 @@[metabase.metabot.toolkit.dashboard-ops :as dashboard-ops][metabase.metabot.toolkit.dashboard-write :as dashboard-write][metabase.test :as mt]+ [metabase.util :as u][toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -100,7 +101,27 @@{:dashboard (hydrated (:id dash)):attrs {}:ops [{:op "add_parameter" :parameter_id "p" :name "P" :type "not-a-type"}]}))))- (is (empty? (:parameters (hydrated (:id dash)))))))))+ (is (empty? (:parameters (hydrated (:id dash))))))))+ (testing "a create's compiled payload is checked as an update's is, on a dry run as on a save"+ (mt/with-current-user (mt/user->id :crowberto)+ (doseq [validate-only? [true false]]+ (testing (if validate-only? "dry run" "save")+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {:parameters some?}}+ (error-data #(dashboard-write/write!+ {:attrs {:name "Invalid parameters"}+ :ops [{:op "add_parameter" :parameter_id "p" :name "P" :type "not-a-type"}]+ :validate-only? validate-only?}))))))+ (is (not (t2/exists? :model/Dashboard :name "Invalid parameters"))))))++(deftest missing-attrs-test+ (testing "attrs left out are none, so an update with only ops saves them and a create reports its missing name"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Dashboard dash {:name "Sales"}]+ (is (=? {:name "Sales", :dashcards [{:visualization_settings {:text "Hello"}}]}+ (dashboard-write/write! {:dashboard (hydrated (:id dash))+ :ops [{:op "add_text" :id -1 :markdown "Hello"}]}))))+ (is (=? {::dashboard-ops/error :invalid-payload, :explanation {:name some?}}+ (error-data #(dashboard-write/write! {:ops [{:op "add_text" :id -1 :markdown "Hello"}]})))))))(deftest unreadable-card-test(testing "a card the user cannot read is refused as unknown, before any write"@@ -216,3 +237,21 @@:dashcard_id (:id dc):size {:size_x 6 :size_y 4}}]:validate-only? true})))))))++(deftest dry-run-overlays-a-stored-card-as-its-row-test+ (testing "a dry run overlaying a card the dashboard shows elsewhere returns its row, readable or not"+ (mt/with-temp [:model/Collection locked {}+ :model/Card hidden {:name "Hidden", :collection_id (:id locked)}+ :model/Card card {:name "Orders"}+ :model/Dashboard dash {:name "Sales"}+ :model/DashboardCard _ {:dashboard_id (:id dash), :card_id (:id hidden)}+ :model/DashboardCard dc {:dashboard_id (:id dash), :card_id (:id card), :row 4}]+ (mt/with-non-admin-groups-no-collection-perms locked+ (mt/with-current-user (mt/user->id :rasta)+ (let [result (dashboard-write/write! {:dashboard (hydrated (:id dash))+ :ops [{:op "set_series"+ :dashcard_id (:id dc)+ :card_ids [(:id hidden)]}]+ :validate-only? true})]+ (is (=? [{:id (:id hidden), :name "Hidden"}]+ (:series (u/find-first-map (:dashcards result) [:id] (:id dc)))))))))))