From 6b1ed75d7ac922f864816d3601ec40ac1b659e49 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Tue, 15 Mar 2022 10:58:32 +1100 Subject: [PATCH] Handle UnicodeEncodeErrors from junk URLs. --- www/conservancy/static/views.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/www/conservancy/static/views.py b/www/conservancy/static/views.py index 8029241d..ea0a6bdd 100644 --- a/www/conservancy/static/views.py +++ b/www/conservancy/static/views.py @@ -34,7 +34,12 @@ def index(request, *args, **kwargs): if path.endswith(u'/'): path += u'index.html' fullpath = os.path.join(STATIC_ROOT, path) - if not os.path.exists(fullpath): + try: + # Junk URLs in production (Python 3.5) are causing UnicodeEncodeErrors + # here. Can't reproduce in development in Python 3.9 - only Python 2.7. + if not os.path.exists(fullpath): + return handler404(request) + except UnicodeEncodeError: return handler404(request) content_type, _ = mimetypes.guess_type(path) if content_type != 'text/html':