]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Non-json body on POST 500's
authorJohn Perkins <john.perkins@rackspace.com>
Wed, 8 Apr 2015 17:24:03 +0000 (12:24 -0500)
committerJohn Perkins <john.perkins@rackspace.com>
Thu, 9 Apr 2015 16:42:29 +0000 (11:42 -0500)
If the body of a POST request is not json, we get crashes.
This can happen when middleware sends along unexpected data.

Closes-bug #1441879

Change-Id: Ifac59476e4785b86bca6e2a54759f4271629a193

neutron/api/v2/base.py
neutron/tests/unit/api/v2/test_base.py

index f0ab4e6f70ac2407ca2fda83d2aa6b61aabba5da..4e795cf2a75f04c6e414bfd5f919f9d3a8c3f464 100644 (file)
@@ -595,21 +595,24 @@ class Controller(object):
             raise webob.exc.HTTPBadRequest(_("Resource body required"))
 
         LOG.debug("Request body: %(body)s", {'body': body})
-        if collection in body:
-            if not allow_bulk:
-                raise webob.exc.HTTPBadRequest(_("Bulk operation "
-                                                 "not supported"))
-            if not body[collection]:
-                raise webob.exc.HTTPBadRequest(_("Resources required"))
-            bulk_body = [
-                Controller.prepare_request_body(
-                    context, item if resource in item else {resource: item},
-                    is_create, resource, attr_info, allow_bulk
-                ) for item in body[collection]
-            ]
-            return {collection: bulk_body}
-
-        res_dict = body.get(resource)
+        try:
+            if collection in body:
+                if not allow_bulk:
+                    raise webob.exc.HTTPBadRequest(_("Bulk operation "
+                                                     "not supported"))
+                if not body[collection]:
+                    raise webob.exc.HTTPBadRequest(_("Resources required"))
+                bulk_body = [
+                    Controller.prepare_request_body(
+                        context, item if resource in item
+                        else {resource: item}, is_create, resource, attr_info,
+                        allow_bulk) for item in body[collection]
+                ]
+                return {collection: bulk_body}
+            res_dict = body.get(resource)
+        except (AttributeError, TypeError):
+            msg = _("Body contains invalid data")
+            raise webob.exc.HTTPBadRequest(msg)
         if res_dict is None:
             msg = _("Unable to find '%s' in request body") % resource
             raise webob.exc.HTTPBadRequest(msg)
index 6630781fe11dc1a5beacae74a14323071ef32ee2..ed9d505030ac299901595eb14318676f521d8374 100644 (file)
@@ -830,6 +830,14 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase):
         data = {'whoa': None}
         self._test_create_failure_bad_request('networks', data)
 
+    def test_create_body_string_not_json(self):
+        data = 'a string'
+        self._test_create_failure_bad_request('networks', data)
+
+    def test_create_body_boolean_not_json(self):
+        data = True
+        self._test_create_failure_bad_request('networks', data)
+
     def test_create_no_resource(self):
         data = {}
         self._test_create_failure_bad_request('networks', data)