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