2020-05-13 12:07:44 +00:00
|
|
|
package alpsviewhtml
|
2020-01-08 13:25:46 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-20 17:28:49 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-02-25 09:47:38 +00:00
|
|
|
"net/url"
|
2020-01-20 17:28:49 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
alpsbase "git.sr.ht/~emersion/alps/plugins/base"
|
2020-01-20 17:28:49 +00:00
|
|
|
"github.com/aymerick/douceur/css"
|
|
|
|
cssparser "github.com/chris-ramon/douceur/parser"
|
2020-01-20 19:27:49 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"golang.org/x/net/html"
|
2020-01-20 17:28:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: this doesn't accomodate for quoting
|
|
|
|
var (
|
2020-01-20 19:27:49 +00:00
|
|
|
cssURLRegexp = regexp.MustCompile(`url\([^)]*\)`)
|
2020-01-20 17:28:49 +00:00
|
|
|
cssExprRegexp = regexp.MustCompile(`expression\([^)]*\)`)
|
2020-01-08 13:25:46 +00:00
|
|
|
)
|
|
|
|
|
2020-01-20 17:28:49 +00:00
|
|
|
var allowedStyles = map[string]bool{
|
2020-01-20 19:27:49 +00:00
|
|
|
"direction": true,
|
|
|
|
"font": true,
|
|
|
|
"font-family": true,
|
|
|
|
"font-style": true,
|
|
|
|
"font-variant": true,
|
|
|
|
"font-size": true,
|
|
|
|
"font-weight": true,
|
|
|
|
"letter-spacing": true,
|
|
|
|
"line-height": true,
|
|
|
|
"text-align": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
"text-decoration": true,
|
2020-01-20 19:27:49 +00:00
|
|
|
"text-indent": true,
|
|
|
|
"text-overflow": true,
|
|
|
|
"text-shadow": true,
|
|
|
|
"text-transform": true,
|
|
|
|
"white-space": true,
|
|
|
|
"word-spacing": true,
|
|
|
|
"word-wrap": true,
|
|
|
|
"vertical-align": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
|
2020-01-20 19:27:49 +00:00
|
|
|
"color": true,
|
|
|
|
"background": true,
|
|
|
|
"background-color": true,
|
|
|
|
"background-image": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
"background-repeat": true,
|
|
|
|
|
2020-01-20 19:27:49 +00:00
|
|
|
"border": true,
|
|
|
|
"border-color": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
"border-radius": true,
|
2020-01-20 19:27:49 +00:00
|
|
|
"height": true,
|
|
|
|
"margin": true,
|
|
|
|
"padding": true,
|
|
|
|
"width": true,
|
|
|
|
"max-width": true,
|
|
|
|
"min-width": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
|
|
|
|
"clear": true,
|
|
|
|
"float": true,
|
|
|
|
|
|
|
|
"border-collapse": true,
|
2020-01-20 19:27:49 +00:00
|
|
|
"border-spacing": true,
|
|
|
|
"caption-side": true,
|
|
|
|
"empty-cells": true,
|
|
|
|
"table-layout": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
|
2020-01-20 19:27:49 +00:00
|
|
|
"list-style-type": true,
|
2020-01-20 17:28:49 +00:00
|
|
|
"list-style-position": true,
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
type sanitizer struct {
|
2020-05-13 12:07:44 +00:00
|
|
|
msg *alpsbase.IMAPMessage
|
2020-02-25 15:13:10 +00:00
|
|
|
allowRemoteResources bool
|
|
|
|
hasRemoteResources bool
|
2020-02-25 08:51:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 09:47:38 +00:00
|
|
|
func (san *sanitizer) sanitizeImageURL(src string) string {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
|
|
|
return "about:blank"
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:45:43 +00:00
|
|
|
switch strings.ToLower(u.Scheme) {
|
2020-02-25 14:37:47 +00:00
|
|
|
// TODO: mid support?
|
2020-02-25 14:45:43 +00:00
|
|
|
case "cid":
|
|
|
|
if san.msg == nil {
|
|
|
|
return "about:blank"
|
|
|
|
}
|
|
|
|
|
|
|
|
part := san.msg.PartByID(u.Opaque)
|
|
|
|
if part == nil || !strings.HasPrefix(part.MIMEType, "image/") {
|
|
|
|
return "about:blank"
|
|
|
|
}
|
|
|
|
|
|
|
|
return part.URL(true).String()
|
|
|
|
case "https":
|
2020-02-25 15:13:10 +00:00
|
|
|
san.hasRemoteResources = true
|
|
|
|
|
|
|
|
if !proxyEnabled || !san.allowRemoteResources {
|
2020-02-25 14:45:43 +00:00
|
|
|
return "about:blank"
|
|
|
|
}
|
2020-02-25 09:47:38 +00:00
|
|
|
|
2020-02-25 14:45:43 +00:00
|
|
|
proxyURL := url.URL{Path: "/proxy"}
|
|
|
|
proxyQuery := make(url.Values)
|
|
|
|
proxyQuery.Set("src", u.String())
|
|
|
|
proxyURL.RawQuery = proxyQuery.Encode()
|
|
|
|
return proxyURL.String()
|
|
|
|
default:
|
2020-02-25 09:47:38 +00:00
|
|
|
return "about:blank"
|
|
|
|
}
|
2020-02-25 08:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (san *sanitizer) sanitizeCSSDecls(decls []*css.Declaration) []*css.Declaration {
|
2020-01-20 17:28:49 +00:00
|
|
|
sanitized := make([]*css.Declaration, 0, len(decls))
|
|
|
|
for _, decl := range decls {
|
|
|
|
if !allowedStyles[decl.Property] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cssExprRegexp.FindStringIndex(decl.Value) != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: more robust CSS declaration parsing
|
|
|
|
decl.Value = cssURLRegexp.ReplaceAllString(decl.Value, "url(about:blank)")
|
|
|
|
|
|
|
|
sanitized = append(sanitized, decl)
|
|
|
|
}
|
|
|
|
return sanitized
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
func (san *sanitizer) sanitizeCSSRule(rule *css.Rule) {
|
2020-01-20 17:28:49 +00:00
|
|
|
// Disallow @import
|
|
|
|
if rule.Kind == css.AtRule && strings.EqualFold(rule.Name, "@import") {
|
|
|
|
rule.Prelude = "url(about:blank)"
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
rule.Declarations = san.sanitizeCSSDecls(rule.Declarations)
|
2020-01-20 17:28:49 +00:00
|
|
|
|
|
|
|
for _, child := range rule.Rules {
|
2020-02-25 08:51:57 +00:00
|
|
|
san.sanitizeCSSRule(child)
|
2020-01-20 17:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
func (san *sanitizer) sanitizeNode(n *html.Node) {
|
2020-01-20 17:28:49 +00:00
|
|
|
if n.Type == html.ElementNode {
|
|
|
|
if strings.EqualFold(n.Data, "img") {
|
|
|
|
for i := range n.Attr {
|
|
|
|
attr := &n.Attr[i]
|
|
|
|
if strings.EqualFold(attr.Key, "src") {
|
2020-02-25 09:47:38 +00:00
|
|
|
attr.Val = san.sanitizeImageURL(attr.Val)
|
2020-01-20 17:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if strings.EqualFold(n.Data, "style") {
|
|
|
|
var s string
|
|
|
|
c := n.FirstChild
|
|
|
|
for c != nil {
|
|
|
|
if c.Type == html.TextNode {
|
|
|
|
s += c.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
next := c.NextSibling
|
|
|
|
n.RemoveChild(c)
|
|
|
|
c = next
|
|
|
|
}
|
|
|
|
|
|
|
|
stylesheet, err := cssparser.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
s = ""
|
|
|
|
} else {
|
|
|
|
for _, rule := range stylesheet.Rules {
|
2020-02-25 08:51:57 +00:00
|
|
|
san.sanitizeCSSRule(rule)
|
2020-01-20 17:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = stylesheet.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
n.AppendChild(&html.Node{
|
|
|
|
Type: html.TextNode,
|
|
|
|
Data: s,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range n.Attr {
|
|
|
|
// Don't use `i, attr := range n.Attr` since `attr` would be a copy
|
|
|
|
attr := &n.Attr[i]
|
|
|
|
|
|
|
|
if strings.EqualFold(attr.Key, "style") {
|
|
|
|
decls, err := cssparser.ParseDeclarations(attr.Val)
|
|
|
|
if err != nil {
|
|
|
|
attr.Val = ""
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
decls = san.sanitizeCSSDecls(decls)
|
2020-01-20 17:28:49 +00:00
|
|
|
|
|
|
|
attr.Val = ""
|
|
|
|
for _, d := range decls {
|
|
|
|
attr.Val += d.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
2020-02-25 08:51:57 +00:00
|
|
|
san.sanitizeNode(c)
|
2020-01-20 17:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
func (san *sanitizer) sanitizeHTML(b []byte) ([]byte, error) {
|
2020-01-20 17:28:49 +00:00
|
|
|
doc, err := html.Parse(bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse HTML: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:51:57 +00:00
|
|
|
san.sanitizeNode(doc)
|
2020-01-20 17:28:49 +00:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := html.Render(&buf, doc); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to render HTML: %v", err)
|
|
|
|
}
|
|
|
|
b = buf.Bytes()
|
|
|
|
|
|
|
|
// bluemonday must always be run last
|
2020-01-08 13:25:46 +00:00
|
|
|
p := bluemonday.UGCPolicy()
|
|
|
|
|
2020-01-20 17:28:49 +00:00
|
|
|
// TODO: use bluemonday's AllowStyles once it's released and
|
|
|
|
// supports <style>
|
2020-01-08 13:25:46 +00:00
|
|
|
p.AllowElements("style")
|
2020-01-20 17:28:49 +00:00
|
|
|
p.AllowAttrs("style").Globally()
|
2020-01-08 13:25:46 +00:00
|
|
|
|
|
|
|
p.AddTargetBlankToFullyQualifiedLinks(true)
|
|
|
|
p.RequireNoFollowOnLinks(true)
|
|
|
|
|
2020-01-20 17:28:49 +00:00
|
|
|
return p.SanitizeBytes(b), nil
|
2020-01-08 13:25:46 +00:00
|
|
|
}
|