From 20f2dfbc48a18d8cce8f95005d067752c0bef31f Mon Sep 17 00:00:00 2001 From: PeterAlfredLee Date: Sat, 15 Aug 2020 17:55:58 +0800 Subject: [PATCH] COMPRESS-546 : throw exception on corrputed z64 ZipArchiveInputStream should throw exception if a corrputed zip64 extra field is met. --- src/changes/changes.xml | 5 +++++ .../archivers/zip/ZipArchiveInputStream.java | 5 +++++ .../archivers/zip/ZipArchiveInputStreamTest.java | 9 +++++++++ src/test/resources/COMPRESS-546.zip | Bin 0 -> 77 bytes 4 files changed, 19 insertions(+) create mode 100644 src/test/resources/COMPRESS-546.zip diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ad224f7239f..73cf48cdcff 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -185,6 +185,11 @@ The type attribute can be add,update,fix,remove. allocation to avoid OOM when dealing some giant 7z archives. Github Pull Request #120. + + ZipArchiveInputStream should throw an exception if a corrputed + zip64 extra field is met. + diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index a0ace8de73c..66fd17b9161 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -429,6 +429,11 @@ private void processZip64Extra(final ZipLong size, final ZipLong cSize) throws Z if (!current.hasDataDescriptor) { if (z64 != null // same as current.usesZip64 but avoids NPE warning && (ZipLong.ZIP64_MAGIC.equals(cSize) || ZipLong.ZIP64_MAGIC.equals(size)) ) { + if (z64.getCompressedSize() == null || z64.getSize() == null) { + // avoid NPE if it's a corrupted zip archive + throw new ZipException("archive contains corrupted zip64 extra field"); + } + current.entry.setCompressedSize(z64.getCompressedSize().getLongValue()); current.entry.setSize(z64.getSize().getLongValue()); } else if (cSize != null && size != null) { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java index 9e47fcd3aad..23b695a2aaf 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java @@ -710,6 +710,15 @@ public void throwsIfThereIsNoEocd() throws Exception { }); } + @Test(expected = IOException.class) + public void throwsIOExceptionIfThereIsCorruptedZip64Extra() throws IOException { + try (InputStream fis = new FileInputStream(getFile("COMPRESS-546.zip")); + ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(fis);) { + while (zipInputStream.getNextZipEntry() != null) { + } + } + } + private static byte[] readEntry(final ZipArchiveInputStream zip, final ZipArchiveEntry zae) throws IOException { final int len = (int)zae.getSize(); final byte[] buff = new byte[len]; diff --git a/src/test/resources/COMPRESS-546.zip b/src/test/resources/COMPRESS-546.zip new file mode 100644 index 0000000000000000000000000000000000000000..0fad172e25efc7a8d781c9dc27d3b62ee28f90cf GIT binary patch literal 77 zcmWIWW@Zs#Xkg%A;E&iHzyJjQfj|XF^D`(g6sMMyWagz8>!lYZrsSlChVU}5%gddM WySE2|ODh-{7(tpC7_$D)VE_PLt{1Za literal 0 HcmV?d00001