fix(35): WR-01 strip whitespace before the list, meta, CSS and comment scheme checks

Single-URL attributes removed ASCII whitespace and control characters
before looking for a scheme, the list-valued, meta content, style and
comment checks did not, so values="java	script:..." and a tab-split
meta refresh passed. The stripping is factored out as stripURLSpace and
used by every scheme check.
This commit is contained in:
Jakub Zych
2026-11-14 17:32:14 +01:00
parent 1d481bb02d
commit baba524caf
4 changed files with 36 additions and 14 deletions

View File

@@ -155,7 +155,7 @@ fmt.Println(err != nil, len(driver.Messages()))
postcard does not pick a locale: the caller passes the full name. A per-language version of a template is a file named after the template, a dash and the locale (`welcome-en.htm`, sent as `acme.blog::mail.welcome-en`). A name with its own file always renders that file. A name the catalog does not hold, of the form `<template>-<locale>` where `<template>` is registered and `<locale>` is a locale tag (two letters, optionally followed by `-` or `_` and a 2 to 8 character subtag, such as `de` or `pt-BR`), sends the default-locale version: the locale's own version when an administrator customized it, else the customized template, else the template's file. Any other unknown name fails with `postcard: unknown template <name>`. So a plugin can pass `<template>-<locale>` for every locale it serves, and a locale without a file still sends mail, in the default language until someone writes that locale's version in the admin.
Before a driver sees a message, postcard refuses a subject or address with a line break, parses every address with `net/mail`, and rejects rendered HTML that contains script, iframe, object or embed tags, inline event handlers, or `javascript:`, `vbscript:` or `data:` URLs. URLs are looked for where a mail client reads one: URL-bearing attributes such as `href` and `src`, `style` attributes, style elements, comments and `meta` content, after character references are decoded, so `href="javascript&#58;..."` is refused. Markup inside `noscript`, `title`, `textarea`, `style`, `xmp` and similar elements, and inside comments, is checked as well, since a mail client may read it as markup. Text is not a URL: a body or a site name such as `metadata:` or `Big Data: Summit` is sent. Variables are escaped by Go's `html/template`.
Before a driver sees a message, postcard refuses a subject or address with a line break, parses every address with `net/mail`, and rejects rendered HTML that contains script, iframe, object or embed tags, inline event handlers, or `javascript:`, `vbscript:` or `data:` URLs. URLs are looked for where a mail client reads one: URL-bearing attributes such as `href` and `src`, `style` attributes, style elements, comments and `meta` content, after character references are decoded and whitespace removed, so `href="javascript&#58;..."` and `values="java&#9;script:..."` are refused. Markup inside `noscript`, `title`, `textarea`, `style`, `xmp` and similar elements, and inside comments, is checked as well, since a mail client may read it as markup. Text is not a URL: a body or a site name such as `metadata:` or `Big Data: Summit` is sent. Variables are escaped by Go's `html/template`.
`postcard.Mailer.Send` delivers before it returns. To keep a request fast, send from a queued job, and send after the write that triggered the mail has committed; see [Queued jobs](jobs.md) and [Transactions](../database/transactions.md).

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 (`javascript&#58;` is `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 not a URL and is not checked for them. 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. Text is not a URL and is not checked for them. 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

@@ -63,7 +63,8 @@ var urlAttributes = map[string]bool{
}
// urlListAttributes hold several URLs (or values that can be URLs). Their
// decoded value is refused when dangerousScheme matches anywhere in it.
// decoded value is refused when containsDangerousScheme finds a scheme
// anywhere in it.
var urlListAttributes = map[string]bool{
"srcset": true,
"ping": true,
@@ -71,11 +72,10 @@ var urlListAttributes = map[string]bool{
"values": true,
}
// hasDangerousScheme reports whether a decoded URL attribute value starts
// with javascript:, vbscript: or data:, after every ASCII whitespace and
// control character is removed and the value is lower-cased, the way
// browsers read it ("java\tscript:" is javascript:).
func hasDangerousScheme(val string) bool {
// stripURLSpace removes every ASCII whitespace and control character
// (U+0000 to U+0020 and U+007F) from val and lower-cases it, the way
// browsers read a URL ("java\tscript:" is javascript:).
func stripURLSpace(val string) string {
var b strings.Builder
b.Grow(len(val))
for i := 0; i < len(val); i++ {
@@ -88,10 +88,25 @@ func hasDangerousScheme(val string) bool {
}
b.WriteByte(c)
}
v := b.String()
return b.String()
}
// hasDangerousScheme reports whether a decoded URL attribute value starts
// with javascript:, vbscript: or data:, once stripURLSpace has removed
// whitespace and control characters.
func hasDangerousScheme(val string) bool {
v := stripURLSpace(val)
return strings.HasPrefix(v, "javascript:") || strings.HasPrefix(v, "vbscript:") || strings.HasPrefix(v, "data:")
}
// containsDangerousScheme reports whether dangerousScheme matches anywhere
// in a decoded value (a URL list, CSS, meta content or a comment) once
// stripURLSpace has removed whitespace and control characters, so a
// tab-split "java&#9;script:" is found as in a single-URL attribute.
func containsDangerousScheme(val string) bool {
return dangerousScheme.MatchString(stripURLSpace(val))
}
// rawTextElements are the elements whose content x/net/html's tokenizer
// reads as one text token (raw text or RCDATA). A mail client may parse that
// content as markup: it renders with scripting off, so noscript content is
@@ -172,9 +187,9 @@ func unsafeMarkupAt(doc string, depth int) error {
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] && dangerousScheme.MatchString(a.Val),
key == "style" && dangerousScheme.MatchString(a.Val),
t.Data == "meta" && key == "content" && dangerousScheme.MatchString(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)}
}
}
@@ -191,7 +206,7 @@ func unsafeMarkupAt(doc string, depth int) error {
inStyle = false
}
case xhtml.TextToken:
if inStyle && dangerousScheme.MatchString(t.Data) {
if inStyle && containsDangerousScheme(t.Data) {
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
// The raw source, not the decoded text: RCDATA (title,
@@ -203,7 +218,7 @@ func unsafeMarkupAt(doc string, depth int) error {
}
}
case xhtml.CommentToken:
if dangerousScheme.MatchString(t.Data) {
if containsDangerousScheme(t.Data) {
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(doc, loc)}
}
if err := unsafeMarkupAt(t.Data, depth+1); err != nil {

View File

@@ -78,6 +78,13 @@ 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"},
// 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},
{"meta_refresh_tab_split", `<meta http-equiv="refresh" content="0;url=java&#9;script:alert(1)">`, `url=java&#9;script:alert(1)`, schemeMsg},
{"srcset_newline_split", `<img src="a.png" srcset="a.png 1x, java&#10;script:x 2x">`, `java&#10;script:x 2x`, schemeMsg},
{"style_tab_split", `<div style="background:url(java&#9;script:x)">x</div>`, `url(java&#9;script:x)`, schemeMsg},
{"style_element_space_split", "<style>td { background: url(java\tscript:x); }</style>", "url(java script:x)", schemeMsg},
{"nested_too_deep", strings.Repeat("<noscript>", maxMarkupDepth+2) + "x", "<noscript>", "too deeply"},
}
for _, tc := range refuses {