Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins.rules: URLCallback shall handle invalid URLs by ignoring them #2086

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions sopel/plugins/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,14 +1678,16 @@ def match(self, bot, pretrigger):
if not self.match_preconditions(bot, pretrigger):
return

urls = (
url
for url in pretrigger.urls
if urlparse(url).scheme in self._schemes
)
# Parse only valid URLs with wanted schemes
for url in pretrigger.urls:
try:
if urlparse(url).scheme not in self._schemes:
# skip URLs with unwanted scheme
continue
except ValueError:
# skip invalid URLs
continue

# Parse URL for each found
for url in urls:
# TODO: convert to 'yield from' when dropping Python 2.7
for result in self.parse(url):
yield result
Expand Down
16 changes: 15 additions & 1 deletion test/plugins/test_plugins_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2609,13 +2609,27 @@ def test_url_callback_parse():
re.escape('https://wikipedia.com/') + r'(\w+)'
)

rule = rules.SearchRule([regex])
rule = rules.URLCallback([regex])
dgw marked this conversation as resolved.
Show resolved Hide resolved
results = list(rule.parse('https://wikipedia.com/something'))
assert len(results) == 1, 'URLCallback on word must match only once'
assert results[0].group(0) == 'https://wikipedia.com/something'
assert results[0].group(1) == 'something'


def test_url_callback_match(mockbot):
regex = re.compile(r'.*')
rule = rules.URLCallback([regex])

line = (
':Foo!foo@example.com PRIVMSG #sopel :'
'two links http://example.com one invalid https://[dfdsdfsdf'
)
pretrigger = trigger.PreTrigger(mockbot.nick, line)
matches = list(rule.match(mockbot, pretrigger))
assert len(matches) == 1, 'URLCallback must ignore invalid URLs'
assert matches[0].group(0) == 'http://example.com'


def test_url_callback_execute(mockbot):
regex = re.compile(r'.*')
rule = rules.URLCallback([regex])
Expand Down