Skip to content

Commit 4c85252

Browse files
committed
Fix 5xx status in old webhooks
1 parent 0765391 commit 4c85252

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

readthedocs/core/views/hooks.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ def build_branches(project, branch_list):
9090

9191

9292
def get_project_from_url(url):
93-
projects = (
94-
Project.objects.filter(repo__iendswith=url) |
95-
Project.objects.filter(repo__iendswith=url + '.git'))
96-
return projects
93+
if not url:
94+
return Project.objects.none()
95+
else:
96+
projects = (
97+
Project.objects.filter(repo__iendswith=url) |
98+
Project.objects.filter(repo__iendswith=url + '.git')
99+
)
100+
return projects
97101

98102

99103
def log_info(project, msg):
@@ -278,13 +282,17 @@ def bitbucket_build(request):
278282
branches = [commit.get('branch', '')
279283
for commit in data['commits']]
280284
repository = data['repository']
285+
if not repository['absolute_url']:
286+
return HttpResponse('Invalid request', status=400)
281287
search_url = 'bitbucket.org{0}'.format(
282288
repository['absolute_url'].rstrip('/')
283289
)
284290
elif version == 2:
285291
changes = data['push']['changes']
286292
branches = [change['new']['name']
287293
for change in changes]
294+
if not data['repository']['full_name']:
295+
return HttpResponse('Invalid request', status=400)
288296
search_url = 'bitbucket.org/{0}'.format(
289297
data['repository']['full_name']
290298
)

readthedocs/rtd_tests/tests/test_post_commit_hooks.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ def test_gitlab_post_commit_knows_default_branches(self):
146146
rtd.default_branch = old_default
147147
rtd.save()
148148

149+
def test_gitlab_request_empty_url(self):
150+
"""
151+
The gitlab hook shouldn't build any project
152+
if the url, ssh_url or ref are empty.
153+
"""
154+
self.payload['project']['http_url'] = ''
155+
r = self.client.post(
156+
'/gitlab/', data=json.dumps(self.payload),
157+
content_type='application/json'
158+
)
159+
self.assertEqual(r.status_code, 404)
160+
149161
def test_gitlab_webhook_is_deprecated(self):
150162
# Project is created after feature, not included in historical allowance
151163
url = 'https://github.com/rtfd/readthedocs-build'
@@ -265,6 +277,19 @@ def test_400_on_no_ref(self):
265277
content_type='application/json')
266278
self.assertEqual(r.status_code, 400)
267279

280+
def test_github_request_empty_url(self):
281+
"""
282+
The github hook shouldn't build any project
283+
if the url, ssh_url or ref are empty.
284+
"""
285+
self.payload['repository']['url'] = ''
286+
self.payload['repository']['ssh_url'] = ''
287+
r = self.client.post(
288+
'/github/', data=json.dumps(self.payload),
289+
content_type='application/json'
290+
)
291+
self.assertEqual(r.status_code, 403)
292+
268293
def test_private_repo_mapping(self):
269294
"""
270295
Test for private GitHub repo mapping.
@@ -534,6 +559,18 @@ def test_bitbucket_default_branch(self):
534559
content_type='application/json')
535560
self.assertContains(r, '(URL Build) Build Started: bitbucket.org/test/project [latest]')
536561

562+
def test_bitbucket_request_empty_url(self):
563+
"""
564+
The bitbucket hook shouldn't build any project
565+
if the url, ssh_url or ref are empty.
566+
"""
567+
self.git_payload['repository']['absolute_url'] = ''
568+
r = self.client.post(
569+
'/bitbucket/', data=json.dumps(self.git_payload),
570+
content_type='application/json'
571+
)
572+
self.assertEqual(r.status_code, 400)
573+
537574
def test_bitbucket_webhook_is_deprecated(self):
538575
# Project is created after feature, not included in historical allowance
539576
url = 'https://bitbucket.org/rtfd/readthedocs-build'

0 commit comments

Comments
 (0)