瀏覽代碼

fix(mediaproxy): ignore unsupported proxy targets

Frédéric Guillot 1 月之前
父節點
當前提交
30c9ba9f18
共有 2 個文件被更改,包括 40 次插入3 次删除
  1. 32 0
      internal/mediaproxy/media_proxy_test.go
  2. 8 3
      internal/mediaproxy/rewriter.go

+ 32 - 0
internal/mediaproxy/media_proxy_test.go

@@ -646,6 +646,38 @@ func TestShouldProxifyURLWithMimeType(t *testing.T) {
 			mediaProxyResourceTypes: []string{},
 			expected:                false,
 		},
+		{
+			name:                    "Relative URL should not be proxified",
+			mediaURL:                "/image.jpg",
+			mediaMimeType:           "image/jpeg",
+			mediaProxyOption:        "all",
+			mediaProxyResourceTypes: []string{"image"},
+			expected:                false,
+		},
+		{
+			name:                    "Protocol-relative URL should not be proxified",
+			mediaURL:                "//cdn.example.com/image.jpg",
+			mediaMimeType:           "image/jpeg",
+			mediaProxyOption:        "all",
+			mediaProxyResourceTypes: []string{"image"},
+			expected:                false,
+		},
+		{
+			name:                    "Unsupported scheme should not be proxified",
+			mediaURL:                "ftp://example.com/image.jpg",
+			mediaMimeType:           "image/jpeg",
+			mediaProxyOption:        "all",
+			mediaProxyResourceTypes: []string{"image"},
+			expected:                false,
+		},
+		{
+			name:                    "Blob URL should not be proxified",
+			mediaURL:                "blob:https://example.com/123",
+			mediaMimeType:           "image/jpeg",
+			mediaProxyOption:        "all",
+			mediaProxyResourceTypes: []string{"image"},
+			expected:                false,
+		},
 		{
 			name:                    "URL with partial MIME type match should be proxified",
 			mediaURL:                "http://example.com/image.jpg",

+ 8 - 3
internal/mediaproxy/rewriter.go

@@ -4,12 +4,12 @@
 package mediaproxy // import "miniflux.app/v2/internal/mediaproxy"
 
 import (
+	"net/url"
 	"slices"
 	"strings"
 
 	"miniflux.app/v2/internal/config"
 	"miniflux.app/v2/internal/reader/sanitizer"
-	"miniflux.app/v2/internal/urllib"
 
 	"github.com/PuerkitoBio/goquery"
 	"github.com/gorilla/mux"
@@ -109,14 +109,19 @@ func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFun
 
 // shouldProxifyURL checks if the media URL should be proxified based on the media proxy option and URL scheme.
 func shouldProxifyURL(mediaURL, mediaProxyOption string) bool {
+	parsedURL, err := url.Parse(mediaURL)
+	if err != nil || !parsedURL.IsAbs() || parsedURL.Host == "" {
+		return false
+	}
+
 	switch {
 	case mediaURL == "":
 		return false
 	case strings.HasPrefix(mediaURL, "data:"):
 		return false
-	case mediaProxyOption == "all":
+	case mediaProxyOption == "all" && (strings.EqualFold(parsedURL.Scheme, "http") || strings.EqualFold(parsedURL.Scheme, "https")):
 		return true
-	case mediaProxyOption != "none" && !urllib.IsHTTPS(mediaURL):
+	case mediaProxyOption != "none" && strings.EqualFold(parsedURL.Scheme, "http"):
 		return true
 	default:
 		return false