Skip to content

Commit 3e38d60

Browse files
author
Matthew Fisher
committed
Merge pull request #2818 from bacongobbler/you-shall-not-build
fix(controller): disallow evil users from creating builds
2 parents b39b442 + ba5a3aa commit 3e38d60

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

controller/api/tests/test_build.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def test_build_default_containers(self):
172172
def test_build_str(self):
173173
"""Test the text representation of a build."""
174174
url = '/v1/apps'
175-
response = self.client.post(url)
176175
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
177176
self.assertEqual(response.status_code, 201)
178177
app_id = response.data['id']
@@ -207,3 +206,18 @@ def test_admin_can_create_builds_on_other_apps(self):
207206
build = Build.objects.get(uuid=response.data['uuid'])
208207
self.assertEqual(str(build), "{}-{}".format(
209208
response.data['app'], response.data['uuid'][:7]))
209+
210+
@mock.patch('requests.post', mock_import_repository_task)
211+
def test_unauthorized_user_cannot_create_build(self):
212+
"""An unauthorized user should not be able to create builds for other apps."""
213+
url = '/v1/apps'
214+
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
215+
app_id = response.data['id']
216+
# attempt to create a build as a malicious user
217+
evil_user = User.objects.get(username='autotest2')
218+
evil_token = Token.objects.get(user=evil_user).key
219+
url = "/v1/apps/{app_id}/builds".format(**locals())
220+
body = {'image': 'eeeeeevillllll'}
221+
response = self.client.post(url, json.dumps(body), content_type='application/json',
222+
HTTP_AUTHORIZATION='token {}'.format(evil_token))
223+
self.assertEqual(response.status_code, 403)

controller/api/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def get_success_headers(self, data):
260260

261261
def create(self, request, *args, **kwargs):
262262
app = get_object_or_404(models.App, id=self.kwargs['id'])
263+
self.check_object_permissions(self.request, app)
263264
request._data = request.DATA.copy()
264265
request.DATA['app'] = app
265266
try:

0 commit comments

Comments
 (0)