mirror of https://github.com/miniflux/v2.git
Rewrite iframe Youtube URLs to https://www.youtube-nocookie.com
This commit is contained in:
parent
36dab8b518
commit
c719cf7df0
|
@ -8,6 +8,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/miniflux/miniflux/url"
|
"github.com/miniflux/miniflux/url"
|
||||||
|
@ -15,6 +16,10 @@ import (
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
youtubeEmbedRegex = regexp.MustCompile(`http[s]?://www\.youtube\.com/embed/(.*)`)
|
||||||
|
)
|
||||||
|
|
||||||
// Sanitize returns safe HTML.
|
// Sanitize returns safe HTML.
|
||||||
func Sanitize(baseURL, input string) string {
|
func Sanitize(baseURL, input string) string {
|
||||||
tokenizer := html.NewTokenizer(bytes.NewBufferString(input))
|
tokenizer := html.NewTokenizer(bytes.NewBufferString(input))
|
||||||
|
@ -85,8 +90,12 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([
|
||||||
}
|
}
|
||||||
|
|
||||||
if isExternalResourceAttribute(attribute.Key) {
|
if isExternalResourceAttribute(attribute.Key) {
|
||||||
if tagName == "iframe" && !isValidIframeSource(attribute.Val) {
|
if tagName == "iframe" {
|
||||||
continue
|
if isValidIframeSource(attribute.Val) {
|
||||||
|
value = rewriteIframeURL(attribute.Val)
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
value, err = url.AbsoluteURL(baseURL, value)
|
value, err = url.AbsoluteURL(baseURL, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -274,6 +283,7 @@ func isValidIframeSource(src string) bool {
|
||||||
whitelist := []string{
|
whitelist := []string{
|
||||||
"http://www.youtube.com",
|
"http://www.youtube.com",
|
||||||
"https://www.youtube.com",
|
"https://www.youtube.com",
|
||||||
|
"https://www.youtube-nocookie.com",
|
||||||
"http://player.vimeo.com",
|
"http://player.vimeo.com",
|
||||||
"https://player.vimeo.com",
|
"https://player.vimeo.com",
|
||||||
"http://www.dailymotion.com",
|
"http://www.dailymotion.com",
|
||||||
|
@ -365,3 +375,12 @@ func inList(needle string, haystack []string) bool {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rewriteIframeURL(link string) string {
|
||||||
|
matches := youtubeEmbedRegex.FindStringSubmatch(link)
|
||||||
|
if len(matches) == 2 {
|
||||||
|
return `https://www.youtube-nocookie.com/embed/` + matches[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return link
|
||||||
|
}
|
||||||
|
|
|
@ -162,3 +162,53 @@ func TestEspaceAttributes(t *testing.T) {
|
||||||
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReplaceYoutubeURL(t *testing.T) {
|
||||||
|
input := `<iframe src="http://www.youtube.com/embed/test123?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent"></iframe>`
|
||||||
|
expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent"></iframe>`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if expected != output {
|
||||||
|
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplaceSecureYoutubeURL(t *testing.T) {
|
||||||
|
input := `<iframe src="https://www.youtube.com/embed/test123"></iframe>`
|
||||||
|
expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123"></iframe>`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if expected != output {
|
||||||
|
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplaceSecureYoutubeURLWithParameters(t *testing.T) {
|
||||||
|
input := `<iframe src="https://www.youtube.com/embed/test123?rel=0&controls=0"></iframe>`
|
||||||
|
expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&controls=0"></iframe>`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if expected != output {
|
||||||
|
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplaceYoutubeURLAlreadyReplaced(t *testing.T) {
|
||||||
|
input := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&controls=0"></iframe>`
|
||||||
|
expected := `<iframe src="https://www.youtube-nocookie.com/embed/test123?rel=0&controls=0"></iframe>`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if expected != output {
|
||||||
|
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplaceIframeURL(t *testing.T) {
|
||||||
|
input := `<iframe src="https://player.vimeo.com/video/123456?title=0&byline=0"></iframe>`
|
||||||
|
expected := `<iframe src="https://player.vimeo.com/video/123456?title=0&byline=0"></iframe>`
|
||||||
|
output := Sanitize("http://example.org/", input)
|
||||||
|
|
||||||
|
if expected != output {
|
||||||
|
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue