diff --git a/glance/api/v3/artifacts.py b/glance/api/v3/artifacts.py index df50372f0e..88a9884c69 100644 --- a/glance/api/v3/artifacts.py +++ b/glance/api/v3/artifacts.py @@ -114,6 +114,13 @@ class ArtifactsController(object): filter['value'] = self._get_latest_version( req, filters['name'][0]['value'], type_name, type_version) + else: + try: + semantic_version.Version(filter['value'], partial=True) + except ValueError: + msg = (_('The format of the version %s is not valid. ' + 'Use semver notation') % filter['value']) + raise webob.exc.HTTPBadRequest(explanation=msg) res = artifact_repo.list(filters=filters, show_level=Showlevel.BASIC, diff --git a/glance/tests/functional/artifacts/test_artifacts.py b/glance/tests/functional/artifacts/test_artifacts.py index a697bcf4c3..b71b14a5c3 100644 --- a/glance/tests/functional/artifacts/test_artifacts.py +++ b/glance/tests/functional/artifacts/test_artifacts.py @@ -1956,3 +1956,12 @@ paste.filter_factory = glance.tests.utils:FakeAuthMiddleware.factory result = self._check_artifact_get(url=url)['artifacts'] self.assertEqual(1, len(result)) + + def test_filter_by_bad_version(self): + bad_versions = ['kkk', '1.k', 'h.0', '1.3.hf', 's.9.2s2'] + response_string = ('The format of the version %s is not valid. ' + 'Use semver notation') + for bad_version in bad_versions: + url = '/withprops/v1.0/drafts?version=gt:%s' % bad_version + result = self._check_artifact_get(url=url, status=400) + self.assertIn(response_string % bad_version, result)