diff --git a/zulip_bots/zulip_bots/bots/github_detail/github_detail.py b/zulip_bots/zulip_bots/bots/github_detail/github_detail.py index e304866..42c5bca 100644 --- a/zulip_bots/zulip_bots/bots/github_detail/github_detail.py +++ b/zulip_bots/zulip_bots/bots/github_detail/github_detail.py @@ -74,9 +74,15 @@ class GithubHandler(object): return # Capture owner, repo, id - issue_prs = re.finditer( - self.HANDLE_MESSAGE_REGEX, message['content']) + issue_prs = list(re.finditer( + self.HANDLE_MESSAGE_REGEX, message['content'])) bot_messages = [] + if len(issue_prs) > 5: + # We limit to 5 requests to prevent denial-of-service + bot_message = 'Please ask for <=5 links in any one request' + bot_handler.send_reply(message, bot_message) + return + for issue_pr in issue_prs: owner, repo = self.get_owner_and_repo(issue_pr) if owner and repo: diff --git a/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py b/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py index 325d718..645a8f7 100755 --- a/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py +++ b/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py @@ -81,3 +81,11 @@ class TestGithubDetailBot(BotTestCase): with self.mock_config_info(self.mock_config): self.verify_reply(request, bot_response) + + def test_too_many_request(self) -> None: + request = 'zulip/zulip#1 zulip/zulip#1 zulip/zulip#1 zulip/zulip#1 '\ + 'zulip/zulip#1 zulip/zulip#1 zulip/zulip#1 zulip/zulip#1' + bot_response = 'Please ask for <=5 links in any one request' + + with self.mock_config_info(self.mock_config): + self.verify_reply(request, bot_response)