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

Skip to content
Merged < 8000 /div>
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
18 changes: 18 additions & 0 deletions internal/config/options.go
7438
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ func NewConfigOptions() *configOptions {
rawValue: "0",
valueType: boolType,
},
"ICON_FETCH_ALLOW_PRIVATE_NETWORKS": {
parsedBoolValue: false,
rawValue: "0",
valueType: boolType,
},
"INVIDIOUS_INSTANCE": {
parsedStringValue: "yewtu.be",
rawValue: "yewtu.be",
Expand Down Expand Up @@ -355,6 +360,11 @@ func NewConfigOptions() *configOptions {
rawValue: "",
valueType: urlType,
},
"MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS": {
parsedBoolValue: false,
rawValue: "0",
valueType: boolType,
},
"MEDIA_PROXY_HTTP_CLIENT_TIMEOUT": {
parsedDuration: 120 * time.Second,
rawValue: "120",
Expand Down Expand Up @@ -778,6 +788,10 @@ func (c *configOptions) HTTPS() bool {
return c.options["HTTPS"].parsedBoolValue
}

func (c *configOptions) IconFetchAllowPrivateNetworks() bool {
return c.options["ICON_FETCH_ALLOW_PRIVATE_NETWORKS"].parsedBoolValue
}

func (c *configOptions) InvidiousInstance() string {
return c.options["INVIDIOUS_INSTANCE"].parsedStringValue
}
Expand Down Expand Up @@ -830,6 +844,10 @@ func (c *configOptions) MediaCustomProxyURL() *url.URL {
return c.options["MEDIA_PROXY_CUSTOM_URL"].parsedURLValue
}

func (c *configOptions) MediaProxyAllowPrivateNetworks() bool {
return c.options["MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS"].parsedBoolValue
}

func (c *configOptions) MediaProxyHTTPClientTimeout() time.Duration {
return c.options["MEDIA_PROXY_HTTP_CLIENT_TIMEOUT"].parsedDuration
}
Expand Down
48 changes: 48 additions & 0 deletions internal/config/options_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,30 @@ func TestHTTPClientTimeoutOptionParsing(t *testing.T) {
}
}

func TestIconFetchAllowPrivateNetworksOptionParsing(t *testing.T) {
configParser := NewConfigParser()

if configParser.options.IconFetchAllowPrivateNetworks() {
t.Fatalf("Expected ICON_FETCH_ALLOW_PRIVATE_NETWORKS to be disabled by default")
}

if err := configParser.parseLines([]string{"ICON_FETCH_ALLOW_PRIVATE_NETWORKS=1"}); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if !configParser.options.IconFetchAllowPrivateNetworks() {
t.Fatalf("Expected ICON_FETCH_ALLOW_PRIVATE_NETWORKS to be enabled")
}

if err := configParser.parseLines([]string{"ICON_FETCH_ALLOW_PRIVATE_NETWORKS=0"}); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if configParser.options.IconFetchAllowPrivateNetworks() {
t.Fatalf("Expected ICON_FETCH_ALLOW_PRIVATE_NETWORKS to be disabled")
}
}

func TestHTTPServerTimeoutOptionParsing(t *testing.T) {
configParser := NewConfigParser()

Expand Down Expand Up @@ -1431,6 +1455,30 @@ func TestMediaProxyHTTPClientTimeoutOptionParsing(t *testing.T) {
}
}

func TestMediaProxyAllowPrivateNetworksOptionParsing(t *testing.T) {
configParser := NewConfigParser()

if configParser.options.MediaProxyAllowPrivateNetworks() {
t.Fatalf("Expected MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS to be disabled by default")
}

if err := configParser.parseLines([]string{"MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS=1"}); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if !configParser.options.MediaProxyAllowPrivateNetworks() {
t.Fatalf("Expected MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS to be enabled")
}

if err := configParser.parseLines([]string{"MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS=0"}); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if configParser.options.MediaProxyAllowPrivateNetworks() {
t.Fatalf("Expected MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS to be disabled")
}
}

func TestMediaProxyPrivateKeyOptionParsing(t *testing.T) {
configParser := NewConfigParser()

Expand Down
40 changes: 40 additions & 0 deletions internal/reader/icon/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (f *iconFinder) downloadIcon(iconURL string) (*model.Icon, error) {
slog.String("icon_url", iconURL),
)

if err := ensureRemoteIconURLAllowed(iconURL, config.Opts.IconFetchAllowPrivateNetworks()); err != nil {
return nil, err
}

responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(iconURL))
defer responseHandler.Close()

Expand Down Expand Up @@ -330,3 +334,39 @@ func parseImageDataURL(value string) (*model.Icon, error) {
MimeType: mediaType,
}, nil
}

func ensureRemoteIconURLAllowed(iconURL string, allowPrivateNetworks bool) error {
parsedURL, err := url.Parse(iconURL)
if err != nil {
return fmt.Errorf("icon: invalid icon URL %q: %w", iconURL, err)
}

if !parsedURL.IsAbs() {
return fmt.Errorf("icon: icon URL %q must be absolute", iconURL)
}

scheme := strings.ToLower(parsedURL.Scheme)
if scheme != "http" && scheme != "https" {
return fmt.Errorf("icon: unsupported icon URL scheme %q", parsedURL.Scheme)
}

hostname := parsedURL.Hostname()
if hostname == "" {
return fmt.Errorf("icon: icon URL %q has no hostname", iconURL)
}

if allowPrivateNetworks {
return nil
}

isPrivate, err := urllib.ResolvesToPrivateIP(hostname)
if err != nil {
return fmt.Errorf("icon: unable to resolve icon hostname %q: %w", hostname, err)
}

if isPrivate {
return fmt.Errorf("icon: refusing to download icon from private network host %q", hostname)
}

return nil
}
18 changes: 18 additions & 0 deletions internal/reader/icon/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,24 @@ func TestResizeIconWebp(t *testing.T) {
}
}

func TestEnsureRemoteIconURLAllowedRejectsPrivateNetworks(t *testing.T) {
if err := ensureRemoteIconURLAllowed("http://192.168.0.1/favicon.ico", false); err == nil {
t.Fatal("Expected private network hosts to be rejected")
}
}

func TestEnsureRemoteIconURLAllowedAllowsPublicNetworks(t *testing.T) {
if err := ensureRemoteIconURLAllowed("https://1.1.1.1/favicon.ico", false); err != nil {
t.Fatalf("Expected public network hosts to be allowed: %v", err)
}
}

func TestEnsureRemoteIconURLAllowedAllowsPrivateWhenEnabled(t *testing.T) {
if err := ensureRemoteIconURLAllowed("http://10.0.0.5/icon.png", true); err != nil {
t.Fatalf("Expected private network hosts to be allowed when explicitly enabled: %v", err)
}
}

func TestResizeInvalidImage(t *testing.T) {
icon := model.Icon{
Content: []byte("invalid data"),
Expand Down
19 changes: 19 additions & 0 deletions internal/ui/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"miniflux.app/v2/internal/http/response/html"
"miniflux.app/v2/internal/reader/fetcher"
"miniflux.app/v2/internal/reader/rewrite"
"miniflux.app/v2/internal/urllib"
)

func (h *handler) mediaProxy(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -81,6 +82,24 @@ func (h *handler) mediaProxy(w http.ResponseWriter, r *http.Request) {
}

mediaURL := string(decodedURL)

if !config.Opts.MediaProxyAllowPrivateNetworks() {
if isPrivate, err := urllib.ResolvesToPrivateIP(parsedMediaURL.Hostname()); err != nil {
slog.Warn("MediaProxy: Unable to resolve hostname",
slog.String("media_url", mediaURL),
slog.Any("error", err),
)
html.Forbidden(w, r)
return
} else if isPrivate {
slog.Warn("MediaProxy: Refusing to access private IP address",
slog.String("media_url", mediaURL),
)
html.Forbidden(w, r)
return
}
}

slog.Debug("MediaProxy: Fetching remote resource",
slog.String("media_url", mediaURL),
)
Expand Down
32 changes: 32 additions & 0 deletions internal/urllib/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package urllib // import "miniflux.app/v2/internal/urllib"
import (
"errors"
"fmt"
"net"
"net/url"
"slices"
"strings"
)

Expand Down Expand Up @@ -138,3 +140,33 @@ func JoinBaseURLAndPath(baseURL, path string) (string, error) {

return finalURL, nil
}

// ResolvesToPrivateIP resolves a hostname and returns true if
// ANY resolved IP address is non-public.
func ResolvesToPrivateIP(host string) (bool, error) {
ips, err := net.LookupIP(host)
if err != nil {
return false, err
}

if slices.ContainsFunc(ips, isNonPublicIP) {
return true, nil
}

return false, nil
}

// isNonPublicIP returns true if the given IP is private, loopback,
// link-local, multicast, or unspecified.
func isNonPublicIP(ip net.IP) bool {
if ip == nil {
return true
}

return ip.IsPrivate() ||
ip.IsLoopback() ||
ip.IsLinkLocalUnicast() ||
ip.IsLinkLocalMulticast() ||
ip.IsMulticast() ||
ip.IsUnspecified()
}
74 changes: 73 additions & 1 deletion internal/urllib/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package urllib // import "miniflux.app/v2/internal/urllib"

import "testing"
import (
"net"
"testing"
)

func TestIsRelativePath(t *testing.T) {
scenarios := map[string]bool{
Expand Down Expand Up @@ -154,3 +157,72 @@ func TestJoinBaseURLAndPath(t *testing.T) {
})
}
}

func TestIsNonPublicIP(t *testing.T) {
testCases := []struct {
name string
ipString string
want bool
}{
{"nil", "", true},
{"private IPv4", "192.168.1.10", true},
{"loopback IPv4", "127.0.0.1", true},
{"link-local IPv4", "169.254.42.1", true},
{"multicast IPv4", "224.0.0.1", true},
{"unspecified IPv6", "::", true},
{"loopback IPv6", "::1", true},
{"multicast IPv6", "ff02::1", true},
{"public IPv4", "93.184.216.34", false},
{"public IPv6", "2001:4860:4860::8888", false},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var ip net.IP
if tc.ipString != "" {
ip = net.ParseIP(tc.ipString)
if ip == nil {
t.Fatalf("unable to parse %q", tc.ipString)
}
}

if got := isNonPublicIP(ip); got != tc.want {
t.Fatalf("unexpected result for %s: got %v want %v", tc.name, got, tc.want)
}
})
}
}

func TestResolvesToPrivateIP(t *testing.T) {
testCases := []struct {
name string
host string
want bool
}{
{"localhost", "localhost", true},
{"example.org", "example.org", false},
{"loopback IPv4 literal", "127.0.0.1", true},
{"loopback IPv6 literal", "::1", true},
{"private IPv4 literal", "192.168.1.1", true},
{"public IPv4 literal", "93.184.216.34", false},
{"public IPv6 literal", "2001:4860:4860::8888", false},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := ResolvesToPrivateIP(tc.host)
if err != nil {
t.Fatalf("unexpected error for %s: %v", tc.host, err)
}
if got != tc.want {
t.Fatalf("unexpected result for %s: got %v want %v", tc.name, got, tc.want)
}
})
}
}

func TestResolvesToPrivateIPError(t *testing.T) {
if _, err := ResolvesToPrivateIP(""); err == nil {
t.Fatalf("expected an error for empty host")
}
}
12 changes: 11 additions & 1 deletion miniflux.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" Manpage for miniflux.
.TH "MINIFLUX" "1" "September 29, 2025" "\ \&" "\ \&"
.TH "MINIFLUX" "1" "December 29, 2025" "\ \&" "\ \&"

.SH NAME
miniflux \- Minimalist and opinionated feed reader
Expand Down Expand Up @@ -336,6 +336,11 @@ Forces cookies to use secure flag and send HSTS header\&.
.br
Default is disabled\&.
.TP
.B ICON_FETCH_ALLOW_PRIVATE_NETWORKS
Set to 1 to allow downloading favicons that resolve to private or loopback networks\&.
.br
Disabled by default, private networks are refused\&.
.TP
.B INVIDIOUS_INSTANCE
Set a custom invidious instance to use\&.
.br
Expand Down Expand Up @@ -393,6 +398,11 @@ Time limit in seconds before the media proxy HTTP client cancel the request\&.
.br
Default is 120 seconds\&.
.TP
.B MEDIA_PROXY_ALLOW_PRIVATE_NETWORKS
Set to 1 to allow proxying media that resolves to private or loopback networks\&.
.br
Disabled by default, private networks are refused\&.
.TP
.B MEDIA_PROXY_RESOURCE_TYPES
A comma-separated list of media types to proxify. Supported values are: image, audio, video\&.
.br
Expand Down
Loading
0