fix(postcard): check URL schemes in decoded attribute values, not in text

- validateHTML keeps the raw tag and handler regexes and reads the rest
  through unsafeMarkup: x/net/html tokenizer, decoded attribute values,
  prefix check on URL attributes, whole-value check on CSS, list
  attributes, meta content and comments; any on* attribute is refused
- text is not a URL: "Metadata: Weekly" or "Big Data: Summit" as a site
  name or body text no longer stops a send (CR-01)
- entity-encoded javascript: is refused whether or not the layout
  opts out of inlining (WR-05)
- draft layout CSS is checked as a style element
- fixtures that pinned prose as unsafe now carry markup-level URLs;
  the 35-07 subtest pinning an unsafe stored name is removed
- TestMailSafetySmoke prose_site_name and encoded_scheme
This commit is contained in:
Jakub Zych
2026-11-14 15:13:35 +01:00
parent 274dff4458
commit 13ab905f8d
11 changed files with 277 additions and 35 deletions

View File

@@ -42,6 +42,9 @@ var builtinLayoutAliases = map[string]string{
var builtinPartials = []string{"button", "panel", "subcopy", "table", "header", "footer"}
var (
// dangerousScheme finds a script or data URL scheme anywhere in a value.
// unsafeMarkup applies it to CSS (style attributes and style elements),
// list-valued URL attributes, meta content and comments, never to text.
dangerousScheme = regexp.MustCompile(`(?i)(?:javascript|vbscript|data):`)
eventHandler = regexp.MustCompile(`(?i)\son[a-z]+\s*=`)
rawUnsafeTag = regexp.MustCompile(`(?i)<(?:script|iframe|object|embed)\b`)
@@ -810,6 +813,11 @@ func markdownHTML(src string) (string, error) {
return buf.String(), nil
}
// validateHTML is the safety check of rendered HTML. Raw unsafe tags
// (script, iframe, object, embed) and whitespace-prefixed on* attributes are
// found in the raw markup; unsafeMarkup then reads the decoded attribute
// values, style elements and comments for event handlers and script or data
// URLs. Text content is not a URL and is not checked for schemes.
func validateHTML(html string) error {
if loc := rawUnsafeTag.FindStringIndex(html); loc != nil {
return &unsafeError{msg: "rendered HTML contains raw unsafe tags", snippet: snippetAt(html, loc)}
@@ -817,10 +825,7 @@ func validateHTML(html string) error {
if loc := eventHandler.FindStringIndex(html); loc != nil {
return &unsafeError{msg: "rendered HTML contains event handlers", snippet: snippetAt(html, loc)}
}
if loc := dangerousScheme.FindStringIndex(html); loc != nil {
return &unsafeError{msg: "rendered HTML contains a dangerous URL scheme", snippet: snippetAt(html, loc)}
}
return nil
return unsafeMarkup(html)
}
// snippetAt returns the match at loc with up to snippetContext bytes on