From 2677720577342e27a327cb3a2f921df9dc0b5c48 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 14 Nov 2017 13:40:11 -0500 Subject: [PATCH] Fix exception raised for certain non-JSON strings given to is_json This is breaking pushes in production for certain manifests Fixes https://jira.prod.coreos.systems/browse/QS-60 --- test/registry_tests.py | 21 +++++++++++++++++++++ util/test/test_validation.py | 17 +++++++++++++++++ util/validation.py | 4 ++-- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 util/test/test_validation.py diff --git a/test/registry_tests.py b/test/registry_tests.py index 5a069e34a..619ebe212 100644 --- a/test/registry_tests.py +++ b/test/registry_tests.py @@ -1536,6 +1536,27 @@ class V2RegistryTests(V2RegistryPullMixin, V2RegistryPushMixin, RegistryTestsMix self.assertTrue('text/plain' in media_types) self.assertTrue('application/json' in media_types) + def test_not_json_labels(self): + # Push a new repo with the latest tag. + images = [{ + 'id': 'someid', + 'config': {'Labels': {'foo': '[hello world]', 'bar': '{wassup?!}'}}, + 'contents': 'somecontent' + }] + + (_, manifests) = self.do_push('devtable', 'newrepo', 'devtable', 'password', images=images) + digest = manifests['latest'].digest + + self.conduct_api_login('devtable', 'password') + labels = self.conduct('GET', '/api/v1/repository/devtable/newrepo/manifest/' + digest + '/labels').json() + self.assertEquals(2, len(labels['labels'])) + + media_types = set([label['media_type'] for label in labels['labels']]) + + self.assertTrue('text/plain' in media_types) + self.assertFalse('application/json' in media_types) + + def test_expiration_label(self): # Push a new repo with the latest tag. images = [{ diff --git a/util/test/test_validation.py b/util/test/test_validation.py new file mode 100644 index 000000000..2381f0455 --- /dev/null +++ b/util/test/test_validation.py @@ -0,0 +1,17 @@ +import pytest + +from util.validation import is_json + +@pytest.mark.parametrize('string_value,expected', [ + ('{}', True), + ('[]', True), + ('[hello world]', False), + ('{hello world}', False), + ('[test] this is a test', False), + ('hello world', False), + ('{"hi": "there"}', True), + ('[1, 2, 3, 4]', True), + ('[1, 2, {"num": 3}, 4]', True), +]) +def test_is_json(string_value, expected): + assert is_json(string_value) == expected diff --git a/util/validation.py b/util/validation.py index 92d766159..235c4afa5 100644 --- a/util/validation.py +++ b/util/validation.py @@ -86,6 +86,6 @@ def is_json(value): try: json.loads(value) return True - except TypeError: + except (TypeError, ValueError): return False - + return False