2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-01-04 06:20:21 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package validator // import "miniflux.app/v2/internal/validator"
|
2021-01-04 06:20:21 +01:00
|
|
|
|
|
|
|
import (
|
2024-07-03 06:03:49 +02:00
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/locale"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2021-01-04 06:20:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateUserCreationWithPassword validates user creation with a password.
|
2023-10-22 04:50:29 +02:00
|
|
|
func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
if request.Username == "" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.user_mandatory_fields")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if store.UserExists(request.Username) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.user_already_exists")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validatePassword(request.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateUserModification validates user modifications.
|
2023-10-22 04:50:29 +02:00
|
|
|
func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
if changes.Username != nil {
|
|
|
|
if *changes.Username == "" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.user_mandatory_fields")
|
2021-01-04 06:20:21 +01:00
|
|
|
} else if store.AnotherUserExists(userID, *changes.Username) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.user_already_exists")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.Password != nil {
|
|
|
|
if err := validatePassword(*changes.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.Theme != nil {
|
|
|
|
if err := validateTheme(*changes.Theme); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.Language != nil {
|
|
|
|
if err := validateLanguage(*changes.Language); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.Timezone != nil {
|
|
|
|
if err := validateTimezone(store, *changes.Timezone); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.EntryDirection != nil {
|
|
|
|
if err := validateEntryDirection(*changes.EntryDirection); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.EntriesPerPage != nil {
|
|
|
|
if err := validateEntriesPerPage(*changes.EntriesPerPage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:29:51 +01:00
|
|
|
if changes.DisplayMode != nil {
|
|
|
|
if err := validateDisplayMode(*changes.DisplayMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 07:37:57 +02:00
|
|
|
if changes.GestureNav != nil {
|
|
|
|
if err := validateGestureNav(*changes.GestureNav); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:53:05 +02:00
|
|
|
if changes.DefaultReadingSpeed != nil {
|
|
|
|
if err := validateReadingSpeed(*changes.DefaultReadingSpeed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.CJKReadingSpeed != nil {
|
|
|
|
if err := validateReadingSpeed(*changes.CJKReadingSpeed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 22:07:55 +02:00
|
|
|
if changes.DefaultHomePage != nil {
|
|
|
|
if err := validateDefaultHomePage(*changes.DefaultHomePage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 14:20:02 +01:00
|
|
|
if changes.MediaPlaybackRate != nil {
|
|
|
|
if err := validateMediaPlaybackRate(*changes.MediaPlaybackRate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 06:03:49 +02:00
|
|
|
if changes.BlockFilterEntryRules != nil {
|
|
|
|
if err := isValidFilterRules(*changes.BlockFilterEntryRules, "block"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.KeepFilterEntryRules != nil {
|
|
|
|
if err := isValidFilterRules(*changes.KeepFilterEntryRules, "keep"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:53:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateReadingSpeed(readingSpeed int) *locale.LocalizedError {
|
2021-08-30 16:53:05 +02:00
|
|
|
if readingSpeed <= 0 {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.settings_reading_speed_is_positive")
|
2021-08-30 16:53:05 +02:00
|
|
|
}
|
2021-01-04 06:20:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validatePassword(password string) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
if len(password) < 6 {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.password_min_length")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateTheme(theme string) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
themes := model.Themes()
|
|
|
|
if _, found := themes[theme]; !found {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_theme")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateLanguage(language string) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
languages := locale.AvailableLanguages()
|
|
|
|
if _, found := languages[language]; !found {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_language")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateTimezone(store *storage.Storage, timezone string) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
timezones, err := store.Timezones()
|
|
|
|
if err != nil {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError(err.Error())
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := timezones[timezone]; !found {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_timezone")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateEntryDirection(direction string) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
if direction != "asc" && direction != "desc" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_entry_direction")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateEntriesPerPage(entriesPerPage int) *locale.LocalizedError {
|
2021-01-04 06:20:21 +01:00
|
|
|
if entriesPerPage < 1 {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.entries_per_page_invalid")
|
2021-01-04 06:20:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-28 22:29:51 +01:00
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateDisplayMode(displayMode string) *locale.LocalizedError {
|
2021-02-28 22:29:51 +01:00
|
|
|
if displayMode != "fullscreen" && displayMode != "standalone" && displayMode != "minimal-ui" && displayMode != "browser" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_display_mode")
|
2021-02-28 22:29:51 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-20 22:07:55 +02:00
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateGestureNav(gestureNav string) *locale.LocalizedError {
|
2022-09-30 07:37:57 +02:00
|
|
|
if gestureNav != "none" && gestureNav != "tap" && gestureNav != "swipe" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_gesture_nav")
|
2022-09-30 07:37:57 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func validateDefaultHomePage(defaultHomePage string) *locale.LocalizedError {
|
2022-07-20 22:07:55 +02:00
|
|
|
defaultHomePages := model.HomePages()
|
|
|
|
if _, found := defaultHomePages[defaultHomePage]; !found {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_default_home_page")
|
2022-07-20 22:07:55 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-16 14:20:02 +01:00
|
|
|
|
|
|
|
func validateMediaPlaybackRate(mediaPlaybackRate float64) *locale.LocalizedError {
|
|
|
|
if mediaPlaybackRate < 0.25 || mediaPlaybackRate > 4 {
|
|
|
|
return locale.NewLocalizedError("error.settings_media_playback_rate_range")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-03 06:03:49 +02:00
|
|
|
|
|
|
|
func isValidFilterRules(filterEntryRules string, filterType string) *locale.LocalizedError {
|
2024-07-03 15:05:09 +02:00
|
|
|
// Valid Format: FieldName=RegEx\nFieldName=RegEx...
|
2024-07-03 06:03:49 +02:00
|
|
|
fieldNames := []string{"EntryTitle", "EntryURL", "EntryCommentsURL", "EntryContent", "EntryAuthor", "EntryTag"}
|
|
|
|
|
|
|
|
rules := strings.Split(filterEntryRules, "\n")
|
|
|
|
for i, rule := range rules {
|
|
|
|
// Check if rule starts with a valid fieldName
|
|
|
|
idx := slices.IndexFunc(fieldNames, func(fieldName string) bool { return strings.HasPrefix(rule, fieldName) })
|
|
|
|
if idx == -1 {
|
|
|
|
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_fieldname_invalid", i+1, "'"+strings.Join(fieldNames, "', '")+"'")
|
|
|
|
}
|
|
|
|
fieldName := fieldNames[idx]
|
|
|
|
fieldRegEx, _ := strings.CutPrefix(rule, fieldName)
|
|
|
|
|
|
|
|
// Check if regex begins with a =
|
|
|
|
if !strings.HasPrefix(fieldRegEx, "=") {
|
|
|
|
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_separator_required", i+1)
|
|
|
|
}
|
|
|
|
fieldRegEx = strings.TrimPrefix(fieldRegEx, "=")
|
|
|
|
|
|
|
|
if fieldRegEx == "" {
|
|
|
|
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_regex_required", i+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if provided pattern is a valid RegEx
|
|
|
|
if !IsValidRegex(fieldRegEx) {
|
|
|
|
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_invalid_regex", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|