Fixes content-type checking for image uploading in API v1 and v2

wsgi.Request.get_content_type() need a list or set for
allowed_content_types as parameter, but API controller passes request's
content type as a string into it directly. This caused 'in' testing
failure.

Fixes bug: 1241387

Change-Id: I820ccce92e4ec1c94b5144309d6ebe2b57baaf55
Signed-off-by: Zhi Yan Liu <zhiyanl@cn.ibm.com>
This commit is contained in:
Zhi Yan Liu 2013-10-21 16:59:04 +08:00
parent 6e9c4dc9f9
commit b316059835
5 changed files with 28 additions and 5 deletions

View File

@ -525,7 +525,7 @@ class Controller(controller.BaseController):
image_meta['size'] = image_size or image_meta['size'] image_meta['size'] = image_size or image_meta['size']
else: else:
try: try:
req.get_content_type('application/octet-stream') req.get_content_type(('application/octet-stream',))
except exception.InvalidContentType: except exception.InvalidContentType:
upload_utils.safe_kill(req, image_meta['id']) upload_utils.safe_kill(req, image_meta['id'])
msg = _("Content-Type must be application/octet-stream") msg = _("Content-Type must be application/octet-stream")

View File

@ -145,7 +145,7 @@ class ImageDataController(object):
class RequestDeserializer(wsgi.JSONRequestDeserializer): class RequestDeserializer(wsgi.JSONRequestDeserializer):
def upload(self, request): def upload(self, request):
try: try:
request.get_content_type('application/octet-stream') request.get_content_type(('application/octet-stream',))
except exception.InvalidContentType: except exception.InvalidContentType:
raise webob.exc.HTTPUnsupportedMediaType() raise webob.exc.HTTPUnsupportedMediaType()

View File

@ -34,18 +34,18 @@ class RequestTest(test_utils.BaseTestCase):
def test_content_type_missing(self): def test_content_type_missing(self):
request = wsgi.Request.blank('/tests/123') request = wsgi.Request.blank('/tests/123')
self.assertRaises(exception.InvalidContentType, self.assertRaises(exception.InvalidContentType,
request.get_content_type, ('application/xml')) request.get_content_type, ('application/xml',))
def test_content_type_unsupported(self): def test_content_type_unsupported(self):
request = wsgi.Request.blank('/tests/123') request = wsgi.Request.blank('/tests/123')
request.headers["Content-Type"] = "text/html" request.headers["Content-Type"] = "text/html"
self.assertRaises(exception.InvalidContentType, self.assertRaises(exception.InvalidContentType,
request.get_content_type, ('application/xml')) request.get_content_type, ('application/xml',))
def test_content_type_with_charset(self): def test_content_type_with_charset(self):
request = wsgi.Request.blank('/tests/123') request = wsgi.Request.blank('/tests/123')
request.headers["Content-Type"] = "application/json; charset=UTF-8" request.headers["Content-Type"] = "application/json; charset=UTF-8"
result = request.get_content_type(('application/json')) result = request.get_content_type(('application/json',))
self.assertEqual(result, "application/json") self.assertEqual(result, "application/json")
def test_content_type_from_accept_xml(self): def test_content_type_from_accept_xml(self):

View File

@ -1204,6 +1204,23 @@ class TestGlanceAPI(base.IsolatedUnitTest):
res = req.get_response(self.api) res = req.get_response(self.api)
self.assertEqual(res.status_int, 200) self.assertEqual(res.status_int, 200)
def test_add_image_wrong_content_type(self):
fixture_headers = {
'x-image-meta-name': 'fake image #3',
'x-image-meta-container_format': 'ami',
'x-image-meta-disk_format': 'ami',
'transfer-encoding': 'chunked',
'content-type': 'application/octet-st',
}
req = webob.Request.blank("/images")
req.method = 'POST'
for k, v in fixture_headers.iteritems():
req.headers[k] = v
res = req.get_response(self.api)
self.assertEquals(res.status_int, 400)
def test_get_index_sort_name_asc(self): def test_get_index_sort_name_asc(self):
""" """
Tests that the /images registry API returns list of Tests that the /images registry API returns list of

View File

@ -344,6 +344,12 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
self.assertRaises(webob.exc.HTTPUnsupportedMediaType, self.assertRaises(webob.exc.HTTPUnsupportedMediaType,
self.deserializer.upload, request) self.deserializer.upload, request)
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-st'
request.body = 'YYYYY'
self.assertRaises(webob.exc.HTTPUnsupportedMediaType,
self.deserializer.upload, request)
class TestImageDataSerializer(test_utils.BaseTestCase): class TestImageDataSerializer(test_utils.BaseTestCase):