Skip to content

Commit b2da91d

Browse files
committed
Added a middleware exception handler for the case of people submitting unicode outside the Basic Multilingual Plane, which cannot currently be saved to the database.
- Legacy-Id: 13508
1 parent 96c5b2b commit b2da91d

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

ietf/middleware.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

33
from django.db import connection
4+
from django.db.utils import OperationalError
45
from django.shortcuts import render
56
from django.http import HttpResponsePermanentRedirect
6-
from ietf.utils.log import log
7+
from ietf.utils.log import log, exception_components
78
from ietf.utils.mail import log_smtp_exception
89
import re
910
import smtplib
@@ -31,6 +32,20 @@ def process_exception(self, request, exception):
3132
{'exception': extype, 'args': value, 'traceback': "".join(tb)} )
3233
return None
3334

35+
class Utf8ExceptionMiddleware(object):
36+
def __init__(self, get_response):
37+
self.get_response = get_response
38+
def __call__(self, request):
39+
return self.get_response(request)
40+
def process_exception(self, request, exception):
41+
if isinstance(exception, OperationalError):
42+
extype, value, tb = exception_components(exception)
43+
if value[0] == 1366:
44+
log("Database 4-byte utf8 exception: %s: %s" % (extype, value))
45+
return render(request, 'utf8_4byte_failed.html',
46+
{'exception': extype, 'args': value, 'traceback': "".join(tb)} )
47+
return None
48+
3449
def redirect_trailing_period_middleware(get_response):
3550
def redirect_trailing_period(request):
3651
response = get_response(request)
@@ -53,4 +68,4 @@ def unicode_nfkc_normalization(request):
5368
response = get_response(request)
5469
return response
5570
return unicode_nfkc_normalization
56-
71+

ietf/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def skip_unreadable_post(record):
341341
'django.middleware.http.ConditionalGetMiddleware',
342342
'ietf.middleware.sql_log_middleware',
343343
'ietf.middleware.SMTPExceptionMiddleware',
344+
'ietf.middleware.Utf8ExceptionMiddleware',
344345
'ietf.middleware.redirect_trailing_period_middleware',
345346
'django.middleware.clickjacking.XFrameOptionsMiddleware',
346347
'ietf.middleware.unicode_nfkc_normalization_middleware',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% extends "base.html" %}
2+
{# Copyright The IETF Trust 2015, All Rights Reserved #}
3+
{% load origin %}
4+
5+
{% block title %}Unicode outside the Basic Multilingual Plane{% endblock %}
6+
7+
{% block content %}
8+
{% origin %}
9+
<h1>Unicode outside the Basic Multilingual Plane</h1>
10+
11+
<p class="alert alert-warning">
12+
13+
You submitted data containing unicode characters outside the Basic
14+
Multilingual Plane. Such character cannot be stored in the database at
15+
this time. If this causes inconvenience, please send an e-mail with
16+
details to <a href="mailto:webmaster@ietf.org">webmaster@ietf.org</a>, in
17+
order for us to understand the issue.
18+
19+
</p>
20+
21+
{% endblock %}

ietf/utils/log.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

3+
import sys
34
import logging
45
import inspect
56
import os.path
7+
import traceback
68

79
try:
810
import syslog
@@ -49,9 +51,15 @@ def log(msg):
4951
file, line, where = "/<UNKNOWN>", 0, ""
5052
logfunc("ietf%s(%d)%s: %s" % (file, line, where, msg))
5153

54+
logger = logging.getLogger('django')
5255

5356

54-
logger = logging.getLogger('django')
57+
58+
def exception_components(e):
59+
extype = sys.exc_info()[0]
60+
value = sys.exc_info()[1]
61+
tb = traceback.format_tb(sys.exc_info()[2])
62+
return (extype, value, tb)
5563

5664
def build_traceback(stack):
5765
"""

0 commit comments

Comments
 (0)