feed.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package validator // import "miniflux.app/v2/internal/validator"
  4. import (
  5. "log/slog"
  6. "miniflux.app/v2/internal/locale"
  7. "miniflux.app/v2/internal/model"
  8. "miniflux.app/v2/internal/storage"
  9. "miniflux.app/v2/internal/urllib"
  10. )
  11. // ValidateFeedCreation validates feed creation.
  12. func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *locale.LocalizedError {
  13. if request.FeedURL == "" || request.CategoryID <= 0 {
  14. return locale.NewLocalizedError("error.feed_mandatory_fields")
  15. }
  16. if !urllib.IsAbsoluteURL(request.FeedURL) {
  17. return locale.NewLocalizedError("error.invalid_feed_url")
  18. }
  19. if store.FeedURLExists(userID, request.FeedURL) {
  20. return locale.NewLocalizedError("error.feed_already_exists")
  21. }
  22. categoryExists, err := store.CategoryIDExists(userID, request.CategoryID)
  23. if err != nil {
  24. // *locale.LocalizedError (unlike *locale.LocalizedErrorWrapper elsewhere
  25. // in this codebase) has no way to carry an underlying error, so a
  26. // genuine backend failure here can't be distinguished from "category
  27. // not found" in the value returned to the caller. Log it so the
  28. // failure is at least observable, and fall back to the existing
  29. // user-facing message rather than changing this function's public
  30. // return type as part of this PR.
  31. slog.Error("validator: unable to check if feed category exists",
  32. slog.Int64("user_id", userID),
  33. slog.Int64("category_id", request.CategoryID),
  34. slog.Any("error", err),
  35. )
  36. return locale.NewLocalizedError("error.feed_category_not_found")
  37. }
  38. if !categoryExists {
  39. return locale.NewLocalizedError("error.feed_category_not_found")
  40. }
  41. return ValidateFeedSettings(request)
  42. }
  43. // ValidateFeedSettings validates the feed creation settings that do not depend on stored data.
  44. func ValidateFeedSettings(request *model.FeedCreationRequest) *locale.LocalizedError {
  45. if !IsValidRegex(request.BlocklistRules) {
  46. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  47. }
  48. if !IsValidRegex(request.KeeplistRules) {
  49. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  50. }
  51. if !IsValidURLRewriteRules(request.UrlRewriteRules) {
  52. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  53. }
  54. if request.BlockFilterEntryRules != "" {
  55. if err := IsValidFilterRules(request.BlockFilterEntryRules, "block"); err != nil {
  56. return err
  57. }
  58. }
  59. if request.KeepFilterEntryRules != "" {
  60. if err := IsValidFilterRules(request.KeepFilterEntryRules, "keep"); err != nil {
  61. return err
  62. }
  63. }
  64. if err := IsValidRewriteRules(request.RewriteRules); err != nil {
  65. return err
  66. }
  67. if err := IsValidScraperRules(request.ScraperRules); err != nil {
  68. return err
  69. }
  70. if request.ProxyURL != "" && !urllib.IsValidProxyURL(request.ProxyURL) {
  71. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  72. }
  73. return nil
  74. }
  75. // ValidateFeedModification validates feed modification.
  76. func ValidateFeedModification(store *storage.Storage, userID, feedID int64, request *model.FeedModificationRequest) *locale.LocalizedError {
  77. if request.FeedURL != nil {
  78. if *request.FeedURL == "" {
  79. return locale.NewLocalizedError("error.feed_url_not_empty")
  80. }
  81. if !urllib.IsAbsoluteURL(*request.FeedURL) {
  82. return locale.NewLocalizedError("error.invalid_feed_url")
  83. }
  84. if store.AnotherFeedURLExists(userID, feedID, *request.FeedURL) {
  85. return locale.NewLocalizedError("error.feed_already_exists")
  86. }
  87. }
  88. if request.SiteURL != nil {
  89. if *request.SiteURL == "" {
  90. return locale.NewLocalizedError("error.site_url_not_empty")
  91. }
  92. if !urllib.IsAbsoluteURL(*request.SiteURL) {
  93. return locale.NewLocalizedError("error.invalid_site_url")
  94. }
  95. }
  96. if request.Title != nil {
  97. if *request.Title == "" {
  98. return locale.NewLocalizedError("error.feed_title_not_empty")
  99. }
  100. }
  101. if request.CategoryID != nil {
  102. categoryExists, err := store.CategoryIDExists(userID, *request.CategoryID)
  103. if err != nil {
  104. // See the identical rationale in ValidateFeedCreation above.
  105. slog.Error("validator: unable to check if feed category exists",
  106. slog.Int64("user_id", userID),
  107. slog.Int64("category_id", *request.CategoryID),
  108. slog.Any("error", err),
  109. )
  110. return locale.NewLocalizedError("error.feed_category_not_found")
  111. }
  112. if !categoryExists {
  113. return locale.NewLocalizedError("error.feed_category_not_found")
  114. }
  115. }
  116. if request.BlocklistRules != nil {
  117. if !IsValidRegex(*request.BlocklistRules) {
  118. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  119. }
  120. }
  121. if request.KeeplistRules != nil {
  122. if !IsValidRegex(*request.KeeplistRules) {
  123. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  124. }
  125. }
  126. if request.RewriteRules != nil {
  127. if err := IsValidRewriteRules(*request.RewriteRules); err != nil {
  128. return err
  129. }
  130. }
  131. if request.ScraperRules != nil {
  132. if err := IsValidScraperRules(*request.ScraperRules); err != nil {
  133. return err
  134. }
  135. }
  136. if request.UrlRewriteRules != nil {
  137. if !IsValidURLRewriteRules(*request.UrlRewriteRules) {
  138. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  139. }
  140. }
  141. if request.BlockFilterEntryRules != nil && *request.BlockFilterEntryRules != "" {
  142. if err := IsValidFilterRules(*request.BlockFilterEntryRules, "block"); err != nil {
  143. return err
  144. }
  145. }
  146. if request.KeepFilterEntryRules != nil && *request.KeepFilterEntryRules != "" {
  147. if err := IsValidFilterRules(*request.KeepFilterEntryRules, "keep"); err != nil {
  148. return err
  149. }
  150. }
  151. if request.ProxyURL != nil && *request.ProxyURL != "" {
  152. if !urllib.IsValidProxyURL(*request.ProxyURL) {
  153. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  154. }
  155. }
  156. return nil
  157. }