From 017f5981783936dacb5455c481743ca8187efb0e Mon Sep 17 00:00:00 2001 From: Phillip Baker Date: Fri, 28 Aug 2020 11:56:09 -0400 Subject: Add graceful fallback for invalid character encoding For context see https://github.com/Pylons/webob/issues/161 --- routes/middleware.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/routes/middleware.py b/routes/middleware.py index ba51ae3..4ee47da 100644 --- a/routes/middleware.py +++ b/routes/middleware.py @@ -61,9 +61,15 @@ class RoutesMiddleware(object): if '_method' in qs: req = Request(environ) req.errors = 'ignore' - if '_method' in req.GET: + + try: + method = req.GET.get('_method') + except UnicodeDecodeError: + method = None + + if method: old_method = environ['REQUEST_METHOD'] - environ['REQUEST_METHOD'] = req.GET['_method'].upper() + environ['REQUEST_METHOD'] = method.upper() if self.log_debug: log.debug("_method found in QUERY_STRING, altering " "request method to %s", @@ -72,9 +78,15 @@ class RoutesMiddleware(object): if req is None: req = Request(environ) req.errors = 'ignore' - if '_method' in req.POST: + + try: + method = req.POST.get('_method') + except UnicodeDecodeError: + method = None + + if method: old_method = environ['REQUEST_METHOD'] - environ['REQUEST_METHOD'] = req.POST['_method'].upper() + environ['REQUEST_METHOD'] = method.upper() if self.log_debug: log.debug("_method found in POST data, altering " "request method to %s", -- cgit v1.2.1