Skip to content

Commit fa94277

Browse files
committed
Added cleaning of the session request form's 'comments' field, to convert any html entered to text. Related to [17322].
- Legacy-Id: 17324 Note: SVN reference [17322] has been migrated to Git commit eb88abc
1 parent eb88abc commit fa94277

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

ietf/secr/sreq/forms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ietf.group.models import Group
1212
from ietf.meeting.models import ResourceAssociation
1313
from ietf.person.fields import SearchablePersonsField
14-
14+
from ietf.utils.html import clean_text_field
1515

1616
# -------------------------------------------------
1717
# Globals
@@ -145,6 +145,9 @@ def clean_conflict3(self):
145145
check_conflict(conflict, self.group)
146146
return conflict
147147

148+
def clean_comments(self):
149+
return clean_text_field(self.cleaned_data['comments'])
150+
148151
def clean(self):
149152
super(SessionForm, self).clean()
150153
data = self.cleaned_data

ietf/secr/sreq/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ def confirm(request, acronym):
289289
new_session = Session.objects.create(
290290
meeting=meeting,
291291
group=group,
292-
attendees=form.data['attendees'],
292+
attendees=form.cleaned_data['attendees'],
293293
requested_duration=datetime.timedelta(0,int(duration)),
294-
comments=form.data['comments'],
294+
comments=form.cleaned_data['comments'],
295295
type_id='regular',
296296
)
297297
SchedulingEvent.objects.create(

ietf/utils/html.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright The IETF Trust 2010-2019, All Rights Reserved
1+
# Copyright The IETF Trust 2010-2020, All Rights Reserved
22
# -*- coding: utf-8 -*-
33
# Taken from http://code.google.com/p/soclone/source/browse/trunk/soclone/utils/html.py
44
"""Utilities for working with HTML."""
@@ -8,15 +8,19 @@
88

99
import bleach
1010
import copy
11+
import html2text
1112
import lxml.etree
1213
import lxml.html
1314
import lxml.html.clean
1415
import six
1516

1617
import debug # pyflakes:ignore
1718

19+
from django import forms
1820
from django.utils.functional import keep_lazy
1921

22+
from ietf.utils.mime import get_mime_type
23+
2024
acceptable_tags = ('a', 'abbr', 'acronym', 'address', 'b', 'big',
2125
'blockquote', 'body', 'br', 'caption', 'center', 'cite', 'code', 'col',
2226
'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'font',
@@ -76,3 +80,18 @@ def clean_html(self, html):
7680

7781
def sanitize_document(html):
7882
return lxml_cleaner.clean_html(html)
83+
84+
85+
# ----------------------------------------------------------------------
86+
# Text field cleaning
87+
88+
def clean_text_field(text):
89+
mime_type, encoding = get_mime_type(text.encode('utf8'))
90+
if mime_type == 'text/html': # or re.search(r'<\w+>', text):
91+
text = html2text.html2text(text)
92+
elif mime_type in ['text/plain', 'application/x-empty', ]:
93+
pass
94+
else:
95+
raise forms.ValidationError("Unexpected text field mime type: %s" % mime_type)
96+
return text
97+

ietf/utils/mime.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright The IETF Trust 2020, All Rights Reserved
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import absolute_import, print_function, unicode_literals
5+
6+
import magic
7+
8+
def get_mime_type(content):
9+
# try to fixup encoding
10+
if hasattr(magic, "open"):
11+
m = magic.open(magic.MAGIC_MIME)
12+
m.load()
13+
filetype = m.buffer(content)
14+
else:
15+
m = magic.Magic()
16+
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
17+
magic.magic_load(m.cookie, None)
18+
filetype = m.from_buffer(content)
19+
20+
return filetype.split('; ', 1)
21+

ietf/utils/validators.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# Copyright The IETF Trust 2016-2019, All Rights Reserved
1+
# Copyright The IETF Trust 2016-2020, All Rights Reserved
22
# -*- coding: utf-8 -*-
33

4-
54
from __future__ import absolute_import, print_function, unicode_literals
65

76
import os
87
import re
9-
import magic
108
from pyquery import PyQuery
119

1210
from django.conf import settings
@@ -17,6 +15,8 @@
1715

1816
import debug # pyflakes:ignore
1917

18+
from ietf.utils.mime import get_mime_type
19+
2020
# Note that this is an instantiation of the regex validator, _not_ the
2121
# regex-string validator defined right below
2222
validate_no_control_chars = RegexValidator(
@@ -55,20 +55,6 @@ def __ne__(self, other):
5555

5656
validate_regular_expression_string = RegexStringValidator()
5757

58-
def get_mime_type(content):
59-
# try to fixup encoding
60-
if hasattr(magic, "open"):
61-
m = magic.open(magic.MAGIC_MIME)
62-
m.load()
63-
filetype = m.buffer(content)
64-
else:
65-
m = magic.Magic()
66-
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
67-
magic.magic_load(m.cookie, None)
68-
filetype = m.from_buffer(content)
69-
70-
return filetype.split('; ', 1)
71-
7258
def validate_file_size(file):
7359
if file._size > settings.SECR_MAX_UPLOAD_SIZE:
7460
raise ValidationError('Please keep filesize under %s. Requested upload size was %s' % (filesizeformat(settings.SECR_MAX_UPLOAD_SIZE), filesizeformat(file._size)))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ factory-boy>=2.9.0
2828
google-api-python-client
2929
Faker>=0.8.8,!=0.8.9,!=0.8.10 # from factory-boy # Faker 0.8.9,0.8.10 sometimes return string names instead of unicode.
3030
hashids>=1.1.0
31+
html2text>=2019.8.11
3132
html5lib>=1.0.1
3233
httplib2>=0.10.3
3334
# jsonfield 3.x and higher requires Django 2.2 or higher

requirements3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ factory-boy>=2.9.0
2929
google-api-python-client
3030
Faker>=0.8.8,!=0.8.9,!=0.8.10 # from factory-boy # Faker 0.8.9,0.8.10 sometimes return string names instead of unicode.
3131
hashids>=1.1.0
32+
html2text>=2019.8.11
3233
html5lib>=1.0.1
3334
httplib2>=0.10.3
3435
# jsonfield 3.x and higher requires Django 2.2 or higher

0 commit comments

Comments
 (0)