Skip to content

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

Reject replacing a dashcard's card with a question internal to another dashboard (#77236)

Andrei Chernykh18f5a74df742
diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.clj
index b068123e525..74fdd1f3e2a 100644
--- a/src/metabase/dashboards_rest/api.clj
+++ b/src/metabase/dashboards_rest/api.clj
@@ -860,10 +860,10 @@
(dashboard-card/delete-dashboard-cards! dashcard-ids)
dashboard-cards))
-(defn- assert-new-dashcards-are-not-internal-to-other-dashboards [dashboard to-create]
+(defn- assert-dashcards-are-not-internal-to-other-dashboards [dashboard dashcards]
(when-let [card-ids (seq (concat
- (seq (keep :card_id to-create))
- (->> to-create
+ (seq (keep :card_id dashcards))
+ (->> dashcards
(mapcat :series)
(keep :id))))]
(api/check-400 (not (t2/exists? :model/Card
@@ -876,7 +876,9 @@
[dashboard current-cards new-cards]
(let [{:keys [to-create to-update to-delete]} (u/row-diff current-cards new-cards)]
(dashboard/archive-or-unarchive-internal-dashboard-questions! (:id dashboard) new-cards)
- (assert-new-dashcards-are-not-internal-to-other-dashboards dashboard to-create)
+ ;; Check both created and updated dashcards: a "Replace" keeps the dashcard id and only swaps
+ ;; card_id, so it lands in `to-update`, not `to-create` (UXW-4731).
+ (assert-dashcards-are-not-internal-to-other-dashboards dashboard (concat to-create to-update))
(when (seq to-update)
(update-dashcards! dashboard to-update))
{:deleted-dashcards (when (seq to-delete)
diff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.clj
index 7ff5dfd0df4..6722c7711a8 100644
--- a/test/metabase/dashboards_rest/api_test.clj
+++ b/test/metabase/dashboards_rest/api_test.clj
@@ -5218,6 +5218,31 @@
(t2/delete! :model/Dashboard :id dash-id)
(is (not (t2/exists? :model/Card :dashboard_id dash-id))))))
+(deftest ^:parallel dashboard-internal-card-cannot-be-replaced-onto-another-dashboard-test
+ ;; UXW-4731: the add-card guard only checked newly-created dashcards, so "Replace" (which keeps the
+ ;; dashcard id and only swaps card_id -> lands in `to-update`) could smuggle a dashboard-internal card
+ ;; from another dashboard onto this one.
+ (mt/with-temp [:model/Dashboard {source-dash-id :id} {}
+ :model/Card {dq-card-id :id} {:dashboard_id source-dash-id}
+ :model/DashboardCard _ {:card_id dq-card-id :dashboard_id source-dash-id}
+ :model/Dashboard {other-dash-id :id} {}
+ :model/Card {regular-card-id :id} {}
+ :model/DashboardCard {dashcard-id :id} {:card_id regular-card-id :dashboard_id other-dash-id}]
+ (testing "Adding it as a new dashcard is rejected"
+ (mt/user-http-request :crowberto :put 400 (str "dashboard/" other-dash-id)
+ {:dashcards [{:id -1 :size_x 1 :size_y 1 :row 0 :col 0 :card_id dq-card-id}]}))
+ (testing "Replacing an existing dashcard's card with it is also rejected"
+ (mt/user-http-request :crowberto :put 400 (str "dashboard/" other-dash-id)
+ {:dashcards [{:id dashcard-id :size_x 1 :size_y 1 :row 0 :col 0 :card_id dq-card-id}]}))
+ (testing "Adding it as an extra series on an existing dashcard is also rejected"
+ (mt/user-http-request :crowberto :put 400 (str "dashboard/" other-dash-id)
+ {:dashcards [{:id dashcard-id :size_x 1 :size_y 1 :row 0 :col 0
+ :card_id regular-card-id
+ :series [{:id dq-card-id}]}]}))
+ (testing "The foreign dashboard-internal card was not persisted onto the other dashboard"
+ (is (not (t2/exists? :model/DashboardCard :dashboard_id other-dash-id :card_id dq-card-id)))
+ (is (not (t2/exists? :model/DashboardCardSeries :card_id dq-card-id))))))
+
(deftest dashboard-questions-are-archived-with-the-dashboard
(testing "It gets archived with the dashboard"
(mt/with-temp [:model/Dashboard {dash-id :id} {}