[Git][reproducible-builds/diffoscope][master] Handle error when encrypted archive file is exctracted. (Closes: #904685)

Chris Lamb gitlab at salsa.debian.org
Tue Aug 7 16:04:58 CEST 2018


Chris Lamb pushed to branch master at Reproducible Builds / diffoscope


Commits:
a6beb043 by Ricardo Gaviria at 2018-08-07T14:04:26Z
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 <lamby at debian.org>

- - - - -


4 changed files:

- diffoscope/comparators/zip.py
- tests/comparators/test_zip.py
- + tests/data/encrypted1.zip
- + tests/data/encrypted2.zip


Changes:

=====================================
diffoscope/comparators/zip.py
=====================================
--- a/diffoscope/comparators/zip.py
+++ b/diffoscope/comparators/zip.py
@@ -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)


=====================================
tests/comparators/test_zip.py
=====================================
--- a/tests/comparators/test_zip.py
+++ b/tests/comparators/test_zip.py
@@ -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


=====================================
tests/data/encrypted1.zip
=====================================
Binary files /dev/null and b/tests/data/encrypted1.zip differ


=====================================
tests/data/encrypted2.zip
=====================================
Binary files /dev/null and b/tests/data/encrypted2.zip differ



View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/commit/a6beb04368bce3f56993a7fd29313e2442db7b3b

-- 
View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/commit/a6beb04368bce3f56993a7fd29313e2442db7b3b
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.reproducible-builds.org/pipermail/rb-commits/attachments/20180807/6e24246a/attachment.html>


More information about the rb-commits mailing list