Tags: harden validations

An invalid input would cause a server execption. We now validate
that the body in the tags validation is not None.

TrivialFix

Change-Id: I97a00ee6d2672485334bba757706f107e052b551
Closes-Bug: #1736678
This commit is contained in:
Gary Kotton 2017-12-06 00:32:24 -08:00
parent 259eedaaba
commit 5da1a6f7c6
2 changed files with 16 additions and 3 deletions

View File

@ -71,7 +71,7 @@ def validate_tag(tag):
def validate_tags(body):
if 'tags' not in body:
if not isinstance(body, dict) or 'tags' not in body:
raise exceptions.InvalidInput(error_message=_("Invalid tags body"))
msg = validators.validate_list_of_unique_strings(body['tags'], MAX_TAG_LEN)
if msg:

View File

@ -161,8 +161,13 @@ class TestTagApiBase(test_securitygroup.SecurityGroupsTestCase,
subresource='tags', sub_id=tag)
return req.get_response(self.ext_api)
def _put_tags(self, tags):
body = {'tags': tags}
def _put_tags(self, tags=None, body=None):
if tags:
body = {'tags': tags}
elif body:
body = body
else:
body = {}
req = self._req('PUT', self.collection, data=body, id=self.resource_id,
subresource='tags')
return req.get_response(self.ext_api)
@ -273,6 +278,14 @@ class TestResourceTagApi(TestTagApiBase):
self._assertEqualTags(['red', 'green'], tags)
self._test_notification_report(expect_notify)
def test_put_invalid_tags(self):
res = self._put_tags()
self.assertEqual(400, res.status_int)
res = self._put_tags(body=7)
self.assertEqual(400, res.status_int)
res = self._put_tags(body={'invalid': True})
self.assertEqual(400, res.status_int)
def test_put_tags_replace(self):
res = self._put_tags(['red', 'green'])
self.assertEqual(200, res.status_int)