mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Validate Keep list and Block list rules syntax
This commit is contained in:
parent
05fd83bd6f
commit
e8d0360e64
19 changed files with 173 additions and 23 deletions
|
@ -15,7 +15,7 @@ func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.F
|
|||
return NewValidationError("error.feed_mandatory_fields")
|
||||
}
|
||||
|
||||
if !isValidURL(request.FeedURL) {
|
||||
if !IsValidURL(request.FeedURL) {
|
||||
return NewValidationError("error.invalid_feed_url")
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,14 @@ func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.F
|
|||
return NewValidationError("error.feed_category_not_found")
|
||||
}
|
||||
|
||||
if !IsValidRegex(request.BlocklistRules) {
|
||||
return NewValidationError("error.feed_invalid_blocklist_rule")
|
||||
}
|
||||
|
||||
if !IsValidRegex(request.KeeplistRules) {
|
||||
return NewValidationError("error.feed_invalid_keeplist_rule")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -37,7 +45,7 @@ func ValidateFeedModification(store *storage.Storage, userID int64, request *mod
|
|||
return NewValidationError("error.feed_url_not_empty")
|
||||
}
|
||||
|
||||
if !isValidURL(*request.FeedURL) {
|
||||
if !IsValidURL(*request.FeedURL) {
|
||||
return NewValidationError("error.invalid_feed_url")
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +55,7 @@ func ValidateFeedModification(store *storage.Storage, userID int64, request *mod
|
|||
return NewValidationError("error.site_url_not_empty")
|
||||
}
|
||||
|
||||
if !isValidURL(*request.SiteURL) {
|
||||
if !IsValidURL(*request.SiteURL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
}
|
||||
}
|
||||
|
@ -64,5 +72,17 @@ func ValidateFeedModification(store *storage.Storage, userID int64, request *mod
|
|||
}
|
||||
}
|
||||
|
||||
if request.BlocklistRules != nil {
|
||||
if !IsValidRegex(*request.BlocklistRules) {
|
||||
return NewValidationError("error.feed_invalid_blocklist_rule")
|
||||
}
|
||||
}
|
||||
|
||||
if request.KeeplistRules != nil {
|
||||
if !IsValidRegex(*request.KeeplistRules) {
|
||||
return NewValidationError("error.feed_invalid_keeplist_rule")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import "miniflux.app/model"
|
|||
|
||||
// ValidateSubscriptionDiscovery validates subscription discovery requests.
|
||||
func ValidateSubscriptionDiscovery(request *model.SubscriptionDiscoveryRequest) *ValidationError {
|
||||
if !isValidURL(request.URL) {
|
||||
if !IsValidURL(request.URL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"miniflux.app/locale"
|
||||
)
|
||||
|
@ -53,7 +54,14 @@ func ValidateDirection(direction string) error {
|
|||
return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
|
||||
}
|
||||
|
||||
func isValidURL(absoluteURL string) bool {
|
||||
// IsValidRegex verifies if the regex can be compiled.
|
||||
func IsValidRegex(expr string) bool {
|
||||
_, err := regexp.Compile(expr)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsValidURL verifies if the provided value is a valid absolute URL.
|
||||
func IsValidURL(absoluteURL string) bool {
|
||||
_, err := url.ParseRequestURI(absoluteURL)
|
||||
return err == nil
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ func TestIsValidURL(t *testing.T) {
|
|||
}
|
||||
|
||||
for link, expected := range scenarios {
|
||||
result := isValidURL(link)
|
||||
result := IsValidURL(link)
|
||||
if result != expected {
|
||||
t.Errorf(`Unexpected result, got %v instead of %v`, result, expected)
|
||||
}
|
||||
|
@ -46,3 +46,17 @@ func TestValidateDirection(t *testing.T) {
|
|||
t.Error(`An invalid direction should generate a error`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidRegex(t *testing.T) {
|
||||
scenarios := map[string]bool{
|
||||
"(?i)miniflux": true,
|
||||
"[": false,
|
||||
}
|
||||
|
||||
for expr, expected := range scenarios {
|
||||
result := IsValidRegex(expr)
|
||||
if result != expected {
|
||||
t.Errorf(`Unexpected result, got %v instead of %v`, result, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue