Django

Code

Changeset 8096

Show
Ignore:
Timestamp:
07/26/08 17:48:51 (4 months ago)
Author:
mtredinnick
Message:

Fixed #7658 -- Added some Windows-specific tempfile handling. The standard
stuff doesn't work with the way Django's file uploading code wants to operate.
Patch from Mike Axiak.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/files/uploadedfile.py

    r7908 r8096  
    44 
    55import os 
    6 import tempfile 
    76import warnings 
    87try: 
     
    1211 
    1312from django.conf import settings 
     13 
     14from django.core.files import temp as tempfile 
    1415 
    1516__all__ = ('UploadedFile', 'TemporaryUploadedFile', 'InMemoryUploadedFile', 'SimpleUploadedFile') 
  • django/trunk/tests/regressiontests/file_uploads/tests.py

    r8007 r8096  
    33import sha 
    44import shutil 
    5 import tempfile 
    65import unittest 
    76 
     7from django.core.files import temp as tempfile 
    88from django.core.files.uploadedfile import SimpleUploadedFile 
    99from django.test import TestCase, client 
     
    2323    def test_large_upload(self): 
    2424        tdir = tempfile.gettempdir() 
    25          
     25 
    2626        file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) 
    2727        file1.write('a' * (2 ** 21)) 
     
    5959 
    6060        self.assertEqual(response.status_code, 200) 
    61      
     61 
    6262    def test_dangerous_file_names(self): 
    6363        """Uploaded file names should be sanitized before ever reaching the view.""" 
    6464        # This test simulates possible directory traversal attacks by a 
    65         # malicious uploader We have to do some monkeybusiness here to construct  
     65        # malicious uploader We have to do some monkeybusiness here to construct 
    6666        # a malicious payload with an invalid file name (containing os.sep or 
    6767        # os.pardir). This similar to what an attacker would need to do when 
     
    8080            "../..\\hax0rd.txt"         # Relative path, mixed. 
    8181        ] 
    82          
     82 
    8383        payload = [] 
    8484        for i, name in enumerate(scary_file_names): 
     
    9494            '', 
    9595        ]) 
    96          
     96 
    9797        payload = "\r\n".join(payload) 
    9898        r = { 
     
    110110            got = recieved["file%s" % i] 
    111111            self.assertEqual(got, "hax0rd.txt") 
    112              
     112 
    113113    def test_filename_overflow(self): 
    114114        """File names over 256 characters (dangerous on some platforms) get fixed up.""" 
     
    132132        got = simplejson.loads(self.client.request(**r).content) 
    133133        self.assert_(len(got['file']) < 256, "Got a long file name (%s characters)." % len(got['file'])) 
    134          
     134 
    135135    def test_custom_upload_handler(self): 
    136         # A small file (under the 5M quota)                 
     136        # A small file (under the 5M quota) 
    137137        smallfile = tempfile.NamedTemporaryFile() 
    138138        smallfile.write('a' * (2 ** 21)) 
     
    141141        bigfile = tempfile.NamedTemporaryFile() 
    142142        bigfile.write('a' * (10 * 2 ** 20)) 
    143                  
     143 
    144144        # Small file posting should work. 
    145145        response = self.client.post('/file_uploads/quota/', {'f': open(smallfile.name)}) 
    146146        got = simplejson.loads(response.content) 
    147147        self.assert_('f' in got) 
    148          
     148 
    149149        # Large files don't go through. 
    150150        response = self.client.post("/file_uploads/quota/", {'f': open(bigfile.name)}) 
    151151        got = simplejson.loads(response.content) 
    152152        self.assert_('f' not in got) 
    153          
     153 
    154154    def test_broken_custom_upload_handler(self): 
    155155        f = tempfile.NamedTemporaryFile() 
     
    190190class DirectoryCreationTests(unittest.TestCase): 
    191191    """ 
    192     Tests for error handling during directory creation  
     192    Tests for error handling during directory creation 
    193193    via _save_FIELD_file (ticket #6450) 
    194194    """ 
     
    222222            # The test needs to be done on a specific string as IOError 
    223223            # is raised even without the patch (just not early enough) 
    224             self.assertEquals(err.args[0],  
     224            self.assertEquals(err.args[0], 
    225225                              "%s exists and is not a directory" % UPLOAD_TO) 
    226226        except: