From bf632fad2e19e9ece4db5957f05727f373541917 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 4 Jan 2020 15:18:24 -0800 Subject: Allow only absolute URLs in comments URL Some feeds are using invalid URLs (random text). --- url/url.go | 9 +++++++++ url/url_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+) (limited to 'url') diff --git a/url/url.go b/url/url.go index b02348f..d0c627e 100644 --- a/url/url.go +++ b/url/url.go @@ -11,6 +11,15 @@ import ( "strings" ) +// IsAbsoluteURL returns true if the link is absolute. +func IsAbsoluteURL(link string) bool { + u, err := url.Parse(link) + if err != nil { + return false + } + return u.IsAbs() +} + // AbsoluteURL converts the input URL as absolute URL if necessary. func AbsoluteURL(baseURL, input string) (string, error) { if strings.HasPrefix(input, "//") { diff --git a/url/url_test.go b/url/url_test.go index 56b6e13..ea488cf 100644 --- a/url/url_test.go +++ b/url/url_test.go @@ -6,6 +6,21 @@ package url // import "miniflux.app/url" import "testing" +func TestIsAbsoluteURL(t *testing.T) { + scenarios := map[string]bool{ + "https://example.org/file.pdf": true, + "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": true, + "invalid url": false, + } + + for input, expected := range scenarios { + actual := IsAbsoluteURL(input) + if actual != expected { + t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input) + } + } +} + func TestAbsoluteURL(t *testing.T) { scenarios := [][]string{ []string{"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"}, -- cgit v1.2.3