mirror of
https://github.com/miniflux/v2.git
synced 2025-08-31 18:31:01 +00:00
feat: add new settings option to allow external fonts
This commit is contained in:
parent
600dea6ce5
commit
e555e442fb
32 changed files with 257 additions and 56 deletions
|
@ -123,6 +123,12 @@ func ValidateUserModification(store *storage.Storage, userID int64, changes *mod
|
|||
}
|
||||
}
|
||||
|
||||
if changes.ExternalFontHosts != nil {
|
||||
if !IsValidDomainList(*changes.ExternalFontHosts) {
|
||||
return locale.NewLocalizedError("error.settings_invalid_domain_list")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,11 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var domainRegex = regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
|
||||
|
||||
// ValidateRange makes sure the offset/limit values are valid.
|
||||
func ValidateRange(offset, limit int) error {
|
||||
if offset < 0 {
|
||||
|
@ -43,3 +46,24 @@ func IsValidURL(absoluteURL string) bool {
|
|||
_, err := url.ParseRequestURI(absoluteURL)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func IsValidDomain(domain string) bool {
|
||||
domain = strings.ToLower(domain)
|
||||
|
||||
if len(domain) < 1 || len(domain) > 253 {
|
||||
return false
|
||||
}
|
||||
|
||||
return domainRegex.MatchString(domain)
|
||||
}
|
||||
|
||||
func IsValidDomainList(value string) bool {
|
||||
domains := strings.Split(strings.TrimSpace(value), " ")
|
||||
for _, domain := range domains {
|
||||
if !IsValidDomain(domain) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -59,3 +59,21 @@ func TestIsValidRegex(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidDomain(t *testing.T) {
|
||||
scenarios := map[string]bool{
|
||||
"example.org": true,
|
||||
"example": false,
|
||||
"example.": false,
|
||||
"example..": false,
|
||||
"mail.example.com:443": false,
|
||||
"*.example.com": false,
|
||||
}
|
||||
|
||||
for domain, expected := range scenarios {
|
||||
result := IsValidDomain(domain)
|
||||
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