package dispatch
import (
"html"
"mime"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/notifications/render"
"github.com/coder/coder/v2/coderd/notifications/types"
markdown "github.com/coder/coder/v2/coderd/render"
)
// Benign values, so a test measures only what its own payload injected.
func templateHelpers() map[string]any {
return map[string]any{
"base_url": func() string { return "https://coder.example.com" },
"current_year": func() string { return "2026" },
"logo_url": func() string { return "https://coder.example.com/logo.png" },
"app_name": func() string { return "Coder" },
}
}
func TestSMTPHTMLTemplateEscapesUntrustedValues(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
title string
userName string
actions []types.TemplateAction
injected string
}{
{
name: "EntityEncodedAnchorInSubject",
title: `Template "<a href="https://attacker.example/login">Re-authenticate now</a>" deleted`,
userName: "Bobby",
injected: `Re-authenticate now`,
},
{
name: "EntityEncodedImageInSubject",
title: `Workspace "<img src=x onerror="alert(1)">" marked dormant`,
userName: "Bobby",
injected: ``,
},
{
name: "RawHTMLInUserName",
title: "Account suspended",
userName: `Bobby
`,
injected: `
`,
},
{
name: "RawHTMLInActionLabel",
title: "Account suspended",
userName: "Bobby",
actions: []types.TemplateAction{
{Label: `
`, URL: "https://coder.example.com/"},
},
injected: `
`,
},
{
name: "RawHTMLInActionURL",
title: "Account suspended",
userName: "Bobby",
actions: []types.TemplateAction{
{Label: "Open Coder", URL: `https://coder.example.com/?x=`},
},
injected: ``,
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// Decodes the entities, so the title arrives as live markup.
subject, err := markdown.PlaintextFromMarkdown(tc.title)
require.NoError(t, err)
// Actions are set as the template sees them. The enqueuer renders
// them into JSON first, which rejects a `"` of its own accord.
payload := types.MessagePayload{
NotificationTemplateID: "00000000-0000-0000-0000-000000000000",
UserName: tc.userName,
Actions: tc.actions,
Labels: map[string]string{
"_subject": subject,
"_body": "
Test body
", }, } got, err := render.GoTemplate(htmlTemplate, payload, templateHelpers()) require.NoError(t, err) escaped := html.EscapeString(tc.injected) require.NotEqual(t, tc.injected, escaped, "case carries no HTML to escape, so it guards nothing") require.NotContains(t, got, tc.injected, "untrusted markup reached the rendered email: %s", got) require.Contains(t, got, escaped, "the value must still be displayed, entity encoded: %s", got) }) } } func TestSMTPHTMLTemplateEscapesAppearanceHelpers(t *testing.T) { t.Parallel() const ( appName = `Coder">` logoURL = `https://example.com/logo.png">Test body
", }, } helpers := templateHelpers() helpers["logo_url"] = func() string { return logoURL } helpers["app_name"] = func() string { return appName } got, err := render.GoTemplate(htmlTemplate, payload, helpers) require.NoError(t, err) require.True(t, strings.Contains(got, html.EscapeString(appName)), "application name must be HTML escaped") require.True(t, strings.Contains(got, html.EscapeString(logoURL)), "logo URL must be HTML escaped") require.False(t, strings.Contains(got, appName), "raw application name must not be rendered") require.False(t, strings.Contains(got, logoURL), "raw logo URL must not be rendered") } // The template escapes every value it interpolates except _body, which is // trusted rendered Markdown. The three values here cannot carry markup in // production, so this test is the only thing that fails if their escaping is // removed. func TestSMTPHTMLTemplateEscapesTrustedValues(t *testing.T) { t.Parallel() const injected = `a"onclick=alert(1)` for _, tc := range []struct { name string apply func(*types.MessagePayload, map[string]any) }{ { // net/url preserves a quote in the query and --access-url is // validated for its scheme only, so an operator can land this. name: "BaseURL", apply: func(_ *types.MessagePayload, h map[string]any) { h["base_url"] = func() string { return "https://coder.example.com/?q=" + injected } }, }, { name: "CurrentYear", apply: func(_ *types.MessagePayload, h map[string]any) { h["current_year"] = func() string { return injected } }, }, { name: "NotificationTemplateID", apply: func(p *types.MessagePayload, _ map[string]any) { p.NotificationTemplateID = injected }, }, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() payload := types.MessagePayload{ NotificationTemplateID: "00000000-0000-0000-0000-000000000000", UserName: "Test User", Labels: map[string]string{ "_subject": "Test notification", "_body": "Test body
", }, } helpers := templateHelpers() tc.apply(&payload, helpers) got, err := render.GoTemplate(htmlTemplate, payload, helpers) require.NoError(t, err) require.NotContains(t, got, injected, "raw value reached the rendered email: %s", got) require.Contains(t, got, html.EscapeString(injected), "the value must still be displayed, entity encoded: %s", got) }) } } func TestValidateFromAddr(t *testing.T) { t.Parallel() tests := []struct { name string input string expectedEnvelope string expectedHeader string expectedErrContain string }{ { name: "bare email address", input: "system@coder.com", expectedEnvelope: "system@coder.com", expectedHeader: "system@coder.com", }, { name: "email with display name", input: "Coder System