From 5acb0ad227503228c7951c3b690b8d14dba2e22c Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 26 Jun 2019 09:13:09 -0300 Subject: [PATCH] MM-16369: include potential subpath in emails (#11311) * MM-16369: include potential subpath in emails Various email templates were extracting the `Host` but not the `Path` from the SiteURL, excluding a potential subpath defined therein. * improve coverage of condenseSiteURL --- app/email.go | 26 ++++++++++++++++++-------- app/email_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 app/email_test.go diff --git a/app/email.go b/app/email.go index 6d6f8d9261..fb287ef33f 100644 --- a/app/email.go +++ b/app/email.go @@ -6,6 +6,7 @@ package app import ( "fmt" "net/url" + "path" "net/http" @@ -26,6 +27,15 @@ const ( emailRateLimitingMaxBurst = 20 ) +func condenseSiteURL(siteURL string) string { + parsedSiteURL, _ := url.Parse(siteURL) + if parsedSiteURL.Path == "" || parsedSiteURL.Path == "/" { + return parsedSiteURL.Host + } + + return path.Join(parsedSiteURL.Host, parsedSiteURL.Path) +} + func (a *App) SetupInviteEmailRateLimiting() error { store, err := memstore.New(emailRateLimitingMemstoreSize) if err != nil { @@ -117,14 +127,14 @@ func (a *App) SendVerifyEmail(userEmail, locale, siteURL, token string) *model.A link := fmt.Sprintf("%s/do_verify_email?token=%s&email=%s", siteURL, token, url.QueryEscape(userEmail)) - url, _ := url.Parse(siteURL) + serverURL := condenseSiteURL(siteURL) subject := T("api.templates.verify_subject", map[string]interface{}{"SiteName": a.ClientConfig()["SiteName"]}) bodyPage := a.NewEmailTemplate("verify_body", locale) bodyPage.Props["SiteURL"] = siteURL - bodyPage.Props["Title"] = T("api.templates.verify_body.title", map[string]interface{}{"ServerURL": url.Host}) + bodyPage.Props["Title"] = T("api.templates.verify_body.title", map[string]interface{}{"ServerURL": serverURL}) bodyPage.Props["Info"] = T("api.templates.verify_body.info") bodyPage.Props["VerifyUrl"] = link bodyPage.Props["Button"] = T("api.templates.verify_body.button") @@ -159,15 +169,15 @@ func (a *App) SendSignInChangeEmail(email, method, locale, siteURL string) *mode func (a *App) SendWelcomeEmail(userId string, email string, verified bool, locale, siteURL string) *model.AppError { T := utils.GetUserTranslations(locale) - rawUrl, _ := url.Parse(siteURL) + serverURL := condenseSiteURL(siteURL) subject := T("api.templates.welcome_subject", map[string]interface{}{"SiteName": a.ClientConfig()["SiteName"], - "ServerURL": rawUrl.Host}) + "ServerURL": serverURL}) bodyPage := a.NewEmailTemplate("welcome_body", locale) bodyPage.Props["SiteURL"] = siteURL - bodyPage.Props["Title"] = T("api.templates.welcome_body.title", map[string]interface{}{"ServerURL": rawUrl.Host}) + bodyPage.Props["Title"] = T("api.templates.welcome_body.title", map[string]interface{}{"ServerURL": serverURL}) bodyPage.Props["Info"] = T("api.templates.welcome_body.info") bodyPage.Props["Button"] = T("api.templates.welcome_body.button") bodyPage.Props["Info2"] = T("api.templates.welcome_body.info2") @@ -382,15 +392,15 @@ func (a *App) NewEmailTemplate(name, locale string) *utils.HTMLTemplate { func (a *App) SendDeactivateAccountEmail(email string, locale, siteURL string) *model.AppError { T := utils.GetUserTranslations(locale) - rawUrl, _ := url.Parse(siteURL) + serverURL := condenseSiteURL(siteURL) subject := T("api.templates.deactivate_subject", map[string]interface{}{"SiteName": a.ClientConfig()["SiteName"], - "ServerURL": rawUrl.Host}) + "ServerURL": serverURL}) bodyPage := a.NewEmailTemplate("deactivate_body", locale) bodyPage.Props["SiteURL"] = siteURL - bodyPage.Props["Title"] = T("api.templates.deactivate_body.title", map[string]interface{}{"ServerURL": rawUrl.Host}) + bodyPage.Props["Title"] = T("api.templates.deactivate_body.title", map[string]interface{}{"ServerURL": serverURL}) bodyPage.Props["Info"] = T("api.templates.deactivate_body.info", map[string]interface{}{"SiteURL": siteURL}) bodyPage.Props["Warning"] = T("api.templates.deactivate_body.warning") diff --git a/app/email_test.go b/app/email_test.go new file mode 100644 index 0000000000..206d370e95 --- /dev/null +++ b/app/email_test.go @@ -0,0 +1,28 @@ +package app + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCondenseSiteURL(t *testing.T) { + require.Equal(t, "", condenseSiteURL("")) + require.Equal(t, "mattermost.com", condenseSiteURL("mattermost.com")) + require.Equal(t, "mattermost.com", condenseSiteURL("mattermost.com/")) + require.Equal(t, "chat.mattermost.com", condenseSiteURL("chat.mattermost.com")) + require.Equal(t, "chat.mattermost.com", condenseSiteURL("chat.mattermost.com/")) + require.Equal(t, "mattermost.com/subpath", condenseSiteURL("mattermost.com/subpath")) + require.Equal(t, "mattermost.com/subpath", condenseSiteURL("mattermost.com/subpath/")) + require.Equal(t, "chat.mattermost.com/subpath", condenseSiteURL("chat.mattermost.com/subpath")) + require.Equal(t, "chat.mattermost.com/subpath", condenseSiteURL("chat.mattermost.com/subpath/")) + + require.Equal(t, "mattermost.com:8080", condenseSiteURL("http://mattermost.com:8080")) + require.Equal(t, "mattermost.com:8080", condenseSiteURL("http://mattermost.com:8080/")) + require.Equal(t, "chat.mattermost.com:8080", condenseSiteURL("http://chat.mattermost.com:8080")) + require.Equal(t, "chat.mattermost.com:8080", condenseSiteURL("http://chat.mattermost.com:8080/")) + require.Equal(t, "mattermost.com:8080/subpath", condenseSiteURL("http://mattermost.com:8080/subpath")) + require.Equal(t, "mattermost.com:8080/subpath", condenseSiteURL("http://mattermost.com:8080/subpath/")) + require.Equal(t, "chat.mattermost.com:8080/subpath", condenseSiteURL("http://chat.mattermost.com:8080/subpath")) + require.Equal(t, "chat.mattermost.com:8080/subpath", condenseSiteURL("http://chat.mattermost.com:8080/subpath/")) +}