Skip to content

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

Strip template tags from public and embed native queries (#80574)

Alexander Polyankin70504458f672
diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts
index c12de87a009..ae725bc282f 100644
--- a/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts
@@ -71,7 +71,9 @@ export const updateQuestionSdk =
shouldRunQueryOnQuestionChange = false;
}
- nextQuestion = nextQuestion.applyTemplateTagParameters();
+ if (!isGuestEmbed) {
+ nextQuestion = nextQuestion.applyTemplateTagParameters();
+ }
const rawSeries = createRawSeries({
card: nextQuestion.card(),
diff --git a/src/metabase/public_sharing_rest/api.clj b/src/metabase/public_sharing_rest/api.clj
index b59997e87eb..1bde6012e7b 100644
--- a/src/metabase/public_sharing_rest/api.clj
+++ b/src/metabase/public_sharing_rest/api.clj
@@ -1,7 +1,6 @@
(ns metabase.public-sharing-rest.api
"Metabase API endpoints for viewing publicly-accessible Cards and Dashboards."
(:require
- [clojure.string :as str]
[hiccup.core :as hiccup]
[medley.core :as m]
[metabase.actions.core :as actions]
@@ -66,17 +65,12 @@
(defn- blank-dataset-query
"Replace the contents of `query` with a blank query of the same type, so the query contents themselves (SQL,
- filters, aggregations, etc.) are never exposed to the general public. Native queries keep their parameter template
- tags so the frontend can still derive parameters from them. MBQL queries keep only their source table or source
- card, plus a placeholder `:count` aggregation per original aggregation, so the frontend can still tell MBQL and
- native queries apart (e.g. to use the pivot endpoints) and resolve `:aggregation` column refs."
+ filters, aggregations, template tags, etc.) are never exposed to the general public. MBQL queries keep only their
+ source table or source card, plus a placeholder `:count` aggregation per original aggregation, so the frontend can
+ still tell MBQL and native queries apart (e.g. to use the pivot endpoints) and resolve `:aggregation` column refs."
[query]
(if (lib/native? query)
- (let [text (->> (lib/template-tags query)
- (filter queries/parameter-template-tag?)
- (map #(str "{{" (:name %) "}}"))
- (str/join " "))]
- (lib/with-native-query query (if (str/blank? text) "-" text)))
+ (lib/native-query query "-")
(if-let [source (or (some->> (lib/primary-source-table-id query) (lib.metadata/table query))
(some->> (lib/primary-source-card-id query) (lib.metadata/card query)))]
(reduce lib/aggregate
diff --git a/test/metabase/embedding_rest/api/embed_test.clj b/test/metabase/embedding_rest/api/embed_test.clj
index 664991aa2b5..b2da7e6b0f7 100644
--- a/test/metabase/embedding_rest/api/embed_test.clj
+++ b/test/metabase/embedding_rest/api/embed_test.clj
@@ -181,9 +181,8 @@
(let [{:keys [dataset_query]} (client/client :get 200 (card-url card))]
(is (= {:lib/type "mbql/query"
:database (mt/id)
- :stages [{:lib/type "mbql.stage/native"
- :native "-"
- :template-tags []}]}
+ :stages [{:lib/type "mbql.stage/native"
+ :native "-"}]}
dataset_query)))))))
(deftest we-should-fail-when-attempting-to-use-an-expired-token
diff --git a/test/metabase/public_sharing_rest/api_test.clj b/test/metabase/public_sharing_rest/api_test.clj
index 99d5a542f4d..e4f34682619 100644
--- a/test/metabase/public_sharing_rest/api_test.clj
+++ b/test/metabase/public_sharing_rest/api_test.clj
@@ -156,8 +156,8 @@
:aggregation [["count" {:lib/uuid string?}]]}]}
dataset_query)))))))
-(deftest fetch-card-strips-native-query-keeps-parameter-tags-test
- (testing "GET /api/public/card/:uuid strips the native query text but keeps parameter template tags"
+(deftest fetch-card-strips-native-query-test
+ (testing "GET /api/public/card/:uuid strips the native query text and its template tags"
(mt/with-temporary-setting-values [enable-public-sharing true]
(mt/with-temp [:model/NativeQuerySnippet snippet {:name "greeting" :content "'hello'"}]
(with-temp-public-card
@@ -179,14 +179,11 @@
:display-name "Snippet: Greeting"
:snippet-id (:id snippet)}}))})]
(let [{:keys [dataset_query]} (client/client :get 200 (str "public/card/" uuid))]
- (is (=? {:lib/type "mbql/query"
- :database (mt/id)
- :stages [{:lib/type "mbql.stage/native"
- :native "{{price}}"
- :template-tags [{:name "price"
- :type "dimension"
- :widget-type "category"}]}]}
- dataset_query))))))))
+ (is (= {:lib/type "mbql/query"
+ :database (mt/id)
+ :stages [{:lib/type "mbql.stage/native"
+ :native "-"}]}
+ dataset_query))))))))
(deftest fetch-dashboard-strips-dataset-query-test
(testing "GET /api/public/dashboard/:uuid replaces each Card's query with a blank query so its contents are not exposed"