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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user