From 2c08b40bf08a7dfbe8aa3a7b9a2648010b51330b Mon Sep 17 00:00:00 2001 From: Kamil Rykowski Date: Wed, 18 Mar 2015 13:29:20 +0100 Subject: [PATCH] Validate tag name when filtering for images Right now all invalid tags are skipped when filtering for image list. We should change this behaviour to raise an exception when invalid tag value is provided. Additionally remove misplaced `pass` from one of the tests. Closes-Bug: 1433962 Change-Id: If5148608a70ee019697ea2dcb84e19a53222d596 --- glanceclient/v2/images.py | 6 ++++-- tests/v2/test_images.py | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/glanceclient/v2/images.py b/glanceclient/v2/images.py index 17193257..01ce40b9 100644 --- a/glanceclient/v2/images.py +++ b/glanceclient/v2/images.py @@ -132,8 +132,10 @@ class Controller(object): tags_url_params = [] for tag in tags: - if isinstance(tag, six.string_types): - tags_url_params.append({'tag': encodeutils.safe_encode(tag)}) + if not isinstance(tag, six.string_types): + raise exc.HTTPBadRequest("Invalid tag value %s" % tag) + + tags_url_params.append({'tag': encodeutils.safe_encode(tag)}) for param, value in six.iteritems(filters): if isinstance(value, six.string_types): diff --git a/tests/v2/test_images.py b/tests/v2/test_images.py index 00b06e34..f15d795e 100644 --- a/tests/v2/test_images.py +++ b/tests/v2/test_images.py @@ -631,7 +631,6 @@ class TestController(testtools.TestCase): images = list(self.controller.list(**filters)) self.assertEqual(1, len(images)) self.assertEqual('%s' % img_id, images[0].id) - pass def test_list_images_for_tag_multiple_images(self): img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1' @@ -654,6 +653,13 @@ class TestController(testtools.TestCase): images = list(self.controller.list(**filters)) self.assertEqual(0, len(images)) + def test_list_images_for_invalid_tag(self): + filters = {'filters': {'tag': [[]]}} + + self.assertRaises(exc.HTTPBadRequest, + list, + self.controller.list(**filters)) + def test_list_images_with_single_sort_key(self): img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1' sort_key = 'name'