Skip to content
Snippets Groups Projects
Commit a6beb043 authored by Ricardo Gaviria's avatar Ricardo Gaviria Committed by Chris Lamb
Browse files

Handle error when encrypted archive file is exctracted. (Closes: #904685)


 - Adding encrypted zip test files and test case
 - Check wether archive is encrypted, and raises exception

Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent 1599b01d
No related branches found
No related tags found
No related merge requests found
Pipeline #15962 failed
......@@ -25,6 +25,7 @@ import zipfile
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from diffoscope.exc import ContainerExtractionError
from .utils.file import File
from .directory import Directory
......@@ -98,9 +99,18 @@ class ZipContainer(Archive):
# any weird character so we can get to the bytes.
targetpath = os.path.join(dest_dir, os.path.basename(member_name)).encode(
sys.getfilesystemencoding(), errors='replace')
with self.archive.open(member_name) as source, open(targetpath, 'wb') as target:
shutil.copyfileobj(source, target)
return targetpath.decode(sys.getfilesystemencoding())
try:
with self.archive.open(member_name) as source, \
open(targetpath, 'wb') as target:
shutil.copyfileobj(source, target)
return targetpath.decode(sys.getfilesystemencoding())
except RuntimeError as exc:
# Handle encrypted files see line 1292 of zipfile.py
is_encrypted = self.archive.getinfo(member_name).flag_bits & 0x1
if is_encrypted:
raise ContainerExtractionError(member_name, exc)
raise
def get_member(self, member_name):
zipinfo = self.archive.getinfo(member_name)
......
......@@ -29,6 +29,8 @@ from ..utils.nonexisting import assert_non_existing
zip1 = load_fixture('test1.zip')
zip2 = load_fixture('test2.zip')
zip3 = load_fixture('test3.zip')
encrypted_zip1 = load_fixture('encrypted1.zip')
encrypted_zip2 = load_fixture('encrypted2.zip')
mozzip1 = load_fixture('test1.mozzip')
mozzip2 = load_fixture('test2.mozzip')
......@@ -110,3 +112,8 @@ def test_mozzip_compressed_files(mozzip_differences):
@skip_unless_tools_exist('zipinfo')
def test_mozzip_compare_non_existing(monkeypatch, mozzip1):
assert_non_existing(monkeypatch, mozzip1)
def test_encrypted(encrypted_zip1, encrypted_zip2):
difference = encrypted_zip1.compare(encrypted_zip2)
assert difference is not None
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment