Fix 500 error when filtering by invalid version string

Added checking the filter version pattern semver

ApiImpact
FastTrack

Change-Id: Id3e55faa7fdb6c69cd482c8a6b5b7152fda7310c
Closes-bug: #1491049
This commit is contained in:
Darja Shakhray 2015-09-15 14:44:00 +03:00
parent fc44ebb8e9
commit cd0ec70c95
2 changed files with 16 additions and 0 deletions

View File

@ -116,6 +116,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,

View File

@ -1874,3 +1874,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)