Skip to content

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

Fix image tile url tracking (#81371)

Nathan Voxland2921ecdcf9f9
diff --git a/src/metabase/embedding_rest/api/common.clj b/src/metabase/embedding_rest/api/common.clj
index 315abd3a24b..3d111b7bff9 100644
--- a/src/metabase/embedding_rest/api/common.clj
+++ b/src/metabase/embedding_rest/api/common.clj
@@ -478,8 +478,8 @@
the embed tiles endpoints. Callers select each entity exactly once and thread it here. Returns a Ring response."
[dashboard dashcard card parameters zoom x y lat-field lon-field]
(database-routing/with-database-routing-off
- (api.tiles/process-tiles-query-for-dashcard dashboard dashcard card
- parameters zoom x y lat-field lon-field)))
+ (api.public/process-tiles-query-for-dashcard dashboard dashcard card
+ parameters zoom x y lat-field lon-field)))
(defn card-param-values
"Search for card parameter values. Does security checks to ensure the parameter is on the card and then gets param
diff --git a/src/metabase/embedding_rest/api/embed.clj b/src/metabase/embedding_rest/api/embed.clj
index 49fc75e1967..e50cc2c82f8 100644
--- a/src/metabase/embedding_rest/api/embed.clj
+++ b/src/metabase/embedding_rest/api/embed.clj
@@ -441,9 +441,8 @@
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
card (api/check-404 (t2/select-one :model/Card card-id))]
(api.embed.common/check-embedding-enabled-for-dashboard dashboard)
- (request/as-admin
- (api.embed.common/process-tiles-query-for-dashcard
- dashboard dashcard card
- (api.embed.common/tile-parameters-for-dashboard
- dashboard (embedding.jwt/get-in-unsigned-token-or-throw unsigned [:params]) parameters)
- zoom x y latField lonField))))
+ (api.embed.common/process-tiles-query-for-dashcard
+ dashboard dashcard card
+ (api.embed.common/tile-parameters-for-dashboard
+ dashboard (embedding.jwt/get-in-unsigned-token-or-throw unsigned [:params]) parameters)
+ zoom x y latField lonField)))
diff --git a/src/metabase/embedding_rest/api/preview_embed.clj b/src/metabase/embedding_rest/api/preview_embed.clj
index 602daad8c20..16341e10866 100644
--- a/src/metabase/embedding_rest/api/preview_embed.clj
+++ b/src/metabase/embedding_rest/api/preview_embed.clj
@@ -284,9 +284,8 @@
dashboard (api/check-404 (t2/select-one :model/Dashboard dashboard-id))
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
card (api/check-404 (t2/select-one :model/Card card-id))]
- (request/as-admin
- (api.embed.common/process-tiles-query-for-dashcard
- dashboard dashcard card
- (api.embed.common/tile-parameters-for-dashboard
- dashboard (embed/get-in-unsigned-token-or-throw unsigned-token [:params]) parameters)
- zoom x y latField lonField))))
+ (api.embed.common/process-tiles-query-for-dashcard
+ dashboard dashcard card
+ (api.embed.common/tile-parameters-for-dashboard
+ dashboard (embed/get-in-unsigned-token-or-throw unsigned-token [:params]) parameters)
+ zoom x y latField lonField)))
diff --git a/src/metabase/public_sharing_rest/api.clj b/src/metabase/public_sharing_rest/api.clj
index 7cd5f7f2d75..c2a0918c72d 100644
--- a/src/metabase/public_sharing_rest/api.clj
+++ b/src/metabase/public_sharing_rest/api.clj
@@ -363,6 +363,18 @@
(binding [api/*current-user-id* nil]
(m/mapply qp.dashboard/process-query-for-dashcard options)))))
+(defn process-tiles-query-for-dashcard
+ "Like [[process-query-for-dashcard]], but renders a map tile for pre-loaded Card `card` on pre-loaded DashboardCard
+ `dashcard` belonging to pre-loaded Dashboard `dashboard`. Runs with the same superuser perms and anonymized current
+ user as [[process-query-for-dashcard]].
+
+ Callers are responsible for 404-checking each entity before threading it here. Returns a Ring response."
+ [dashboard dashcard card parameters zoom x y lat-field lon-field]
+ (request/as-admin
+ (binding [api/*current-user-id* nil]
+ (api.tiles/process-tiles-query-for-dashcard dashboard dashcard card
+ parameters zoom x y lat-field lon-field))))
+
;; 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
;;
@@ -771,10 +783,9 @@
(public-sharing.validation/check-public-sharing-enabled)
(let [dashboard (api/check-404 (public-sharing/public-uuid->model :model/Dashboard uuid))
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
- card (api/check-404 (t2/select-one :model/Card card-id))]
- (request/as-admin
- (api.tiles/process-tiles-query-for-dashcard dashboard dashcard card
- parameters zoom x y latField lonField))))
+ card (api/check-404 (t2/select-one :model/Card :id card-id :archived false))]
+ (process-tiles-query-for-dashcard dashboard dashcard card
+ parameters zoom x y latField lonField)))
;;; ------------------------------------------------ Public Documents -------------------------------------------------
diff --git a/test/metabase/embedding_rest/api/embed_test.clj b/test/metabase/embedding_rest/api/embed_test.clj
index 2ba2d2787cf..7288c68adaa 100644
--- a/test/metabase/embedding_rest/api/embed_test.clj
+++ b/test/metabase/embedding_rest/api/embed_test.clj
@@ -10,7 +10,6 @@
[clojure.test :refer :all]
[dk.ative.docjure.spreadsheet :as spreadsheet]
[java-time.api :as t]
- [metabase.api.common :as api]
[metabase.dashboards-rest.api-test :as api.dashboard-test]
[metabase.embedding-rest.api.common :as api.embed.common]
[metabase.lib.core :as lib]
@@ -28,6 +27,7 @@
[metabase.test.http-client :as client]
[metabase.tiles.api-test :as tiles.api-test]
[metabase.util :as u]
+ [metabase.util.json :as json]
[metabase.util.random :as u.random]
[toucan2.core :as t2])
(:import
@@ -953,8 +953,7 @@
:data {:rows [[1]]}}
(mt/user-http-request :rasta :get 202 (dashcard-url dashcard {:params {:venue_id 100}}))))
(is (= {}
- (binding [api/*current-user-id* (mt/user->id :rasta)]
- (:last_used_param_values (t2/hydrate (t2/select-one :model/Dashboard (:dashboard_id dashcard)) :last_used_param_values)))))))))
+ (public-test/last-used-param-values :rasta (:dashboard_id dashcard))))))))
(deftest downloading-csv-json-xlsx-results-from-the-dashcard-endpoint-shouldn-t-be-subject-to-the-default-query-constraints
(testing (str "Downloading CSV/JSON/XLSX results from the dashcard endpoint shouldn't be subject to the default "
@@ -2196,6 +2195,31 @@
:latField (tiles.api-test/encoded-lat-field-ref)
:lonField (tiles.api-test/encoded-lon-field-ref)))))))))
+(deftest dashcard-tile-query-does-not-save-last-used-parameters-test
+ (testing "GET api/embed/tiles/dashboard/:token/dashcard/:dashcard-id/card/:card-id/:zoom/:x/:y"
+ (testing "must not persist the URL's parameters as a signed-in visitor's last used parameter values"
+ (mt/with-temporary-setting-values [dashboards-save-last-used-parameters true]
+ (with-embedding-enabled-and-new-secret-key!
+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:enable_embedding true
+ :embedding_params {:state "enabled"}
+ :parameters [{:id "_STATE_", :name "State"
+ :slug "state", :type "string/="}]}
+ :model/Card {card-id :id} {:dataset_query (venues-query)}
+ :model/DashboardCard {dashcard-id :id} {:card_id card-id
+ :dashboard_id dashboard-id
+ :parameter_mappings [{:parameter_id "_STATE_"
+ :card_id card-id
+ :target [:dimension [:field (mt/id :people :state) nil]]}]}]
+ (let [token (dash-token dashboard-id)]
+ (is (png? (mt/user-http-request
+ :rasta :get 200 (format "embed/tiles/dashboard/%s/dashcard/%d/card/%d/1/1/1"
+ token dashcard-id card-id)
+ :latField (tiles.api-test/encoded-lat-field-ref)
+ :lonField (tiles.api-test/encoded-lon-field-ref)
+ :parameters (json/encode [{:id "_STATE_", :value ["CA"]}]))))
+ (is (= {}
+ (public-test/last-used-param-values :rasta dashboard-id))))))))))
+
(deftest card-tile-query-implicit-join-ref-test
(testing "GET api/embed/tiles/card/:uuid/:zoom/:x/:y returns a 400 when the lat/lon refs use an implicit join"
(with-embedding-enabled-and-new-secret-key!
diff --git a/test/metabase/public_sharing_rest/api_test.clj b/test/metabase/public_sharing_rest/api_test.clj
index e357651dcd9..260298f7ddf 100644
--- a/test/metabase/public_sharing_rest/api_test.clj
+++ b/test/metabase/public_sharing_rest/api_test.clj
@@ -9,6 +9,7 @@
[dk.ative.docjure.spreadsheet :as spreadsheet]
[metabase.analytics.snowplow-test :as snowplow-test]
[metabase.analytics.stats :as stats]
+ [metabase.api.common :as api]
[metabase.app-db.encryption-test-util :as encryption-tu]
[metabase.dashboards-rest.api-test :as api.dashboard-test]
[metabase.lib.core :as lib]
@@ -2195,6 +2196,61 @@
:latField lat-field
:lonField lon-field)))))))))
+(defn last-used-param-values
+ "The `{parameter-id value}` map that `user-kw` would see as `dashboard-id`'s last used parameter values.
+
+ `:last_used_param_values` hydrates off `api/*current-user-id*` rather than off an argument, so reading another
+ user's values means binding it. Also used by [[metabase.embedding-rest.api.embed-test]]."
+ [user-kw dashboard-id]
+ (binding [api/*current-user-id* (mt/user->id user-kw)]
+ (:last_used_param_values
+ (t2/hydrate (t2/select-one :model/Dashboard dashboard-id) :last_used_param_values))))
+
+(deftest dashcard-tile-query-does-not-save-last-used-parameters-test
+ (testing "GET api/public/tiles/dashboard/:uuid/dashcard/:dashcard-id/card/:card-id/:zoom/:x/:y"
+ (testing "must not persist the URL's parameters as a signed-in visitor's last used parameter values"
+ (let [uuid (str (random-uuid))]
+ (mt/with-temporary-setting-values [enable-public-sharing true
+ dashboards-save-last-used-parameters true]
+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:public_uuid uuid
+ :parameters [{:id "_STATE_", :name "State"
+ :slug "state", :type "string/="}]}
+ :model/Card {card-id :id} {:dataset_query (venues-query)}
+ :model/DashboardCard {dashcard-id :id} {:card_id card-id
+ :dashboard_id dashboard-id
+ :parameter_mappings [{:parameter_id "_STATE_"
+ :card_id card-id
+ :target [:dimension [:field (mt/id :people :state) nil]]}]}]
+ (let [url (str "public/tiles/dashboard/" uuid "/dashcard/" dashcard-id "/card/" card-id "/1/1/1")]
+ (is (png? (mt/user-http-request :rasta :get 200 url
+ :latField (tiles.api-test/encoded-lat-field-ref)
+ :lonField (tiles.api-test/encoded-lon-field-ref)
+ :parameters (json/encode [{:id "_STATE_", :value ["CA"]}]))))
+ (is (= {}
+ (last-used-param-values :rasta dashboard-id))))))))))
+
+(deftest dashcard-tile-query-archived-card-test
+ (testing "GET api/public/tiles/dashboard/:uuid/dashcard/:dashcard-id/card/:card-id/:zoom/:x/:y"
+ (testing "must not render an archived Card, matching its non-tiles siblings (SEC-1143)"
+ (let [uuid (str (random-uuid))]
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:public_uuid uuid}
+ :model/Card {card-id :id} {:dataset_query (venues-query)}
+ :model/DashboardCard {dashcard-id :id} {:card_id card-id
+ :dashboard_id dashboard-id}]
+ ;; assert on the status alone: the success body is a PNG, and letting it reach the test report
+ ;; renders the whole run's output binary.
+ (let [url (str "public/tiles/dashboard/" uuid "/dashcard/" dashcard-id "/card/" card-id "/1/1/1")
+ tile-status #(:status (client/client-full-response
+ :get url
+ :latField (tiles.api-test/encoded-lat-field-ref)
+ :lonField (tiles.api-test/encoded-lon-field-ref)))]
+ (testing "sanity: the tile renders while the Card is live"
+ (is (= 200 (tile-status))))
+ (t2/update! :model/Card card-id {:archived true})
+ (testing "and 404s once it is archived"
+ (is (= 404 (tile-status)))))))))))
+
(deftest card-tile-query-implicit-join-ref-test
(testing "GET api/public/tiles/card/:uuid/:zoom/:x/:y returns a 400 when the lat/lon refs use an implicit join"
(let [uuid (str (random-uuid))]