Fix UnicodeDecodeError when decode API input

Convert UnicodeDecodeError to HTTPBadRequest in
FaultWrapper.

Change-Id: I826f05084b0a0ef170ef293d382868409b96ed3d
Closes-Bug: #1746202
This commit is contained in:
zhongjun 2018-01-31 11:02:44 +08:00 committed by zhongjun
parent a07d522970
commit bf19988984
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.