2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-06-12 21:45:09 -04:00
|
|
|
"regexp"
|
2020-09-29 02:07:04 -04:00
|
|
|
"strconv"
|
2017-11-20 00:10:04 -05:00
|
|
|
"strings"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
2023-08-13 22:09:01 -04:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2017-11-25 21:08:59 -05:00
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
"golang.org/x/net/html"
|
|
|
|
)
|
|
|
|
|
2018-06-12 21:45:09 -04:00
|
|
|
var (
|
2018-07-05 01:45:44 -04:00
|
|
|
youtubeEmbedRegex = regexp.MustCompile(`//www\.youtube\.com/embed/(.*)`)
|
2018-06-12 21:45:09 -04:00
|
|
|
)
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
// Sanitize returns safe HTML.
|
|
|
|
func Sanitize(baseURL, input string) string {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
var tagStack []string
|
2021-02-13 16:52:18 -05:00
|
|
|
var parentTag string
|
2018-06-24 13:16:07 -04:00
|
|
|
blacklistedTagDepth := 0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2021-02-13 16:52:18 -05:00
|
|
|
tokenizer := html.NewTokenizer(bytes.NewBufferString(input))
|
2017-11-20 00:10:04 -05:00
|
|
|
for {
|
|
|
|
if tokenizer.Next() == html.ErrorToken {
|
|
|
|
err := tokenizer.Err()
|
|
|
|
if err == io.EOF {
|
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
token := tokenizer.Token()
|
|
|
|
switch token.Type {
|
|
|
|
case html.TextToken:
|
2018-06-24 13:16:07 -04:00
|
|
|
if blacklistedTagDepth > 0 {
|
2018-06-23 20:50:43 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-13 16:52:18 -05:00
|
|
|
// An iframe element never has fallback content.
|
|
|
|
// See https://www.w3.org/TR/2010/WD-html5-20101019/the-iframe-element.html#the-iframe-element
|
|
|
|
if parentTag == "iframe" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:08:59 -05:00
|
|
|
buffer.WriteString(html.EscapeString(token.Data))
|
2017-11-20 00:10:04 -05:00
|
|
|
case html.StartTagToken:
|
|
|
|
tagName := token.DataAtom.String()
|
2021-02-13 16:52:18 -05:00
|
|
|
parentTag = tagName
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
if !isPixelTracker(tagName, token.Attr) && isValidTag(tagName) {
|
|
|
|
attrNames, htmlAttributes := sanitizeAttributes(baseURL, tagName, token.Attr)
|
|
|
|
|
|
|
|
if hasRequiredAttributes(tagName, attrNames) {
|
|
|
|
if len(attrNames) > 0 {
|
|
|
|
buffer.WriteString("<" + tagName + " " + htmlAttributes + ">")
|
|
|
|
} else {
|
|
|
|
buffer.WriteString("<" + tagName + ">")
|
|
|
|
}
|
|
|
|
|
|
|
|
tagStack = append(tagStack, tagName)
|
|
|
|
}
|
2020-09-29 02:07:04 -04:00
|
|
|
} else if isBlockedTag(tagName) {
|
2018-06-24 13:16:07 -04:00
|
|
|
blacklistedTagDepth++
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
case html.EndTagToken:
|
|
|
|
tagName := token.DataAtom.String()
|
|
|
|
if isValidTag(tagName) && inList(tagName, tagStack) {
|
|
|
|
buffer.WriteString(fmt.Sprintf("</%s>", tagName))
|
2020-09-29 02:07:04 -04:00
|
|
|
} else if isBlockedTag(tagName) {
|
2018-06-24 13:16:07 -04:00
|
|
|
blacklistedTagDepth--
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
case html.SelfClosingTagToken:
|
|
|
|
tagName := token.DataAtom.String()
|
|
|
|
if !isPixelTracker(tagName, token.Attr) && isValidTag(tagName) {
|
|
|
|
attrNames, htmlAttributes := sanitizeAttributes(baseURL, tagName, token.Attr)
|
|
|
|
|
|
|
|
if hasRequiredAttributes(tagName, attrNames) {
|
|
|
|
if len(attrNames) > 0 {
|
|
|
|
buffer.WriteString("<" + tagName + " " + htmlAttributes + "/>")
|
|
|
|
} else {
|
|
|
|
buffer.WriteString("<" + tagName + "/>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:08:59 -05:00
|
|
|
func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([]string, string) {
|
|
|
|
var htmlAttrs, attrNames []string
|
2017-11-20 00:10:04 -05:00
|
|
|
var err error
|
2022-07-04 22:59:52 -04:00
|
|
|
var isImageLargerThanLayout bool
|
2022-09-12 01:32:16 -04:00
|
|
|
var isAnchorLink bool
|
2022-07-04 22:59:52 -04:00
|
|
|
|
|
|
|
if tagName == "img" {
|
|
|
|
imgWidth := getIntegerAttributeValue("width", attributes)
|
|
|
|
isImageLargerThanLayout = imgWidth > 750
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
for _, attribute := range attributes {
|
|
|
|
value := attribute.Val
|
|
|
|
|
|
|
|
if !isValidAttribute(tagName, attribute.Key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:07:04 -04:00
|
|
|
if (tagName == "img" || tagName == "source") && attribute.Key == "srcset" {
|
|
|
|
value = sanitizeSrcsetAttr(baseURL, value)
|
|
|
|
}
|
|
|
|
|
2022-07-04 22:59:52 -04:00
|
|
|
if tagName == "img" && (attribute.Key == "width" || attribute.Key == "height") {
|
|
|
|
if !isPositiveInteger(value) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if isImageLargerThanLayout {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-03 20:36:27 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
if isExternalResourceAttribute(attribute.Key) {
|
2018-06-12 21:45:09 -04:00
|
|
|
if tagName == "iframe" {
|
2020-09-06 16:41:42 -04:00
|
|
|
if isValidIframeSource(baseURL, attribute.Val) {
|
2018-06-12 21:45:09 -04:00
|
|
|
value = rewriteIframeURL(attribute.Val)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-06 17:33:28 -05:00
|
|
|
} else if tagName == "img" && attribute.Key == "src" && isValidDataAttribute(attribute.Val) {
|
2020-10-15 01:19:05 -04:00
|
|
|
value = attribute.Val
|
2022-09-12 01:32:16 -04:00
|
|
|
} else if isAnchor("a", attribute) {
|
|
|
|
value = attribute.Val
|
|
|
|
isAnchorLink = true
|
2017-11-20 00:10:04 -05:00
|
|
|
} else {
|
2023-08-13 22:09:01 -04:00
|
|
|
value, err = urllib.AbsoluteURL(baseURL, value)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:07:04 -04:00
|
|
|
if !hasValidURIScheme(value) || isBlockedResource(value) {
|
2017-11-20 00:10:04 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attrNames = append(attrNames, attribute.Key)
|
2017-11-25 21:08:59 -05:00
|
|
|
htmlAttrs = append(htmlAttrs, fmt.Sprintf(`%s="%s"`, attribute.Key, html.EscapeString(value)))
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2022-09-12 01:32:16 -04:00
|
|
|
if !isAnchorLink {
|
|
|
|
extraAttrNames, extraHTMLAttributes := getExtraAttributes(tagName)
|
|
|
|
if len(extraAttrNames) > 0 {
|
|
|
|
attrNames = append(attrNames, extraAttrNames...)
|
|
|
|
htmlAttrs = append(htmlAttrs, extraHTMLAttributes...)
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return attrNames, strings.Join(htmlAttrs, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExtraAttributes(tagName string) ([]string, []string) {
|
2018-07-02 03:16:27 -04:00
|
|
|
switch tagName {
|
|
|
|
case "a":
|
2017-11-20 00:10:04 -05:00
|
|
|
return []string{"rel", "target", "referrerpolicy"}, []string{`rel="noopener noreferrer"`, `target="_blank"`, `referrerpolicy="no-referrer"`}
|
2018-07-02 03:16:27 -04:00
|
|
|
case "video", "audio":
|
2017-11-20 00:10:04 -05:00
|
|
|
return []string{"controls"}, []string{"controls"}
|
2018-07-02 03:16:27 -04:00
|
|
|
case "iframe":
|
2019-09-11 00:12:38 -04:00
|
|
|
return []string{"sandbox", "loading"}, []string{`sandbox="allow-scripts allow-same-origin allow-popups"`, `loading="lazy"`}
|
|
|
|
case "img":
|
|
|
|
return []string{"loading"}, []string{`loading="lazy"`}
|
2018-07-02 03:16:27 -04:00
|
|
|
default:
|
|
|
|
return nil, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidTag(tagName string) bool {
|
2020-09-29 02:07:04 -04:00
|
|
|
for element := range getTagAllowList() {
|
2017-11-20 00:10:04 -05:00
|
|
|
if tagName == element {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidAttribute(tagName, attributeName string) bool {
|
2020-09-29 02:07:04 -04:00
|
|
|
for element, attributes := range getTagAllowList() {
|
2017-11-20 00:10:04 -05:00
|
|
|
if tagName == element {
|
|
|
|
if inList(attributeName, attributes) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isExternalResourceAttribute(attribute string) bool {
|
|
|
|
switch attribute {
|
|
|
|
case "src", "href", "poster", "cite":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPixelTracker(tagName string, attributes []html.Attribute) bool {
|
|
|
|
if tagName == "img" {
|
|
|
|
hasHeight := false
|
|
|
|
hasWidth := false
|
|
|
|
|
|
|
|
for _, attribute := range attributes {
|
|
|
|
if attribute.Key == "height" && attribute.Val == "1" {
|
|
|
|
hasHeight = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attribute.Key == "width" && attribute.Val == "1" {
|
|
|
|
hasWidth = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasHeight && hasWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasRequiredAttributes(tagName string, attributes []string) bool {
|
|
|
|
elements := make(map[string][]string)
|
|
|
|
elements["a"] = []string{"href"}
|
|
|
|
elements["iframe"] = []string{"src"}
|
|
|
|
elements["img"] = []string{"src"}
|
2020-09-29 02:07:04 -04:00
|
|
|
elements["source"] = []string{"src", "srcset"}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
for element, attrs := range elements {
|
|
|
|
if tagName == element {
|
|
|
|
for _, attribute := range attributes {
|
|
|
|
for _, attr := range attrs {
|
|
|
|
if attr == attribute {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-02 14:03:03 -05:00
|
|
|
// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
|
|
|
func hasValidURIScheme(src string) bool {
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist := []string{
|
2020-01-02 14:03:03 -05:00
|
|
|
"apt:",
|
|
|
|
"bitcoin:",
|
|
|
|
"callto:",
|
|
|
|
"dav:",
|
|
|
|
"davs:",
|
2017-11-20 00:10:04 -05:00
|
|
|
"ed2k://",
|
|
|
|
"facetime://",
|
2020-01-02 14:03:03 -05:00
|
|
|
"feed:",
|
2017-11-20 00:10:04 -05:00
|
|
|
"ftp://",
|
2020-01-02 14:03:03 -05:00
|
|
|
"geo:",
|
2017-11-20 00:10:04 -05:00
|
|
|
"gopher://",
|
|
|
|
"git://",
|
|
|
|
"http://",
|
|
|
|
"https://",
|
|
|
|
"irc://",
|
|
|
|
"irc6://",
|
|
|
|
"ircs://",
|
|
|
|
"itms://",
|
2020-01-02 14:03:03 -05:00
|
|
|
"itms-apps://",
|
|
|
|
"magnet:",
|
|
|
|
"mailto:",
|
|
|
|
"news:",
|
|
|
|
"nntp:",
|
2017-11-20 00:10:04 -05:00
|
|
|
"rtmp://",
|
2020-01-02 14:03:03 -05:00
|
|
|
"sip:",
|
|
|
|
"sips:",
|
|
|
|
"skype:",
|
|
|
|
"spotify:",
|
2017-11-20 00:10:04 -05:00
|
|
|
"ssh://",
|
|
|
|
"sftp://",
|
|
|
|
"steam://",
|
|
|
|
"svn://",
|
2020-01-02 14:03:03 -05:00
|
|
|
"svn+ssh://",
|
|
|
|
"tel:",
|
2017-11-20 00:10:04 -05:00
|
|
|
"webcal://",
|
2020-01-02 14:03:03 -05:00
|
|
|
"xmpp:",
|
2023-09-23 16:54:48 -04:00
|
|
|
|
|
|
|
// iOS Apps
|
|
|
|
"opener://", // https://www.opener.link
|
|
|
|
"hack://", // https://apps.apple.com/it/app/hack-for-hacker-news-reader/id1464477788?l=en-GB
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range whitelist {
|
|
|
|
if strings.HasPrefix(src, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:07:04 -04:00
|
|
|
func isBlockedResource(src string) bool {
|
2017-11-20 00:10:04 -05:00
|
|
|
blacklist := []string{
|
|
|
|
"feedsportal.com",
|
|
|
|
"api.flattr.com",
|
|
|
|
"stats.wordpress.com",
|
|
|
|
"plus.google.com/share",
|
|
|
|
"twitter.com/share",
|
|
|
|
"feeds.feedburner.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, element := range blacklist {
|
|
|
|
if strings.Contains(src, element) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:41:42 -04:00
|
|
|
func isValidIframeSource(baseURL, src string) bool {
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist := []string{
|
2018-07-05 01:45:44 -04:00
|
|
|
"//www.youtube.com",
|
2017-11-20 00:10:04 -05:00
|
|
|
"http://www.youtube.com",
|
|
|
|
"https://www.youtube.com",
|
2018-06-12 21:45:09 -04:00
|
|
|
"https://www.youtube-nocookie.com",
|
2017-11-20 00:10:04 -05:00
|
|
|
"http://player.vimeo.com",
|
|
|
|
"https://player.vimeo.com",
|
|
|
|
"http://www.dailymotion.com",
|
|
|
|
"https://www.dailymotion.com",
|
|
|
|
"http://vk.com",
|
|
|
|
"https://vk.com",
|
2018-04-26 16:46:40 -04:00
|
|
|
"http://soundcloud.com",
|
|
|
|
"https://soundcloud.com",
|
|
|
|
"http://w.soundcloud.com",
|
|
|
|
"https://w.soundcloud.com",
|
|
|
|
"http://bandcamp.com",
|
|
|
|
"https://bandcamp.com",
|
2018-07-10 23:56:54 -04:00
|
|
|
"https://cdn.embedly.com",
|
2021-02-20 07:11:13 -05:00
|
|
|
"https://player.bilibili.com",
|
2023-10-27 00:24:53 -04:00
|
|
|
"https://player.twitch.tv",
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:41:42 -04:00
|
|
|
// allow iframe from same origin
|
2023-08-13 22:09:01 -04:00
|
|
|
if urllib.Domain(baseURL) == urllib.Domain(src) {
|
2020-09-06 16:41:42 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:43:03 -05:00
|
|
|
// allow iframe from custom invidious instance
|
2023-08-13 22:09:01 -04:00
|
|
|
if config.Opts != nil && config.Opts.InvidiousInstance() == urllib.Domain(src) {
|
2022-01-05 23:43:03 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
for _, prefix := range whitelist {
|
|
|
|
if strings.HasPrefix(src, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:07:04 -04:00
|
|
|
func getTagAllowList() map[string][]string {
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist := make(map[string][]string)
|
2022-07-03 20:36:27 -04:00
|
|
|
whitelist["img"] = []string{"alt", "title", "src", "srcset", "sizes", "width", "height"}
|
2020-09-29 02:07:04 -04:00
|
|
|
whitelist["picture"] = []string{}
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist["audio"] = []string{"src"}
|
|
|
|
whitelist["video"] = []string{"poster", "height", "width", "src"}
|
2020-09-29 02:07:04 -04:00
|
|
|
whitelist["source"] = []string{"src", "type", "srcset", "sizes", "media"}
|
2022-09-12 01:32:16 -04:00
|
|
|
whitelist["dt"] = []string{"id"}
|
|
|
|
whitelist["dd"] = []string{"id"}
|
|
|
|
whitelist["dl"] = []string{"id"}
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist["table"] = []string{}
|
|
|
|
whitelist["caption"] = []string{}
|
|
|
|
whitelist["thead"] = []string{}
|
|
|
|
whitelist["tfooter"] = []string{}
|
|
|
|
whitelist["tr"] = []string{}
|
|
|
|
whitelist["td"] = []string{"rowspan", "colspan"}
|
|
|
|
whitelist["th"] = []string{"rowspan", "colspan"}
|
2022-09-12 01:32:16 -04:00
|
|
|
whitelist["h1"] = []string{"id"}
|
|
|
|
whitelist["h2"] = []string{"id"}
|
|
|
|
whitelist["h3"] = []string{"id"}
|
|
|
|
whitelist["h4"] = []string{"id"}
|
|
|
|
whitelist["h5"] = []string{"id"}
|
|
|
|
whitelist["h6"] = []string{"id"}
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist["strong"] = []string{}
|
|
|
|
whitelist["em"] = []string{}
|
|
|
|
whitelist["code"] = []string{}
|
|
|
|
whitelist["pre"] = []string{}
|
|
|
|
whitelist["blockquote"] = []string{}
|
|
|
|
whitelist["q"] = []string{"cite"}
|
|
|
|
whitelist["p"] = []string{}
|
2022-09-12 01:32:16 -04:00
|
|
|
whitelist["ul"] = []string{"id"}
|
|
|
|
whitelist["li"] = []string{"id"}
|
|
|
|
whitelist["ol"] = []string{"id"}
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist["br"] = []string{}
|
|
|
|
whitelist["del"] = []string{}
|
2022-09-12 01:32:16 -04:00
|
|
|
whitelist["a"] = []string{"href", "title", "id"}
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist["figure"] = []string{}
|
|
|
|
whitelist["figcaption"] = []string{}
|
|
|
|
whitelist["cite"] = []string{}
|
|
|
|
whitelist["time"] = []string{"datetime"}
|
|
|
|
whitelist["abbr"] = []string{"title"}
|
|
|
|
whitelist["acronym"] = []string{"title"}
|
|
|
|
whitelist["wbr"] = []string{}
|
|
|
|
whitelist["dfn"] = []string{}
|
|
|
|
whitelist["sub"] = []string{}
|
2023-01-31 08:35:43 -05:00
|
|
|
whitelist["sup"] = []string{"id"}
|
2017-11-20 00:10:04 -05:00
|
|
|
whitelist["var"] = []string{}
|
|
|
|
whitelist["samp"] = []string{}
|
|
|
|
whitelist["s"] = []string{}
|
|
|
|
whitelist["del"] = []string{}
|
|
|
|
whitelist["ins"] = []string{}
|
|
|
|
whitelist["kbd"] = []string{}
|
|
|
|
whitelist["rp"] = []string{}
|
|
|
|
whitelist["rt"] = []string{}
|
|
|
|
whitelist["rtc"] = []string{}
|
|
|
|
whitelist["ruby"] = []string{}
|
|
|
|
whitelist["iframe"] = []string{"width", "height", "frameborder", "src", "allowfullscreen"}
|
|
|
|
return whitelist
|
|
|
|
}
|
|
|
|
|
|
|
|
func inList(needle string, haystack []string) bool {
|
|
|
|
for _, element := range haystack {
|
|
|
|
if element == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2018-06-12 21:45:09 -04:00
|
|
|
|
|
|
|
func rewriteIframeURL(link string) string {
|
|
|
|
matches := youtubeEmbedRegex.FindStringSubmatch(link)
|
|
|
|
if len(matches) == 2 {
|
2023-07-05 11:11:56 -04:00
|
|
|
return config.Opts.YouTubeEmbedUrlOverride() + matches[1]
|
2018-06-12 21:45:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return link
|
|
|
|
}
|
2018-06-23 20:50:43 -04:00
|
|
|
|
2020-09-29 02:07:04 -04:00
|
|
|
func isBlockedTag(tagName string) bool {
|
2018-06-24 13:16:07 -04:00
|
|
|
blacklist := []string{
|
|
|
|
"noscript",
|
|
|
|
"script",
|
|
|
|
"style",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, element := range blacklist {
|
|
|
|
if element == tagName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2018-06-23 20:50:43 -04:00
|
|
|
}
|
2020-09-29 02:07:04 -04:00
|
|
|
|
|
|
|
func sanitizeSrcsetAttr(baseURL, value string) string {
|
2022-07-04 15:48:48 -04:00
|
|
|
imageCandidates := ParseSrcSetAttribute(value)
|
2020-09-29 02:07:04 -04:00
|
|
|
|
2022-07-04 15:48:48 -04:00
|
|
|
for _, imageCandidate := range imageCandidates {
|
2023-08-13 22:09:01 -04:00
|
|
|
absoluteURL, err := urllib.AbsoluteURL(baseURL, imageCandidate.ImageURL)
|
2022-07-04 15:48:48 -04:00
|
|
|
if err == nil {
|
|
|
|
imageCandidate.ImageURL = absoluteURL
|
2020-09-29 02:07:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 15:48:48 -04:00
|
|
|
return imageCandidates.String()
|
2020-09-29 02:07:04 -04:00
|
|
|
}
|
2021-02-06 17:33:28 -05:00
|
|
|
|
|
|
|
func isValidDataAttribute(value string) bool {
|
|
|
|
var dataAttributeAllowList = []string{
|
|
|
|
"data:image/avif",
|
|
|
|
"data:image/apng",
|
|
|
|
"data:image/png",
|
|
|
|
"data:image/svg",
|
|
|
|
"data:image/svg+xml",
|
|
|
|
"data:image/jpg",
|
|
|
|
"data:image/jpeg",
|
|
|
|
"data:image/gif",
|
|
|
|
"data:image/webp",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range dataAttributeAllowList {
|
|
|
|
if strings.HasPrefix(value, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-07-03 20:36:27 -04:00
|
|
|
|
2022-09-12 01:32:16 -04:00
|
|
|
func isAnchor(tagName string, attribute html.Attribute) bool {
|
|
|
|
return tagName == "a" && attribute.Key == "href" && strings.HasPrefix(attribute.Val, "#")
|
|
|
|
}
|
|
|
|
|
2022-07-03 20:36:27 -04:00
|
|
|
func isPositiveInteger(value string) bool {
|
|
|
|
if number, err := strconv.Atoi(value); err == nil {
|
|
|
|
return number > 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-07-04 22:59:52 -04:00
|
|
|
|
|
|
|
func getAttributeValue(name string, attributes []html.Attribute) string {
|
|
|
|
for _, attribute := range attributes {
|
|
|
|
if attribute.Key == name {
|
|
|
|
return attribute.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIntegerAttributeValue(name string, attributes []html.Attribute) int {
|
|
|
|
number, _ := strconv.Atoi(getAttributeValue(name, attributes))
|
|
|
|
return number
|
|
|
|
}
|