Ticket #18004: 18004-multipartparser-r17813.diff

File 18004-multipartparser-r17813.diff, 3.9 KB (added by Tai Lee, 12 years ago)
  • django/http/multipartparser.py

     
    147147                transfer_encoding = meta_data.get('content-transfer-encoding')
    148148                if transfer_encoding is not None:
    149149                    transfer_encoding = transfer_encoding[0].strip()
    150                 field_name = force_unicode(field_name, encoding, errors='replace')
     150                field_name = force_unicode(field_name, encoding, errors='strict')
    151151
    152152                if item_type == FIELD:
    153153                    # This is a post field, we can just set it in the post
     
    161161                        data = field_stream.read()
    162162
    163163                    self._post.appendlist(field_name,
    164                                           force_unicode(data, encoding, errors='replace'))
     164                                          force_unicode(data, encoding, errors='strict'))
    165165                elif item_type == FILE:
    166166                    # This is a file, use the handler...
    167167                    file_name = disposition.get('filename')
    168168                    if not file_name:
    169169                        continue
    170                     file_name = force_unicode(file_name, encoding, errors='replace')
     170                    file_name = force_unicode(file_name, encoding, errors='strict')
    171171                    file_name = self.IE_sanitize(unescape_entities(file_name))
    172172
    173173                    content_type = meta_data.get('content-type', ('',))[0].strip()
     
    243243                # If it returns a file object, then set the files dict.
    244244                self._files.appendlist(force_unicode(old_field_name,
    245245                                                     self._encoding,
    246                                                      errors='replace'),
     246                                                     errors='strict'),
    247247                                       file_obj)
    248248                break
    249249
  • tests/regressiontests/file_uploads/tests.py

     
    1313from django.core.files.uploadedfile import SimpleUploadedFile
    1414from django.http.multipartparser import MultiPartParser
    1515from django.test import TestCase, client
    16 from django.utils import simplejson, unittest
     16from django.utils import encoding, simplejson, unittest
    1717
    1818from . import uploadhandler
    1919from .models import FileModel, temp_storage, UPLOAD_TO
     
    104104
    105105        self.assertEqual(response.status_code, 200)
    106106
     107    def test_non_utf_post_data(self):
     108        BIG5_STRING = u'test-0123456789_δΈ­ζ–‡.jpg'
     109
     110        tdir = tempfile.gettempdir()
     111
     112        # This file contains chinese symbols in the name.
     113        file1 = open(os.path.join(tdir, BIG5_STRING.encode('big5')), 'w+b')
     114        file1.write('b' * (2 ** 10))
     115        file1.seek(0)
     116
     117        self.assertRaises(
     118            encoding.DjangoUnicodeDecodeError,
     119            self.client.post,
     120            '/file_uploads/unicode_name/',
     121            {'file_unicode': file1}
     122        )
     123
     124        file1.close()
     125        try:
     126            os.unlink(file1.name)
     127        except:
     128            pass
     129
     130        self.assertRaises(
     131            encoding.DjangoUnicodeDecodeError,
     132            self.client.post,
     133            '/file_uploads/unicode_name/',
     134            {BIG5_STRING.encode('big5'): 'string data'}
     135        )
     136
     137        self.assertRaises(
     138            encoding.DjangoUnicodeDecodeError,
     139            self.client.post,
     140            '/file_uploads/unicode_name/',
     141            {'string data': BIG5_STRING.encode('big5')}
     142        )
     143
    107144    def test_dangerous_file_names(self):
    108145        """Uploaded file names should be sanitized before ever reaching the view."""
    109146        # This test simulates possible directory traversal attacks by a
Back to Top