force <style></style> removal in html part of incoming emails
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -16,6 +17,8 @@ import (
|
|||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var styleRegex = regexp.MustCompile("<style((.|\n|\r)*?)<\\/style>")
|
||||||
|
|
||||||
// IncomingFilteringOptions for incoming mail
|
// IncomingFilteringOptions for incoming mail
|
||||||
type IncomingFilteringOptions interface {
|
type IncomingFilteringOptions interface {
|
||||||
SpamcheckSMTP() bool
|
SpamcheckSMTP() bool
|
||||||
@@ -82,11 +85,8 @@ func NewEmail(messageID, inReplyTo, references, subject, from, to, text, html st
|
|||||||
}
|
}
|
||||||
|
|
||||||
if html != "" {
|
if html != "" {
|
||||||
var err error
|
html = styleRegex.ReplaceAllString(html, "")
|
||||||
html, err = StripHTMLTag(html, "style")
|
email.HTML = html
|
||||||
if err == nil {
|
|
||||||
email.HTML = html
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return email
|
return email
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"golang.org/x/net/html"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StripHTMLTag from text
|
|
||||||
//
|
|
||||||
// Source: https://siongui.github.io/2018/01/16/go-remove-html-inline-style/
|
|
||||||
func StripHTMLTag(text, tag string) (string, error) {
|
|
||||||
doc, err := html.Parse(strings.NewReader(text))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
stripHTMLTag(doc, tag)
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
|
||||||
err = html.Render(&out, doc)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return out.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripHTMLTag(node *html.Node, tag string) {
|
|
||||||
i := -1
|
|
||||||
for index, attr := range node.Attr {
|
|
||||||
if attr.Key == tag {
|
|
||||||
i = index
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if i != -1 {
|
|
||||||
node.Attr = append(node.Attr[:i], node.Attr[i+1:]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
|
||||||
stripHTMLTag(child, tag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user