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