Skip to content

Commit 3c82dc6

Browse files
committed
Merged in [16922] from sasha@dashcare.nl:
Fix ietf-tools#2584 - Add additional content validation for uploaded texts. Permitted MIME types are now text/plain, text/markdown and text/x-rst. This applies to all usages of get_cleaned_text_file_content(), including reviews, but also other similar places where text can either be written either into a textarea or uploaded. - Legacy-Id: 16930 Note: SVN reference [16922] has been migrated to Git commit fd53f98
2 parents a597f32 + fd53f98 commit 3c82dc6

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

ietf/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ def skip_unreadable_post(record):
678678
"bluesheets": "https://www.ietf.org/proceedings/{meeting.number}/bluesheets/{doc.uploaded_filename}",
679679
}
680680

681+
# Valid MIME types for cases where text is uploaded and immediately extracted,
682+
# e.g. a charter or a review. Must be a tuple, not a list.
683+
DOC_TEXT_FILE_VALID_UPLOAD_MIME_TYPES = ('text/plain', 'text/markdown', 'text/x-rst')
684+
681685
# Override this in settings_local.py if needed
682686
CACHE_MIDDLEWARE_SECONDS = 300
683687
CACHE_MIDDLEWARE_KEY_PREFIX = ''

ietf/utils/test_textupload.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright The IETF Trust 2015-2019, All Rights Reserved
2+
# -*- coding: utf-8 -*-
3+
4+
5+
from __future__ import absolute_import, print_function, unicode_literals
6+
7+
from django.core.exceptions import ValidationError
8+
from django.core.files.uploadedfile import SimpleUploadedFile
9+
10+
from .textupload import get_cleaned_text_file_content
11+
from ietf.utils.test_utils import TestCase
12+
13+
14+
class GetCleanedTextFileContentTest(TestCase):
15+
def test_no_file(self):
16+
self.assertEqual(get_cleaned_text_file_content(None), "")
17+
18+
def test_valid_file(self):
19+
data = 'testing 👾'
20+
uploaded_file = SimpleUploadedFile('data.txt', data.encode('utf-8'))
21+
self.assertEqual(get_cleaned_text_file_content(uploaded_file), data)
22+
23+
def test_invalid_mime_type_gif(self):
24+
data = 'GIF89a;'
25+
uploaded_file = SimpleUploadedFile('data.txt', data.encode('utf-8'))
26+
with self.assertRaises(ValidationError) as context:
27+
get_cleaned_text_file_content(uploaded_file)
28+
self.assertIn('does not appear to be a text file', context.exception.message)
29+
self.assertIn('image/gif', context.exception.message)
30+
31+
def test_invalid_mime_type_rst(self):
32+
data = r'{\rtf1}'
33+
uploaded_file = SimpleUploadedFile('data.txt', data.encode('utf-8'))
34+
with self.assertRaises(ValidationError) as context:
35+
get_cleaned_text_file_content(uploaded_file)
36+
self.assertIn('does not appear to be a text file', context.exception.message)
37+
self.assertIn('text/rtf', context.exception.message)

ietf/utils/textupload.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import re
88

9+
from django.conf import settings
910
from django.core.exceptions import ValidationError
1011

1112
import debug # pyflakes:ignore
@@ -36,8 +37,10 @@ def get_cleaned_text_file_content(uploaded_file):
3637
magic.magic_load(m.cookie, None)
3738
filetype = m.from_buffer(content)
3839

39-
if not filetype.startswith("text"):
40-
raise ValidationError("Uploaded file does not appear to be a text file.")
40+
if not filetype.startswith(settings.DOC_TEXT_FILE_VALID_UPLOAD_MIME_TYPES):
41+
raise ValidationError("Uploaded file does not appear to be a text file. "
42+
"Permitted MIME types are {}, this file is {}"
43+
.format(', '.join(settings.DOC_TEXT_FILE_VALID_UPLOAD_MIME_TYPES), filetype))
4144

4245
match = re.search(r"charset=([\w-]+)", filetype)
4346
if not match:

0 commit comments

Comments
 (0)