reader/processor: error out for improper rewrite regexp

It's possible to specify a rewrite regex that validates but doesn't compile such
as:

    rewrite("(((unmatched-capture-group"|"rewrite)))")

In case we encounter one, exit early instead of letting the server panic.
This commit is contained in:
Ankit Pandey 2024-05-26 13:57:14 -07:00 committed by Frédéric Guillot
parent 5ce3f24838
commit b68b05c64c

View file

@ -232,7 +232,14 @@ func getUrlFromEntry(feed *model.Feed, entry *model.Entry) string {
parts := customReplaceRuleRegex.FindStringSubmatch(feed.UrlRewriteRules) parts := customReplaceRuleRegex.FindStringSubmatch(feed.UrlRewriteRules)
if len(parts) >= 3 { if len(parts) >= 3 {
re := regexp.MustCompile(parts[1]) re, err := regexp.Compile(parts[1])
if err != nil {
slog.Error("Failed on regexp compilation",
slog.String("url_rewrite_rules", feed.UrlRewriteRules),
slog.Any("error", err),
)
return url
}
url = re.ReplaceAllString(entry.URL, parts[2]) url = re.ReplaceAllString(entry.URL, parts[2])
slog.Debug("Rewriting entry URL", slog.Debug("Rewriting entry URL",
slog.String("original_entry_url", entry.URL), slog.String("original_entry_url", entry.URL),