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
Encode mailto links in emails (#84388)
Chris Truterb54b163cf457
diff --git a/dev/email-preview/index.html b/dev/email-preview/index.htmlindex 5ed888e955a..3ad9c758e33 100644--- a/dev/email-preview/index.html+++ b/dev/email-preview/index.html@@ -12,9 +12,9 @@Fidelity notes: production renders these templates with Java Handlebars(jknack) — this tool uses handlebars.js, which is equivalent for everythingthese templates use ({{#if}}, {{#each}}, {{#with}}, partials). The custom- helpers (equals, format-date, now, initials, card-url, dashboard-url,- trash-url) are re-implemented below; format-date approximates java-time- formatting with a common-token subset.+ helpers (equals, format-date, now, initials, mailto-url, card-url,+ dashboard-url, trash-url) are re-implemented below; format-date approximates+ java-time formatting with a common-token subset.--><html lang="en"><head>@@ -271,6 +271,8 @@ Handlebars.registerHelper('initials', (name) => {const m = String(name || '').match(/\b\w/g);return m ? m.slice(0, 2).join('').toUpperCase() : '';});+Handlebars.registerHelper('mailto-url', (email) =>+ `mailto:${String(email ?? '').replace(/[%?#]/g, encodeURIComponent)}`);Handlebars.registerHelper('card-url', (id) => `${SITE_URL}/question/${id}`);Handlebars.registerHelper('dashboard-url', (id) => `${SITE_URL}/dashboard/${id}`);Handlebars.registerHelper('trash-url', () => `${SITE_URL}/trash`);diff --git a/src/metabase/channel/email/password_reset.hbs b/src/metabase/channel/email/password_reset.hbsindex 53a8b422ea2..3aa880ae846 100644--- a/src/metabase/channel/email/password_reset.hbs+++ b/src/metabase/channel/email/password_reset.hbs@@ -19,7 +19,7 @@<p>Someone requested a password reset for your {{applicationName}} account at {{siteUrl}}, but your accounthas been deactivated. Contact an administrator for further assistance.</p>{{#if adminEmailSet}}- <a href="mailto:{{adminEmail}}">Contact your administrator</a>+ <a href="{{mailto-url adminEmail}}">Contact your administrator</a>{{/if}}{{/if}}{{/if}}diff --git a/src/metabase/channel/email/user_joined_notification.hbs b/src/metabase/channel/email/user_joined_notification.hbsindex e41a144e152..da6718be2cd 100644--- a/src/metabase/channel/email/user_joined_notification.hbs+++ b/src/metabase/channel/email/user_joined_notification.hbs@@ -17,7 +17,7 @@</h1><p style="text-align: center; font-size: 15px; line-height: 165%; color: #2F3C45; margin: 0;">- <a href="mailto:{{{joinedUserEmail}}}" style="color: {{context.application_color}}; text-decoration: none;">{{joinedUserEmail}}</a>+ <a href="{{mailto-url joinedUserEmail}}" style="color: {{context.application_color}}; text-decoration: none;">{{joinedUserEmail}}</a>joined {{#if joinedViaSSO}}via Single Sign-On {{/if}}on {{joinedDate}}.</p>diff --git a/src/metabase/channel/template/handlebars_helper.clj b/src/metabase/channel/template/handlebars_helper.cljindex 5ca0b413432..f98f792c2b1 100644--- a/src/metabase/channel/template/handlebars_helper.clj+++ b/src/metabase/channel/template/handlebars_helper.clj@@ -103,6 +103,16 @@#_{:clj-kondo/ignore [:discouraged-var]}(some->> name (re-seq #"\b(\w)") (take 2) (map second) (str/join "") str/upper-case))+(defhelper mailto-url+ "Return a `mailto:` URL for an email address.+ Percent-encodes only `%`, `?` and `#`, which a mail client would otherwise read as an escape, the start of the+ headers, or a fragment. Every other address comes out unchanged.++ {{mailto-url \"[email protected]\"}}+ ;; => \"mailto:x%[email protected]\""+ [email _params _kparams _options]+ (str "mailto:" (some-> email (str/escape {\% "%25" \? "%3F" \# "%23"}))))+;; Metabase specifics(defhelper card-url@@ -131,5 +141,6 @@"format-date" format-date"now" now"initials" initials+ "mailto-url" mailto-url"card-url" card-url"dashboard-url" dashboard-url})diff --git a/test/metabase/channel/email/messages_test.clj b/test/metabase/channel/email/messages_test.cljindex 77430e0ea10..81b980427b7 100644--- a/test/metabase/channel/email/messages_test.clj+++ b/test/metabase/channel/email/messages_test.clj@@ -55,7 +55,15 @@(let [content (-> (@et/inbox "[email protected]")(get-in [0 :body 0 :content]))](is (str/includes? content "href=\"mailto:[email protected]\""))- (is (not (str/includes? content "mailto:[email protected]"))))))))+ (is (not (str/includes? content "mailto:[email protected]")))))))+ (testing "a `?` in the joined user's email is percent-encoded, so the link does not add a CC"+ (mt/with-temporary-setting-values [admin-email "[email protected]"]+ (et/with-fake-inbox+ (messages/send-user-joined-admin-notification-email! {:first_name "New"+ :common_name "New User"+ :email "[email protected]"})+ (is (str/includes? (get-in (@et/inbox "[email protected]") [0 :body 0 :content])+ "href=\"mailto:x%3Fcc=[email protected]\""))))))#_(deftest render-pulse-email-test(testing "Email with few rows and columns can be rendered when tracing (#21166)"diff --git a/test/metabase/channel/template/handlebars_helper_test.clj b/test/metabase/channel/template/handlebars_helper_test.cljindex b12ef988798..779ac48c07a 100644--- a/test/metabase/channel/template/handlebars_helper_test.clj+++ b/test/metabase/channel/template/handlebars_helper_test.clj@@ -52,6 +52,13 @@(testing "argument can be a datetime object"(is (= "30-01-00" (format-date (t/instant "2000-01-30T00:00:00Z")))))))+(deftest mailto-url-test+ (let [mailto-url #(handlebars/render-string "{{{mailto-url email}}}" {:email %})]+ (testing "ordinary addresses are unchanged"+ (is (= "mailto:[email protected]" (mailto-url "[email protected]"))))+ (testing "only `%`, `?` and `#` are percent-encoded"+ (is (= "mailto:a%25b%3Fcc=c%23d&[email protected]" (mailto-url "a%b?cc=c#d&[email protected]"))))))+(deftest now-test(mt/with-clock #t "2019-12-10T00:00-08:00[US/Pacific]"(is (= "2019-12-10T08:00:00Z" (handlebars/render-string "{{now}}" {})))))