@@ -2110,8 +2110,6 @@ def test_make_zipfile_rootdir_nodir(self):
21102110 def check_unpack_archive (self , format , ** kwargs ):
21112111 self .check_unpack_archive_with_converter (
21122112 format , lambda path : path , ** kwargs )
2113- self .check_unpack_archive_with_converter (
2114- format , FakePath , ** kwargs )
21152113 self .check_unpack_archive_with_converter (format , FakePath , ** kwargs )
21162114
21172115 def check_unpack_archive_with_converter (self , format , converter , ** kwargs ):
@@ -2168,6 +2166,71 @@ def test_unpack_archive_zip(self):
21682166 with self .assertRaises (TypeError ):
21692167 self .check_unpack_archive ('zip' , filter = 'data' )
21702168
2169+ def test_unpack_archive_zip_badpaths (self ):
2170+ srcdir = self .mkdtemp ()
2171+ zipname = os .path .join (srcdir , 'test.zip' )
2172+ abspath = os .path .join (srcdir , 'abspath' )
2173+ with zipfile .ZipFile (zipname , 'w' ) as zf :
2174+ zf .writestr (abspath , 'badfile' )
2175+ zf .writestr (os .sep + abspath , 'badfile' )
2176+ zf .writestr ('/abspath' , 'badfile' )
2177+ zf .writestr ('C:/abspath' , 'badfile' )
2178+ zf .writestr ('D:\\ abspath' , 'badfile' )
2179+ zf .writestr ('E:abspath' , 'badfile' )
2180+ zf .writestr ('F:/G:/abspath' , 'badfile' )
2181+ zf .writestr ('//server/share/abspath' , 'badfile' )
2182+ zf .writestr ('\\ \\ server2\\ share\\ abspath' , 'badfile' )
2183+ zf .writestr ('../relpath' , 'badfile' )
2184+ zf .writestr (os .pardir + os .sep + 'relpath2' , 'badfile' )
2185+ zf .writestr ('good/file' , 'goodfile' )
2186+ zf .writestr ('good..file' , 'goodfile' )
2187+
2188+ dstdir = os .path .join (self .mkdtemp (), 'dst' )
2189+ unpack_archive (zipname , dstdir )
2190+ self .assertTrue (os .path .isfile (os .path .join (dstdir , 'good' , 'file' )))
2191+ self .assertTrue (os .path .isfile (os .path .join (dstdir , 'good..file' )))
2192+ self .assertFalse (os .path .exists (abspath ))
2193+ self .assertFalse (os .path .exists (os .path .join (dstdir , 'abspath' )))
2194+ self .assertFalse (os .path .exists (os .path .join (dstdir , 'G_' )))
2195+ self .assertFalse (os .path .exists (os .path .join (dstdir , 'server' )))
2196+ if os .name != 'nt' :
2197+ self .assertTrue (os .path .isfile (os .path .join (dstdir , 'C:' , 'abspath' )))
2198+ self .assertTrue (os .path .isfile (os .path .join (dstdir , 'D:\\ abspath' )))
2199+ self .assertTrue (os .path .isfile (os .path .join (dstdir , 'E:abspath' )))
2200+ self .assertTrue (os .path .isfile (os .path .join (dstdir , 'F:' , 'G:' , 'abspath' )))
2201+ self .assertTrue (os .path .isfile (os .path .join (dstdir , '\\ \\ server2\\ share\\ abspath' )))
2202+ if os .pardir == '..' :
2203+ self .assertFalse (os .path .exists (os .path .join (dstdir , '..' , 'relpath' )))
2204+ self .assertFalse (os .path .exists (os .path .join (dstdir , 'relpath' )))
2205+ else :
2206+ self .assertTrue (os .path .isfile (os .path .join (dstdir , '..' , 'relpath' )))
2207+ self .assertFalse (os .path .exists (os .path .join (dstdir , os .pardir , 'relpath2' )))
2208+ self .assertFalse (os .path .exists (os .path .join (dstdir , 'relpath2' )))
2209+
2210+ dstdir2 = os .path .join (self .mkdtemp (), 'dst' )
2211+ os .mkdir (dstdir2 )
2212+ with os_helper .change_cwd (dstdir2 ):
2213+ unpack_archive (zipname , '' )
2214+ self .assertTrue (os .path .isfile (os .path .join ('good' , 'file' )))
2215+ self .assertTrue (os .path .isfile ('good..file' ))
2216+ self .assertFalse (os .path .exists (abspath ))
2217+ self .assertFalse (os .path .exists ('abspath' ))
2218+ self .assertFalse (os .path .exists ('C_' ))
2219+ self .assertFalse (os .path .exists ('server' ))
2220+ if os .name != 'nt' :
2221+ self .assertTrue (os .path .isfile (os .path .join ('C:' , 'abspath' )))
2222+ self .assertTrue (os .path .isfile ('D:\\ abspath' ))
2223+ self .assertTrue (os .path .isfile ('E:abspath' ))
2224+ self .assertTrue (os .path .isfile (os .path .join ('F:' , 'G:' , 'abspath' )))
2225+ self .assertTrue (os .path .isfile ('\\ \\ server2\\ share\\ abspath' ))
2226+ if os .pardir == '..' :
2227+ self .assertFalse (os .path .exists (os .path .join ('..' , 'relpath' )))
2228+ self .assertFalse (os .path .exists ('relpath' ))
2229+ else :
2230+ self .assertTrue (os .path .isfile (os .path .join ('..' , 'relpath' )))
2231+ self .assertFalse (os .path .exists (os .path .join (os .pardir , 'relpath2' )))
2232+ self .assertFalse (os .path .exists ('relpath2' ))
2233+
21712234 def test_unpack_registry (self ):
21722235
21732236 formats = get_unpack_formats ()
0 commit comments