8000 [pull] main from miniflux:main by pull[bot] · Pull Request #332 · 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
2 changes: 1 addition & 1 deletion internal/ui/form/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *SubscriptionForm) Validate() *locale.LocalizedError {
return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
}

if s.ProxyURL != "" && !urllib.IsAbsoluteURL(s.ProxyURL) {
if s.ProxyURL != "" && !urllib.IsValidProxyURL(s.ProxyURL) {
return locale.NewLocalizedError("error.invalid_feed_proxy_url")
}

Expand Down
17 changes: 17 additions & 0 deletions internal/urllib/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func hasHTTPPrefix(inputURL string) bool {
return strings.HasPrefix(inputURL, "https://") || strings.HasPrefix(inputURL, "http://")
}

// hasSOCKSPrefix reports whether the URL string begins with an SOCKS5 or SOCKS5H scheme.
func hasSOCKSPrefix(inputURL string) bool {
return strings.HasPrefix(inputURL, "socks5://") || strings.HasPrefix(inputURL, "socks5h://")
}

// IsAbsoluteURL reports whether the link is absolute and starts with an HTTP or HTTPS scheme.
func IsAbsoluteURL(inputURL string) bool {
if !hasHTTPPrefix(inputURL) {
Expand All @@ -55,6 +60,18 @@ func IsAbsoluteURL(inputURL string) bool {
return parsedURL.IsAbs()
}

// IsValidProxyURL reports whether the url is absolute, has a host and starts with an HTTP, HTTPS, SOCKS5 or SOCKS5H scheme.
func IsValidProxyURL(inputURL string) bool {
if !hasHTTPPrefix(inputURL) && !hasSOCKSPrefix(inputURL) {
return false
}
parsedURL, err := url.Parse(inputURL)
if err != nil {
return false
}
return parsedURL.IsAbs() && parsedURL.Host != ""
}

// resolveToAbsoluteURL resolves a relative URL using a base URL, parsing the base only if needed.
func resolveToAbsoluteURL(parsedBaseURL *url.URL, baseURL, relativeURL string) (string, error) {
// Avoid parsing the relative URL if it's already absolute
Expand Down
28 changes: 28 additions & 0 deletions internal/urllib/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ func TestIsAbsoluteURL(t *testing.T) {
}
}

func TestIsValidProxyURL(t *testing.T) {
scenarios := map[string]bool{
"http://127.0.0.1:3128": true,
"http://[::1]:1055": true,
"https://proxy.example.org": true,
"socks5://127.0.0.1:1080": true,
"socks5h://127.0.0.1:1080": true,
"socks5://[::1]:1055": true,
"socks5h://[::1]:1055": true,
"socks5://[::1%25eno1]:1055": true,
"ftp://host": false,
"/relative/path": false,
"invalid url": false,
"http://[::1": false,
"http:///var/run/socket": false,
"sock:///socket.file": false,
"socks5://[::1%eno1]:1055": false,
"": false,
}

for input, expected := range scenarios {
actual := IsValidProxyURL(input)
if actual != expected {
t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input)
}
}
}

func TestAbsoluteURL(t *testing.T) {
type absoluteScenario struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions internal/validator/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.F
}
}

if request.ProxyURL != "" && !urllib.IsAbsoluteURL(request.ProxyURL) {
if request.ProxyURL != "" && !urllib.IsValidProxyURL(request.ProxyURL) {
return locale.NewLocalizedError("error.invalid_feed_proxy_url")
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func ValidateFeedModification(store *storage.Storage, userID, feedID int64, requ
return locale.NewLocalizedError("error.proxy_url_not_empty")
}

if !urllib.IsAbsoluteURL(*request.ProxyURL) {
if !urllib.IsValidProxyURL(*request.ProxyURL) {
return locale.NewLocalizedError("error.invalid_feed_proxy_url")
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/validator/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ValidateSubscriptionDiscovery(request *model.SubscriptionDiscoveryRequest)
return locale.NewLocalizedError("error.invalid_site_url")
}

if request.ProxyURL != "" && !urllib.IsAbsoluteURL(request.ProxyURL) {
if request.ProxyURL != "" && !urllib.IsValidProxyURL(request.ProxyURL) {
return locale.NewLocalizedError("error.invalid_proxy_url")
}

Expand Down
0