Skip to content

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

escape parameter & heading text when rendering notification emails (#80055)

YBd914e47f76ee
diff --git a/src/metabase/channel/impl/email.clj b/src/metabase/channel/impl/email.clj
index 9a50692299c..b658f0bcdac 100644
--- a/src/metabase/channel/impl/email.clj
+++ b/src/metabase/channel/impl/email.clj
@@ -4,7 +4,7 @@
[buddy.core.hash :as buddy-hash]
[clojure.java.io :as io]
[clojure.string :as str]
- [hiccup.core :refer [html]]
+ [hiccup.core :refer [h html]]
[medley.core :as m]
[metabase.analytics-interface.core :as analytics]
[metabase.analytics.core :as analytics.core]
@@ -123,7 +123,8 @@
rendered-params (when (seq inline-params) (render.util/render-parameters inline-params))
heading-text (:text part)
style (style/style (if (seq inline-params) {:margin-bottom "4px"} {}))]
- {:content (str (html [:h2 {:style style} heading-text])
+ ;; hiccup 1 doesn't escape strings and :content reaches `{{{computed.dashboard_content}}}` as-is
+ {:content (str (html [:h2 {:style style} (h heading-text)])
rendered-params)})
:tab-title
{:content (markdown/process-markdown (format "# %s\n---" (:text part)) :html (system/site-url))}))
diff --git a/src/metabase/channel/render/util.clj b/src/metabase/channel/render/util.clj
index 94f7beb8e30..e8b250f24f9 100644
--- a/src/metabase/channel/render/util.clj
+++ b/src/metabase/channel/render/util.clj
@@ -1,7 +1,7 @@
(ns metabase.channel.render.util
(:require
[clojure.string :as str]
- [hiccup.core :refer [html]]
+ [hiccup.core :refer [h html]]
[metabase.channel.render.style :as style]
[metabase.parameters.shared :as shared.params]
[metabase.system.core :as system]
@@ -224,7 +224,10 @@
(defn render-parameters
"Renders parameters as inline left-aligned spans for use inside a dashcard. A parameter that fails to render is
- skipped so it doesn't fail the whole notification."
+ skipped so it doesn't fail the whole notification.
+
+ Names and values come back escaped: `html` here is hiccup 1, which doesn't escape strings, and callers splice
+ the result into the notification body without escaping it."
[parameters]
(html
(let [locale (system/site-locale)]
@@ -241,6 +244,6 @@
{:style (style/style {:margin-bottom "4px"})}
[:span {:style (style/style {:color style/color-text-dark
:padding-right "8px"})}
- name]
+ (h name)]
[:span {:style (style/style {:color style/color-text-medium})}
- value-str]])])))
+ (h value-str)]])])))
diff --git a/test/metabase/channel/render/util_test.clj b/test/metabase/channel/render/util_test.clj
index 7c55b8ee777..82722eb5a16 100644
--- a/test/metabase/channel/render/util_test.clj
+++ b/test/metabase/channel/render/util_test.clj
@@ -1,5 +1,6 @@
(ns metabase.channel.render.util-test
(:require
+ [clojure.string :as str]
[clojure.test :refer :all]
[metabase.channel.render.util :as render-util]))
@@ -170,3 +171,22 @@
(is (= (map #(select-keys % [:name :display_name]) (:cols expected-merged-data))
(map #(select-keys % [:name :display_name]) (:cols result))))
(is (= (:rows expected-merged-data) (:rows result))))))
+
+(deftest ^:parallel render-parameters-escapes-html-test
+ (testing "parameter names and values are HTML-escaped"
+ ;; Callers splice this fragment into the email body as-is, so it has to arrive escaped.
+ (let [name-html "<b>Bold</b>"
+ value-html "R&D <i>x</i>"
+ rendered (render-util/render-parameters
+ [{:id "f0d3d4d3"
+ :type "string/="
+ :name name-html
+ :value [value-html]}])]
+ (testing "no markup from the parameter survives into the rendered HTML"
+ (is (not (str/includes? rendered name-html))
+ "parameter name was not escaped")
+ (is (not (str/includes? rendered value-html))
+ "parameter value was not escaped"))
+ (testing "the text is still shown, escaped"
+ (is (str/includes? rendered "&lt;b&gt;Bold&lt;/b&gt;"))
+ (is (str/includes? rendered "R&amp;D &lt;i&gt;x&lt;/i&gt;"))))))
diff --git a/test/metabase/pulse/dashboard_subscription_test.clj b/test/metabase/pulse/dashboard_subscription_test.clj
index 5dbee49b53b..aa9734008d4 100644
--- a/test/metabase/pulse/dashboard_subscription_test.clj
+++ b/test/metabase/pulse/dashboard_subscription_test.clj
@@ -440,11 +440,12 @@
{:email
(fn [_ [email]]
(testing "Markdown cards are included in email subscriptions"
- (is (= (rasta-dashsub-message {:message [{"Aviary KPIs" true
- "header, quote isn't escaped" true}
+ ;; The apostrophe arrives as `&apos;` now that heading text is escaped on its way into the body.
+ (is (= (rasta-dashsub-message {:message [{"Aviary KPIs" true
+ "header, quote isn&apos;t escaped" true}
pulse.test-util/png-attachment]})
(mt/summarize-multipart-single-email email #"Aviary KPIs"
- #"header, quote isn't escaped")))))
+ #"header, quote isn&apos;t escaped")))))
:slack
(fn [{:keys [card-id dashboard-id]} [pulse-results]]
@@ -542,6 +543,36 @@
#"Quarter and Year"
#"State")))))}}))
+(deftest dashboard-filter-html-escaping-test
+ (let [name-html "<b>Bold</b>"
+ value-html "R&D <i>x</i>"]
+ (tests!
+ {:pulse {:skip_if_empty false}
+ :dashboard (update pulse.test-util/test-dashboard :parameters
+ (fn [params]
+ (for [param params]
+ (cond-> param
+ (= "State" (:name param)) (assoc :name name-html
+ :default [value-html])))))}
+ "Dashboard subscription whose filter name and value contain HTML delivers them escaped"
+ {:card (pulse.test-util/checkins-query-card {})
+
+ :fixture
+ (fn [_ thunk]
+ (thunk))
+
+ :assert
+ {:email
+ (fn [_ [email]]
+ (let [html-body (-> email :message first :content)]
+ (testing "the filter name and value arrive as text, not as markup"
+ (is (not (str/includes? html-body name-html))
+ "filter name was not escaped")
+ (is (not (str/includes? html-body value-html))
+ "filter value was not escaped")
+ (is (str/includes? html-body "&lt;b&gt;Bold&lt;/b&gt;"))
+ (is (str/includes? html-body "R&amp;D &lt;i&gt;x&lt;/i&gt;")))))}})))
+
(deftest filter-render-failure-doesnt-fail-subscription-test
(tests!
{:pulse {:skip_if_empty false}
@@ -621,6 +652,32 @@
:fields [{:type "mrkdwn", :text "*State*\nCA, NY, and NJ"}]}]}
message)))))}}))
+(deftest dashboard-heading-html-escaping-test
+ (let [heading-html "<b>Heading</b>"]
+ (tests!
+ {:pulse {:skip_if_empty false}
+ :dashboard pulse.test-util/test-dashboard}
+ "Dashboard subscription whose heading card contains HTML delivers it escaped"
+ {:card (pulse.test-util/checkins-query-card {})
+
+ :fixture
+ (fn [{dashboard-id :dashboard-id} thunk]
+ (mt/with-temp [:model/DashboardCard _ {:dashboard_id dashboard-id
+ :row 0
+ :col 0
+ :visualization_settings {:virtual_card {:display "heading"}
+ :text heading-html}}]
+ (thunk)))
+
+ :assert
+ {:email
+ (fn [_ [email]]
+ (let [html-body (-> email :message first :content)]
+ (testing "the heading text arrives as text, not as markup"
+ (is (not (str/includes? html-body heading-html))
+ "heading text was not escaped")
+ (is (str/includes? html-body "&lt;b&gt;Heading&lt;/b&gt;")))))}})))
+
(deftest dashboard-with-dashcard-filters-test
(tests!
{:pulse {:skip_if_empty false}