Merge "Fix UnicodeDecodeError when decode API input"

This commit is contained in:
Zuul 2018-02-07 15:58:03 +00:00 committed by Gerrit Code Review
commit b7e2c0d4b3
3 changed files with 20 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import webob.dec
import webob.exc
from manila.api.openstack import wsgi
from manila.i18n import _
from manila import utils
from manila.wsgi import common as base_wsgi
@ -40,6 +41,11 @@ class FaultWrapper(base_wsgi.Middleware):
status, webob.exc.HTTPInternalServerError)()
def _error(self, inner, req):
if isinstance(inner, UnicodeDecodeError):
msg = _("Error decoding your request. Either the URL or the "
"request body contained characters that could not be "
"decoded by Manila.")
return wsgi.Fault(webob.exc.HTTPBadRequest(explanation=msg))
LOG.exception("Caught error: %s", inner)
safe = getattr(inner, 'safe', False)

View File

@ -184,3 +184,12 @@ class ExceptionTest(test.TestCase):
api = self._wsgi_app(fail)
resp = webob.Request.blank('/').get_response(api)
self.assertEqual(500, resp.status_int)
def test_validate_request_unicode_decode_fault(self):
@webob.dec.wsgify
def unicode_error(req):
raise UnicodeDecodeError("ascii", "test".encode(), 0, 1, "bad")
api = self._wsgi_app(unicode_error)
resp = webob.Request.blank('/test?foo=%88').get_response(api)
self.assertEqual(400, resp.status_int)

View File

@ -0,0 +1,5 @@
---
fixes:
- This patch converts UnicodeDecodeError exception into BadRequest, plus
an explicit error message. Fix invalid query parameter could lead to
HTTP 500.