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:
@@ -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:` 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:` and `java	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.
|
||||
|
||||
@@ -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	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 {
|
||||
|
||||
@@ -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:alert(1)">x</a>`, `javascript: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	script:alert(1)"/><text>x</text></a></svg>`, `values="java	script:alert(1)"`, schemeMsg},
|
||||
{"meta_refresh_tab_split", `<meta http-equiv="refresh" content="0;url=java	script:alert(1)">`, `url=java	script:alert(1)`, schemeMsg},
|
||||
{"srcset_newline_split", `<img src="a.png" srcset="a.png 1x, java script:x 2x">`, `java script:x 2x`, schemeMsg},
|
||||
{"style_tab_split", `<div style="background:url(java	script:x)">x</div>`, `url(java	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 {
|
||||
|
||||
Reference in New Issue
Block a user