Skip to content

Commit

Permalink
Avoid exceptions trying to parse incorrect links
Browse files Browse the repository at this point in the history
  • Loading branch information
entantoencuanto committed Jan 7, 2025
1 parent 8458de1 commit 8ba66d0
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def calculate_positive(rule)
end

class LinksParser
UNWISE_REGEXP = /[{}|\\^\[\]`]/

attr_reader :allowlist, :blocklist

def initialize(text, allowlist: [], blocklist: [])
Expand All @@ -93,7 +95,8 @@ def initialize(text, allowlist: [], blocklist: [])

def filtered_links
@filtered_links ||= links.reject do |link|
hostname = URI.parse(link).hostname
hostname = URI.parse(clean_link(link)).hostname

hostname.blank? ||
(allowlist.present? && allowlist.any? { |name| name == hostname }) ||
(blocklist.present? && blocklist.all? { |name| name != hostname })
Expand All @@ -107,6 +110,14 @@ def links
def has_blocked_links?
filtered_links.present?
end

# This method removes characters from link producing errors trying
# to parse them
def clean_link(link)
return link unless UNWISE_REGEXP.match?(link)

link.split(UNWISE_REGEXP).first
end
end

class UserActivities
Expand Down

0 comments on commit 8ba66d0

Please sign in to comment.