8000 [pull] main from miniflux:main by pull[bot] · Pull Request #292 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 2 additions & 59 deletions internal/reader/sanitizer/sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,54 +148,6 @@ var (
"x.com/share",
}

// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
validURISchemes = []string{
// Most commong schemes on top.
"https:",
"http:",

// Then the rest.
"apt:",
"bitcoin:",
"callto:",
"dav:",
"davs:",
"ed2k:",
"facetime:",
"feed:",
"ftp:",
"geo:",
"git:",
"gopher:",
"irc:",
"irc6:",
"ircs:",
"itms-apps:",
"itms:",
"magnet:",
"mailto:",
"news:",
"nntp:",
"rtmp:",
"sftp:",
"sip:",
"sips:",
"shortcuts:",
"skype:",
"spotify:",
"ssh:",
"steam:",
"svn:",
"svn+ssh:",
"tel:",
"webcal:",
"xmpp:",

// iOS Apps
"opener:", // https://www.opener.link
"hack:", // https://apps.apple.com/it/app/hack-for-hacker-news-reader/id1464477788?l=en-GB
}

dataAttributeAllowedPrefixes = []string{
"data:image/avif",
"data:image/apng",
Expand Down Expand Up @@ -351,15 +303,6 @@ func hasRequiredAttributes(s *mandatoryAttributesStruct, tagName string) bool {
return true
}

func hasValidURIScheme(absoluteURL string) bool {
for _, scheme := range validURISchemes {
if strings.HasPrefix(absoluteURL, scheme) {
return true
}
}
return false
}

func isBlockedResource(absoluteURL string) bool {
for _, blockedURL := range blockedResourceURLSubstrings {
if strings.Contains(absoluteURL, blockedURL) {
Expand Down Expand Up @@ -588,7 +531,7 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, tagName string, attributes []htm
continue
}

if !hasValidURIScheme(value) {
if !HasValidURIScheme(value) {
continue
}

Expand Down Expand Up @@ -652,7 +595,7 @@ func sanitizeSrcsetAttr(parsedBaseURL *url.URL, value string) string {
continue
}

if !hasValidURIScheme(absoluteURL) || isBlockedResource(absoluteURL) {
if !HasValidURIScheme(absoluteURL) || isBlockedResource(absoluteURL) {
continue
}

Expand Down
73 changes: 73 additions & 0 deletions internal/reader/sanitizer/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer"

import "strings"

// validURISchemes is the allowlist for URLs in sanitized feed body content.
// It is intentionally broad; stricter surfaces (redirects, template hrefs)
// should use urllib.IsAbsoluteURL / urllib.IsRelativePath instead.
//
// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
var validURISchemes = []string{
// Most commong schemes on top.
"https",
"http",

// Then the rest.
"apt",
"bitcoin",
"callto",
"dav",
"davs",
"ed2k",
"facetime",
"feed",
"ftp",
"geo",
"git",
"gopher",
"irc",
"irc6",
"ircs",
"itms-apps",
"itms",
"magnet",
"mailto",
"news",
"nntp",
"rtmp",
"sftp",
"sip",
"sips",
"shortcuts",
"skype",
"spotify",
"ssh",
"steam",
"svn",
"svn+ssh",
"tel",
"webcal",
"xmpp",

// iOS Apps
"opener", // https://www.opener.link
"hack", // https://apps.apple.com/it/app/hack-for-hacker-news-reader/id1464477788?l=en-GB
}

// HasValidURIScheme reports whether the URL begins with an allowed scheme.
// The scheme comparison is case-insensitive per RFC 3986 §3.1.
func HasValidURIScheme(absoluteURL string) bool {
scheme, _, ok := strings.Cut(absoluteURL, ":")
if !ok || scheme == "" {
return false
}
for _, validScheme := range validURISchemes {
if strings.EqualFold(scheme, validScheme) {
return true
}
}
return false
}
51 changes: 51 additions & 0 deletions internal/reader/sanitizer/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer"

import "testing"

func TestHasValidURIScheme(t *testing.T) {
scenarios := map[string]bool{
// Allowed: web schemes.
"http://example.org/article": true,
"https://example.org/article": true,

// Allowed: a sample of the broader feed-content schemes.
"mailto:author@example.org": true,
"magnet:?xt=urn:btih:abc": true,
"tel:+15551234567": true,
"ftp://example.org/file": true,
"feed:https://example.org/": true,
"webcal://example.org/cal": true,

// Rejected: schemes that enable script execution or local resource access.
"javascript:alert(1)": false,
"data:text/html,<script>alert(1)</script>": false,
"vbscript:msgbox(1)": false,
"file:///etc/passwd": false,

// Rejected: missing or malformed scheme.
"": false,
"example.org": false,
"/relative/path": false,
"//evil.example.org/path": false,

// Allowed: scheme matching is case-insensitive (RFC 3986 §3.1).
"HTTPS://example.org": true,
"MailTo:author@host": true,
"SVN+SSH://example.org": true,

// Rejected: case-insensitive match still rejects disallowed schemes.
"JavaScript:alert(1)": false,
"VBScript:msgbox(1)": false,
}

for input, expected := range scenarios {
t.Run(input, func(t *testing.T) {
if actual := HasValidURIScheme(input); actual != expected {
t.Errorf("HasValidURIScheme(%q) = %v, want %v", input, actual, expected)
}
})
}
}
19 changes: 16 additions & 3 deletions internal/template/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"miniflux.app/v2/internal/locale"
"miniflux.app/v2/internal/mediaproxy"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/reader/sanitizer"
"miniflux.app/v2/internal/timezone"
"miniflux.app/v2/internal/ui/static"
"miniflux.app/v2/internal/urllib"
Expand Down Expand Up @@ -56,9 +57,7 @@ func (f *funcMap) Map() template.FuncMap {
}
return f.basePath + format
},
"safeURL": func(url string) template.URL {
return template.URL(url)
},
"untrustedURL": untrustedURL,
"safeCSS": func(str string) template.CSS {
return template.CSS(str)
},
Expand Down Expand Up @@ -242,6 +241,20 @@ func isEmail(str string) bool {
return err == nil
}

// untrustedURL validates a feed-supplied URL against the sanitizer's scheme
// allowlist before exposing it to html/template. Returns "#" for unsafe URLs
// (e.g. javascript:, data:) so anchors render as inert links.
//
// Go's built-in html/template URL filter only allows http(s), mailto, and
// relative URLs — too narrow for feeds which legitimately use schemes like
// magnet:, feed:, webcal:, and tel:.
func untrustedURL(rawURL string) template.URL {
if !sanitizer.HasValidURIScheme(rawURL) {
return template.URL("#")
}
return template.URL(rawURL)
}

// Returns the duration in human readable format (hours and minutes).
func duration(t time.Time) string {
return durationImpl(t, time.Now())
Expand Down
31 changes: 31 additions & 0 deletions internal/template/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package template // import "miniflux.app/v2/internal/template"

import (
"html/template"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -126,6 +127,36 @@ func TestIsEmail(t *testing.T) {
}
}

func TestUntrustedURL(t *testing.T) {
scenarios := map[string]template.URL{
// Pass-through: schemes accepted by the sanitizer allowlist.
"https://example.org/article": "https://example.org/article",
"http://example.org/article": "http://example.org/article",
"mailto:author@example.org": "mailto:author@example.org",
"magnet:?xt=urn:btih:abc": "magnet:?xt=urn:btih:abc",
"feed:https://example.org/": "feed:https://example.org/",

// Rewritten to "#": schemes that enable script execution or
// local-resource access, plus malformed inputs.
"javascript:alert(1)": "#",
"JavaScript:alert(1)": "#",
"data:text/html,<script>alert(1)</script>": "#",
"vbscript:msgbox(1)": "#",
"file:///etc/passwd": "#",
"//evil.example.org/path": "#",
"/relative/path": "#",
"": "#",
}

for input, expected := range scenarios {
t.Run(input, func(t *testing.T) {
if actual := untrustedURL(input); actual != expected {
t.Errorf("untrustedURL(%q) = %q, want %q", input, actual, expected)
}
})
}
}

func TestDuration(t *testing.T) {
now := time.Now()
var dt = []struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/template/templates/common/feed_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h2 id="feed-title-{{ .ID }}" class="item-title">
<div class="item-meta">
<ul class="item-meta-info">
<li class="item-meta-info-site-url" dir="auto">
<a href="{{ .SiteURL | safeURL }}" title="{{ .SiteURL }}" {{ if $.user.OpenExternalLinksInNewTab }}target="_blank"{{ else }}rel="noopener"{{ end }} data-original-link="{{ $.user.MarkReadOnView }}">
<a href="{{ .SiteURL }}" title="{{ .SiteURL }}" {{ if $.user.OpenExternalLinksInNewTab }}target="_blank"{{ else }}rel="noopener"{{ end }} data-original-link="{{ $.user.MarkReadOnView }}">
{{ domain .SiteURL }}
</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions internal/template/templates/common/item_meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@
</li>
{{ end -}}
<li class="item-meta-icons-external-url">
<a href="{{ .entry.URL | safeURL }}"
<a href="{{ .entry.URL | untrustedURL }}"
aria-describedby="entry-title-{{ .entry.ID }}"
{{ if $.user.OpenExternalLinksInNewTab }}target="_blank"{{ else }}rel="noopener"{{ end }}
data-original-link="{{ .user.MarkReadOnView }}">{{ icon "external-link" }}<span class="icon-label">{{ t "entry.external_link.label" }}</span></a>
</li>
{{ if .entry.CommentsURL }}
<li class="item-meta-icons-comments">
<a href="{{ .entry.CommentsURL | safeURL }}"
<a href="{{ .entry.CommentsURL }}"
aria-describedby="entry-title-{{ .entry.ID }}"
title="{{ t "entry.comments.title" }}"
{{ if $.user.OpenExternalLinksInNewTab }}target="_blank"{{ else }}rel="noopener"{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions internal/template/templates/views/choose_subscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ <h3>{{ t "page.add_feed.choose_feed" }}</h3>

{{ range .subscriptions }}
<div class="radio-group">
<label title="{{ .URL | safeURL }}"><input type="radio" name="url" value="{{ .URL | safeURL }}"> {{ .Title }}</label> ({{ .Type }})
<small title="Type = {{ .Type }}"><a href="{{ .URL | safeURL }}" {{ if $.user.OpenExternalLinksInNewTab }}target="_blank"{{ else }}rel="noopener"{{ end }}>{{ .URL | safeURL }}</a></small>
<label title="{{ .URL }}"><input type="radio" name="url" value="{{ .URL }}"> {{ .Title }}</label> ({{ .Type }})
<small title="Type = {{ .Type }}"><a href="{{ .URL }}" {{ if $.user.OpenExternalLinksInNewTab }}target="_blank"{{ else }}rel="noopener"{{ end }}>{{ .URL }}</a></small>
</div>
{{ end }}

Expand Down
Loading
Loading