fix(quick-261114-p5m): read tags across raw-text and comment ends in the mail URL check

- unsafeMarkupAt runs the existing HTML reading, then a tag reading in
  which attribute quoting alone decides where a tag ends
  (NextIsNotRawText after raw-text start tags, comment openers blanked)
- both readings share unsafeAttrs; existing refusals keep their message
  and snippet
- svg/style end tag inside a quoted attribute is refused at check, save
  and send; README and docs/services/mail.md describe the second reading
This commit is contained in:
Jakub Zych
2026-11-14 18:25:13 +01:00
parent d8137c17d4
commit d6ac0aeb4b
4 changed files with 97 additions and 17 deletions

View File

@@ -27,7 +27,7 @@ Use cases:
- Layouts with a header, a text wrapper and an HTML wrapper, referenced from templates by a short alias (`pact.HasMailTemplates` maps aliases to full names). Three layouts are built in and usable from any plugin's templates: `default`, `system` and `minimal`.
- Partials: reusable blocks named `<plugin>::mail.partials.<name>`, loaded from `views/mail/partials/<name>.htm` and declared through `pact.HasMailPartials`. Bodies and layouts call them with `{{ partial "<name>" "key" value }}`. The partials `button`, `panel`, `table` and `subcopy` are built in.
- Every message gets an HTML part (Markdown converted with goldmark) and a plain-text part. The brand CSS (`mail.brandCss`, or the saved Branding) and `mail.css` go into the layout's style block, and the rendered HTML is then inlined: each CSS rule is copied into the `style` attribute of the elements it matches (go-premailer), so mail clients that drop style blocks still show the design. A layout opts out with `disable_auto_inline_css`.
- Safety checks: rendered HTML with script, iframe, object or embed tags, inline event handlers (any attribute whose name starts with `on`) or `javascript:`/`vbscript:`/`data:` URLs is rejected. Schemes are looked for in URL-bearing attributes, `style` attributes, style elements, comments and `meta` content, after character references are decoded and whitespace and control characters removed (`javascript&#58;` and `java&#9;script:` are `javascript:`). The content of `noscript`, `title`, `textarea`, `style`, `xmp` and the other raw-text elements, and of comments, is checked as markup too, because a mail client may read it so (scripting off, `svg` content, Outlook conditional comments); such elements nested more than eight deep are refused. Text is neither markup nor a URL: it is not checked for schemes, and text shaped like an event handler (`buy one = get one free`, `online = yes`) is sent. CR/LF in the subject or address headers is rejected; addresses are parsed with `net/mail`.
- Safety checks: rendered HTML with script, iframe, object or embed tags, inline event handlers (any attribute whose name starts with `on`) or `javascript:`/`vbscript:`/`data:` URLs is rejected. Schemes are looked for in URL-bearing attributes, `style` attributes, style elements, comments and `meta` content, after character references are decoded and whitespace and control characters removed (`javascript&#58;` and `java&#9;script:` are `javascript:`). The content of `noscript`, `title`, `textarea`, `style`, `xmp` and the other raw-text elements, and of comments, is checked as markup too, because a mail client may read it so (scripting off, `svg` content, Outlook conditional comments); such elements nested more than eight deep are refused. Every tag is also read with attribute quoting alone deciding where it ends, so an end tag or `-->` inside a quoted attribute (`<a title="</style>" href="javascript:...">`) does not hide the rest of the tag. Text is neither markup nor a URL: it is not checked for schemes, and text shaped like an event handler (`buy one = get one free`, `online = yes`) is sent. CR/LF in the subject or address headers is rejected; addresses are parsed with `net/mail`.
- Drivers behind the `postcard.Driver` interface: `postcard.MemoryDriver` (keeps messages for tests), `postcard.LogDriver` (logs headers and the text part through `log/slog`, never the HTML or credentials), `postcard.SMTPDriver` (go-mail with an explicit TLS policy) and `postcard.FailDriver` (a deterministic failure for tests).
- Database overrides: a customized row of `system_mail_templates` replaces a template's subject, body and layout, a customized row of `system_mail_layouts` or `system_mail_partials` replaces a layout or a partial, and `system_mail_layouts` also holds layouts that exist only in the database. The mailer reads them on every send, with no cache, in every process that publishes a `*gorm.DB`. A missing database, tables that are not migrated yet, or an override that does not render or fails the safety checks sends the file version.
- Branding: once the branding settings have been saved, the brand CSS is compiled from their colours (`postcard.CompileBrandCSS`) and replaces `mail.brandCss`, and layouts get the logo's absolute URL as `{{ .logoUrl }}` and the site name as `{{ .siteName }}` (else the display name of `mail.from`). The built-in default layout shows them in a header and a copyright footer.

View File

@@ -115,7 +115,8 @@ func containsDangerousScheme(val string) bool {
// content as markup: it renders with scripting off, so noscript content is
// markup, and title, textarea, style, xmp and the like are foreign elements
// with markup children inside svg or math. unsafeMarkup checks their content
// as markup too.
// as markup too, and its tag reading reads their content as markup in place,
// calling NextIsNotRawText after their start tags.
var rawTextElements = map[string]bool{
"iframe": true,
"noembed": true,
@@ -148,6 +149,27 @@ var rawTextElements = map[string]bool{
// with scripting off, title or textarea in svg, Outlook's conditional
// comments). Each nested check reads a strictly shorter text, so the
// recursion ends; markup nested deeper than maxMarkupDepth is refused.
//
// After this HTML reading, the same text is read a second time as tags only
// (unsafeTagReading). The tokenizer ends a raw-text element at the first
// matching end tag and a comment at the first "-->", even when that end sits
// inside a quoted attribute; a parser that reads the content as markup keeps
// the attribute together, so in <a title="</style>" href="javascript:x"> the
// href is live although the HTML reading saw it as text. The tag reading lets
// attribute quoting alone decide where a tag ends: it does not stop at
// raw-text or comment boundaries and applies the same attribute checks to
// every start tag. It runs after the HTML reading, so every refusal of the
// HTML reading keeps its message and snippet, and it runs at every depth, so
// content re-read as markup is read with quoting deciding too. Each reading
// is one linear pass; the depth limit bounds the recursion.
//
// Rejected alternatives: re-checking from a raw-text element's content start
// to the end of the document re-reads the rest of the document once per
// raw-text element and comment inside the recursion, which is quadratic or
// worse on hostile input; using x/net/html's parse tree as the check loses
// the raw snippet the editor shows, reads xmp and noembed as raw text and
// ends a conditional comment at the first "-->", while their content is
// checked here as markup.
func unsafeMarkup(doc string) error {
return unsafeMarkupAt(doc, 0)
}
@@ -162,6 +184,38 @@ func unsafeMarkupAt(doc string, depth int) error {
if depth > maxMarkupDepth {
return &unsafeError{msg: "rendered HTML nests raw-text elements or comments too deeply", snippet: snippetAt(doc, []int{0, min(len(doc), 48)})}
}
if err := unsafeHTMLReading(doc, depth); err != nil {
return err
}
return unsafeTagReading(doc)
}
// unsafeAttrs checks the attributes of start tag t, found at loc in doc:
// event handlers and dangerous URL schemes. The snippet points at the tag.
func unsafeAttrs(t xhtml.Token, doc string, loc []int) error {
for _, a := range t.Attr {
key := a.Key
if a.Namespace != "" {
key = a.Namespace + ":" + a.Key
}
switch {
case strings.HasPrefix(key, "on"):
return &unsafeError{msg: "rendered HTML contains event handlers", snippet: snippetAt(doc, loc)}
case urlAttributes[key] && hasDangerousScheme(a.Val),
urlListAttributes[key] && containsDangerousScheme(a.Val),
key == "style" && containsDangerousScheme(a.Val),
t.Data == "meta" && key == "content" && containsDangerousScheme(a.Val):
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
}
return nil
}
// unsafeHTMLReading reads doc as x/net/html's tokenizer does: it checks
// start-tag attributes, the text of style elements and comments, and checks
// the content of raw-text elements and comments again as markup one level
// deeper.
func unsafeHTMLReading(doc string, depth int) error {
z := xhtml.NewTokenizer(strings.NewReader(doc))
offset := 0
inStyle := false
@@ -181,20 +235,8 @@ func unsafeMarkupAt(doc string, depth int) error {
rawTag = ""
switch tt {
case xhtml.StartTagToken, xhtml.SelfClosingTagToken:
for _, a := range t.Attr {
key := a.Key
if a.Namespace != "" {
key = a.Namespace + ":" + a.Key
}
switch {
case strings.HasPrefix(key, "on"):
return &unsafeError{msg: "rendered HTML contains event handlers", snippet: snippetAt(doc, loc)}
case urlAttributes[key] && hasDangerousScheme(a.Val),
urlListAttributes[key] && containsDangerousScheme(a.Val),
key == "style" && containsDangerousScheme(a.Val),
t.Data == "meta" && key == "content" && containsDangerousScheme(a.Val):
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
if err := unsafeAttrs(t, doc, loc); err != nil {
return err
}
// The tokenizer reads the content of a raw-text element as text
// even after a self-closing start tag (<style/>), as browsers do.
@@ -230,3 +272,34 @@ func unsafeMarkupAt(doc string, depth int) error {
}
}
}
// unsafeTagReading reads doc as tags only, with attribute quoting alone
// deciding where a tag ends, and checks every start tag's attributes. It
// calls NextIsNotRawText after each raw-text start tag, so the element's
// content is read as tags, and it tokenizes a copy of doc in which the "<"
// of every "<!--" is a space, so no comment is recognised. Only the "<" is
// blanked: the copy has the length of doc, so offsets and snippets point
// into doc, and an attribute value holding a literal "<!--" does not turn
// into a scheme prefix once stripURLSpace removes the space.
func unsafeTagReading(doc string) error {
z := xhtml.NewTokenizer(strings.NewReader(strings.ReplaceAll(doc, "<!--", " !--")))
offset := 0
for {
tt := z.Next()
if tt == xhtml.ErrorToken {
return nil
}
start := offset
offset += len(z.Raw())
if tt != xhtml.StartTagToken && tt != xhtml.SelfClosingTagToken {
continue
}
t := z.Token()
if rawTextElements[t.Data] {
z.NextIsNotRawText()
}
if err := unsafeAttrs(t, doc, []int{start, offset}); err != nil {
return err
}
}
}

View File

@@ -83,6 +83,10 @@ func TestURLSchemeCheck(t *testing.T) {
{"xmp_href", `<xmp><a href="javascript:alert(1)">x</a></xmp>`, `href="javascript:alert(1)"`, schemeMsg},
{"self_closing_style", `<style/><a href="javascript&#58;alert(1)">x</a>`, `javascript&#58;alert(1)`, schemeMsg},
{"conditional_comment_handler", `<!--[if mso]><img src="x.png" onerror="alert(1)"><![endif]-->`, `onerror="alert(1)"`, "event handlers"},
// An end tag inside a quoted attribute ends the raw-text element for
// the tokenizer but not for a conforming parser, which keeps the
// rest of the tag together (CR-02 residual).
{"svg_style_end_in_attr", `<svg><style><a title="</style>" href="javascript:alert(1)">x</a></style></svg>`, `href="javascript:alert(1)"`, schemeMsg},
// Whitespace-split schemes in list, meta, CSS and comment values
// (WR-01).
{"values_tab_split", `<svg><a><animate attributeName="href" values="java&#9;script:alert(1)"/><text>x</text></a></svg>`, `values="java&#9;script:alert(1)"`, schemeMsg},
@@ -123,6 +127,9 @@ func TestURLSchemeCheck(t *testing.T) {
{"encoded", encodedLink},
{"noscript", `<noscript><a href="javascript:alert(1)">x</a></noscript>`},
{"xmp", `<xmp><a href="javascript:alert(1)">x</a></xmp>`},
// An end tag inside a quoted attribute does not hide the rest of the
// tag (CR-02 residual).
{"svg_style_end_in_attr", `<svg><style><a title="</style>" href="javascript:alert(1)">x</a></style></svg>`},
}
file := fileWelcome(t)
if strings.Contains(file.HTML, "javascript") {