github_detail: Limit bot to 5 requests per message.

Fixes #172 .
This commit is contained in:
Nikhil Kumar Mishra 2017-12-15 03:59:42 +05:30 committed by showell
parent 0e8a40b7c0
commit d3916b945d
2 changed files with 16 additions and 2 deletions

View file

@ -74,9 +74,15 @@ class GithubHandler(object):
return return
# Capture owner, repo, id # Capture owner, repo, id
issue_prs = re.finditer( issue_prs = list(re.finditer(
self.HANDLE_MESSAGE_REGEX, message['content']) self.HANDLE_MESSAGE_REGEX, message['content']))
bot_messages = [] 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: for issue_pr in issue_prs:
owner, repo = self.get_owner_and_repo(issue_pr) owner, repo = self.get_owner_and_repo(issue_pr)
if owner and repo: if owner and repo:

View file

@ -81,3 +81,11 @@ class TestGithubDetailBot(BotTestCase):
with self.mock_config_info(self.mock_config): with self.mock_config_info(self.mock_config):
self.verify_reply(request, bot_response) 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)