diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index 4f5e2a95e71..21428791f52 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -718,12 +718,7 @@ class Resource(wsgi.Application): if len(request.body) == 0: LOG.debug("Empty body provided in request") return None, '' - - try: - content_type = request.get_content_type() - except exception.InvalidContentType: - LOG.debug("Unrecognized Content-Type provided in request") - return None, '' + content_type = request.get_content_type() if not content_type: LOG.debug("No Content-Type provided in request") @@ -832,9 +827,14 @@ class Resource(wsgi.Application): # content type action_args = self.get_action_args(request.environ) action = action_args.pop('action', None) - content_type, body = self.get_body(request) - accept = request.best_match_content_type() - + # NOTE(sdague): we filter out InvalidContentTypes early so we + # know everything is good from here on out. + try: + content_type, body = self.get_body(request) + accept = request.best_match_content_type() + except exception.InvalidContentType: + msg = _("Unsupported Content-Type") + return Fault(webob.exc.HTTPUnsupportedMediaType(explanation=msg)) # NOTE(Vek): Splitting the function up this way allows for # auditing by external tools that wrap the existing # function. If we try to audit __call__(), we can diff --git a/cinder/tests/unit/api/openstack/test_wsgi.py b/cinder/tests/unit/api/openstack/test_wsgi.py index 6cb13574d90..570f75838a3 100644 --- a/cinder/tests/unit/api/openstack/test_wsgi.py +++ b/cinder/tests/unit/api/openstack/test_wsgi.py @@ -366,9 +366,8 @@ class ResourceTest(test.TestCase): request.headers['Content-Type'] = 'application/none' request.body = b'foo' - content_type, body = resource.get_body(request) - self.assertIsNone(content_type) - self.assertEqual('', body) + self.assertRaises(exception.InvalidContentType, + resource.get_body, request) def test_get_body_no_content_type(self): class Controller(object): diff --git a/releasenotes/notes/bug-invalid-content-type-1715094-8yu8i9w425ua08f3.yaml b/releasenotes/notes/bug-invalid-content-type-1715094-8yu8i9w425ua08f3.yaml new file mode 100644 index 00000000000..c9a7fd80c04 --- /dev/null +++ b/releasenotes/notes/bug-invalid-content-type-1715094-8yu8i9w425ua08f3.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Cinder now will return 415 (HTTPUnsupportedMediaType) when any unsupported + content type is specified in request header.