Skip to content

Commit d467cd7

Browse files
committed
Strip bad fields for local entry
1 parent ac27728 commit d467cd7

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

Lib/test/test_zipfile/test_core.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,28 @@ def test_generated_valid_zip64_extra_in_local_entry(self):
12171217
b'zzz'
12181218
))
12191219

1220+
# should strip zip64 field if zip64 not used
1221+
fh = io.BytesIO()
1222+
with zipfile.ZipFile(fh, 'w') as zh:
1223+
zinfo = zipfile.ZipInfo('strfile')
1224+
zinfo.extra = (
1225+
# zip64 (should be stripped)
1226+
b'\x01\x00\x10\x00'
1227+
b'\x00\x00\x00\x00\x00\x00\x00\x00'
1228+
b'\x00\x00\x00\x00\x00\x00\x00\x00'
1229+
1230+
# invalid tail
1231+
b'zzz'
1232+
)
1233+
zh.writestr(zinfo, 'foo')
1234+
1235+
fh.seek(0)
1236+
fh.seek(zinfo.header_offset)
1237+
entry = fh.read(zh.start_dir - zinfo.header_offset - zinfo.compress_size)
1238+
header = struct.unpack_from(zipfile.structFileHeader, entry)
1239+
extra_bytes = entry[-header[zipfile._FH_EXTRA_FIELD_LENGTH]:]
1240+
self.assertEqual(extra_bytes, b'zzz')
1241+
12201242
def test_force_zip64(self):
12211243
"""Test that forcing zip64 extensions correctly notes this in the zip file"""
12221244

Lib/zipfile/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,14 @@ def FileHeader(self, zip64=None):
541541
compress_size = self.compress_size
542542
file_size = self.file_size
543543

544-
extra = self.extra
544+
# Strip extra fields that should not exist in the local entry or should
545+
# be determined later. (self.extra can be read from central directory
546+
# when read in append mode)
547+
extra = _Extra.strip(self.extra, (
548+
0x0001, # Zip64
549+
0x0017, # Strong Encryption Header
550+
0x6375, # Unicode Comment
551+
))
545552

546553
min_version = 0
547554
if zip64 is None:
@@ -555,7 +562,7 @@ def FileHeader(self, zip64=None):
555562
extra = struct.pack(
556563
fmt, 1, struct.calcsize(fmt)-4,
557564
file_size, compress_size
558-
) + _Extra.strip(extra, (1,))
565+
) + extra
559566

560567
file_size = 0xffffffff
561568
compress_size = 0xffffffff

0 commit comments

Comments
 (0)