Ticket #18150: patch_3_15_15.diff

File patch_3_15_15.diff, 3.7 KB (added by Vignesh Sarma K, 10 years ago)

Should apply cleanly against master now

  • django/core/files/uploadedfile.py

    diff --git a/django/core/files/uploadedfile.py b/django/core/files/uploadedfile.py
    index 20eaece..81dce38 100644
    a b Classes representing uploaded files.  
    33"""
    44
    55import errno
    6 import os
     6import ntpath
    77from io import BytesIO
    88
    99from django.conf import settings
    class UploadedFile(File):  
    4242    def _set_name(self, name):
    4343        # Sanitize the file name so that it can't be dangerous.
    4444        if name is not None:
     45            # If name ends in backslash replace with 0
     46            if name[-1] == "\\":
     47                name = name[:-1] + "0"
    4548            # Just use the basename of the file -- anything else is dangerous.
    46             name = os.path.basename(name)
     49            name = ntpath.basename(name)
    4750
    4851            # File names longer than 255 characters can cause problems on older OSes.
    4952            if len(name) > 255:
    50                 name, ext = os.path.splitext(name)
     53                name, ext = ntpath.splitext(name)
    5154                ext = ext[:255]
    5255                name = name[:255 - len(ext)] + ext
    5356
  • django/http/multipartparser.py

    diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
    index 53e4149..474619e 100644
    a b class MultiPartParser(object):  
    184184                    if not file_name:
    185185                        continue
    186186                    file_name = force_text(file_name, encoding, errors='replace')
    187                     file_name = self.IE_sanitize(unescape_entities(file_name))
     187                    file_name = unescape_entities(file_name)
    188188
    189189                    content_type, content_type_extra = meta_data.get('content-type', ('', {}))
    190190                    content_type = content_type.strip()
    class MultiPartParser(object):  
    274274                    file_obj)
    275275                break
    276276
    277     def IE_sanitize(self, filename):
    278         """Cleanup filename from Internet Explorer full paths."""
    279         return filename and filename[filename.rfind("\\") + 1:].strip()
    280 
    281277    def _close_files(self):
    282278        # Free up all file handles.
    283279        # FIXME: this currently assumes that upload handlers store the file as 'file'
  • tests/file_uploads/tests.py

    diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py
    old mode 100644
    new mode 100755
    index 91b959b..9a5680d
    a b  
    11#! -*- coding: utf-8 -*-
     2# coding: utf-8
     3
    24from __future__ import unicode_literals
    35
    46import base64
    class FileUploadTests(TestCase):  
    507509        # shouldn't differ.
    508510        self.assertEqual(os.path.basename(obj.testfile.path), 'MiXeD_cAsE.txt')
    509511
     512    def test_fail_backslash(self):
     513        """
     514        Tests filename ending with a backslash, issue #18150 reports crashes
     515        when a filename ends with a backslash
     516        """
     517        name_backslash = "backslash.jpg\\"
     518        payload = client.FakePayload()
     519        payload.write('\r\n'.join([
     520            '--' + client.BOUNDARY,
     521            'Content-Disposition: form-data; name="file1"; filename="%s"' % name_backslash,
     522            'Content-Type: application/octet-stream',
     523            '',
     524            ''
     525        ]))
     526        payload.write('\r\n--' + client.BOUNDARY + '--\r\n')
     527
     528        r = {
     529            'CONTENT_LENGTH': len(payload),
     530            'CONTENT_TYPE':   client.MULTIPART_CONTENT,
     531            'PATH_INFO':      "/file_uploads/echo/",
     532            'REQUEST_METHOD': 'POST',
     533            'wsgi.input':     payload,
     534        }
     535        response = self.client.request(**r)
     536        self.assertEqual(response.status_code, 200)
     537
    510538
    511539@override_settings(MEDIA_ROOT=MEDIA_ROOT)
    512540class DirectoryCreationTests(TestCase):
Back to Top