package postcard
import (
"context"
"errors"
"strings"
"testing"
)
// schemeFiller surrounds a case's markup, so a snippet that points at the
// offending markup is told apart from one that starts the document.
var schemeFiller = strings.Repeat("plain words here ", 6)
// TestURLSchemeCheck covers the anchored, decoded URL-scheme check of
// rendered HTML (WR-05): prose and URLs that merely contain a scheme word
// pass; every URL-bearing attribute, CSS, meta refresh and every
// character-reference or whitespace-split spelling of a script or data URL
// is refused, on a send with inlining on and off and in the editor.
func TestURLSchemeCheck(t *testing.T) {
t.Parallel()
accepts := []struct{ name, html string }{
{"prose_metadata", `
") {
t.Errorf("snippet = %q starts the document, not the offending markup", ue.snippet)
}
})
}
// An entity-encoded script link in a layout override's HTML makes the
// send fall back to the file version, whether or not the layout opts
// out of inlining; the encoded link never reaches the driver.
const encodedLink = `
x`
file := fileWelcome(t)
if strings.Contains(file.HTML, "javascript") {
t.Fatalf("the file version holds a script link:\n%s", file.HTML)
}
plugin := acmeBlog{}
cat := NewCatalog()
if err := cat.Register(plugin.ID(), plugin.MailTemplatesFS(), plugin.MailTemplates(), plugin.MailLayouts()); err != nil {
t.Fatal(err)
}
for _, optOut := range []bool{false, true} {
name := "send/inline"
if optOut {
name = "send/opt_out"
}
t.Run(name, func(t *testing.T) {
t.Parallel()
assertFileFallback(t, cat, file, Options{Overrides: fakeOverrides{ov: Overrides{
Template: &TemplateOverride{Subject: "Hello {{ .name }}", Body: "Hi **{{ .name }}**.", Layout: "encoded"},
Layouts: map[string]LayoutOverride{"encoded": {
HTML: `
{{ .Content }}` + encodedLink + `
`,
Text: "{{ .Content }}",
CSS: "p { color: #123456; }",
DisableAutoInlineCSS: optOut,
}},
}}}, true)
})
}
// The editor refuses the same layout draft on content_html, and a
// layout's CSS with a script URL on content_css; prose in a template
// body validates.
for _, optOut := range []bool{false, true} {
name := "editor/inline"
if optOut {
name = "editor/opt_out"
}
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ed, _ := newEditor(t, nil)
layout := Draft{Kind: KindLayout, Name: defaultLayoutName, HTML: `
{{ .Content }}` + encodedLink + `
`, Text: "{{ .Content }}", DisableAutoInlineCSS: optOut}
de := ed.Validate(ctx, layout)
if de == nil || de.Field != fieldContentHTML || de.Rule != ruleUnsafe {
t.Fatalf("encoded link in a layout draft: %+v, want content_html/unsafe", de)
}
if !strings.Contains(de.Message, "javascript:alert(1)") {
t.Errorf("message = %q, want the encoded href", de.Message)
}
def, ok := NewCatalog().Layout(defaultLayoutName)
if !ok {
t.Fatal("no built-in default layout")
}
css := Draft{Kind: KindLayout, Name: defaultLayoutName, HTML: def.HTML, Text: def.Text, CSS: "td { background: url(javascript:x); }", DisableAutoInlineCSS: optOut}
de = ed.Validate(ctx, css)
if de == nil || de.Field != fieldContentCSS || de.Rule != ruleUnsafe {
t.Fatalf("script URL in a layout's CSS: %+v, want content_css/unsafe", de)
}
tmpl := Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "Hi {{ .Name }}", Body: "See the metadata: field.", Layout: defaultLayoutName}
if de := ed.Validate(ctx, tmpl); de != nil {
t.Errorf("a prose body was refused: %+v", de)
}
})
}
}
// TestInlineUnsafeOutput covers an inlined result that fails the safety
// check (WR-05): the send succeeds with the checked, un-inlined HTML and its
// style block, and one warning per layout per minute names the layout.
func TestInlineUnsafeOutput(t *testing.T) {
t.Parallel()
brand := &Brand{ButtonPrimaryBg: inlineBrandColour}
links := []struct{ name, link string }{
{"plain", `
x`},
{"encoded", `
x`},
}
for _, l := range links {
t.Run(l.name, func(t *testing.T) {
t.Parallel()
m, drv := partialsMailer(t, Options{CSS: inlineMedia}, &Overrides{Brand: brand})
calls := 0
m.(*mailer).inline = func(html string) (string, error) {
calls++
return strings.Replace(html, "