1
0
Fork 0
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:
Frédéric Guillot 2024-10-05 20:37:30 -07:00
parent 600dea6ce5
commit e555e442fb
32 changed files with 257 additions and 56 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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)
}
}
}