This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 678e18988f9bc67046cf3b288cde3d9ceaf0356a
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Dec 3 12:32:58 2026 -0500

    Test and document ZIP filename validation limits (f010)
    
    Add tests for identical raw filenames that resolve differently because
    of Unicode path fields present only in the central directory or
    mismatched EFS flags.
    
    Document the comparison limits and recommend validating the final name
    on the same ZipFile entry used to read its data.
---
 src/site/xdoc/zip.xml                              | 31 ++++++++
 .../compress/archivers/zip/ZipFileTest.java        | 82 ++++++++++++++++++++++
 2 files changed, 113 insertions(+)

diff --git a/src/site/xdoc/zip.xml b/src/site/xdoc/zip.xml
index a22417aff..44c4df5cc 100644
--- a/src/site/xdoc/zip.xml
+++ b/src/site/xdoc/zip.xml
@@ -92,6 +92,37 @@
           first and provide correct and complete information on any
           ZIP archive.</p>
 
+        <p>The readers can also resolve different names for the same
+          entry. <code>ZipArchiveInputStream</code> reads names and
+          encoding information from local file headers, while
+          <code>ZipFile</code> reads names and encoding flags from the
+          central directory and can also use local extra fields.
+          Even when the raw filename bytes match, a Unicode path
+          extra field present only in the central directory or different
+          language encoding flags (EFS) can cause different resolved
+          names.</p>
+
+        <p>Since 1.29.0,
+          
<code>ZipFile.Builder.setRejectLocalFileHeaderNameDivergence(true)</code>
+          rejects entries whose local header and central directory
+          contain different raw filename bytes. Rejection is disabled
+          by default. This check does not compare encoding flags,
+          Unicode extra fields, or resolved names. The entry method
+          <code>isLocalFileHeaderNameDivergent()</code> reports whether
+          a raw filename mismatch was detected; <code>false</code>
+          does not establish that different readers resolve the same
+          name. Setting <code>ignoreLocalFileHeader</code> to
+          <code>true</code> disables this check.</p>
+
+        <p>Applications that make decisions based on entry names should
+          validate the final <code>ZipArchiveEntry.getName()</code>
+          returned by the reader used to consume the entry. With
+          <code>ZipFile</code>, validate the entry obtained from that
+          instance and pass the same entry to its
+          <code>getInputStream(entry)</code> method. Validating names
+          with one reader and extracting with another does not
+          establish which names will be used during extraction.</p>
+
         <p>ZIP archives know a feature called the data descriptor
           which is a way to store an entry's length after the entry's
           data.  This can only work reliably if the size information
diff --git 
a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java 
b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
index 1caa621ea..c9aee8af2 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
@@ -125,6 +125,44 @@ private static byte[] createZip64Archive() throws 
IOException {
         return bos.toByteArray();
     }
 
+    private static byte[] createZipWithMatchingRawNames(final String name, 
final int localFlags, final int centralFlags,
+            final ZipExtraField... centralExtraFields) throws IOException {
+        // One empty STORED entry, with independently specified header flags 
and central-directory-only extra fields.
+        final byte[] rawName = name.getBytes(UTF_8);
+        final byte[] centralExtraData = 
ExtraFieldUtils.mergeCentralDirectoryData(centralExtraFields);
+        final byte[] localHeader = new byte[30];
+        System.arraycopy(ZipArchiveOutputStream.LFH_SIG, 0, localHeader, 0, 
ZipConstants.WORD);
+        ZipShort.putShort(20, localHeader, 4); // Version needed to extract.
+        ZipShort.putShort(localFlags, localHeader, 6);
+        ZipLong.putLong(0x00210000, localHeader, 10); // January 1, 1980, in 
DOS format.
+        ZipShort.putShort(rawName.length, localHeader, 26);
+
+        final byte[] centralHeader = new byte[46];
+        System.arraycopy(ZipArchiveOutputStream.CFH_SIG, 0, centralHeader, 0, 
ZipConstants.WORD);
+        ZipShort.putShort(20, centralHeader, 4); // Version made by.
+        ZipShort.putShort(20, centralHeader, 6); // Version needed to extract.
+        ZipShort.putShort(centralFlags, centralHeader, 8);
+        ZipLong.putLong(0x00210000, centralHeader, 12);
+        ZipShort.putShort(rawName.length, centralHeader, 28);
+        ZipShort.putShort(centralExtraData.length, centralHeader, 30);
+
+        final byte[] endOfCentralDirectory = new byte[22];
+        System.arraycopy(ZipArchiveOutputStream.EOCD_SIG, 0, 
endOfCentralDirectory, 0, ZipConstants.WORD);
+        ZipShort.putShort(1, endOfCentralDirectory, 8); // Entries on this 
disk.
+        ZipShort.putShort(1, endOfCentralDirectory, 10); // Total entries.
+        ZipLong.putLong(centralHeader.length + rawName.length + 
centralExtraData.length, endOfCentralDirectory, 12);
+        ZipLong.putLong(localHeader.length + rawName.length, 
endOfCentralDirectory, 16);
+
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        bos.write(localHeader);
+        bos.write(rawName);
+        bos.write(centralHeader);
+        bos.write(rawName);
+        bos.write(centralExtraData);
+        bos.write(endOfCentralDirectory);
+        return bos.toByteArray();
+    }
+
     private static byte[] createZipWithUnicodeExtraFields(final boolean 
divergentLocalFileHeaderName) throws IOException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
         try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(bos)) {
@@ -1130,6 +1168,50 @@ void 
testRejectDivergentLocalFileHeaderNameAcceptsMatchingNames() throws IOExcep
         }
     }
 
+    @Test
+    void testRejectDivergentLocalFileHeaderNameAllowsDifferentEfsFlags() 
throws IOException {
+        final String name = "\u00e9.txt";
+        final int utf8Flag = 1 << 11;
+        final byte[] data = createZipWithMatchingRawNames(name, 0, utf8Flag);
+        try (ZipFile zipFile = 
ZipFile.builder().setByteArray(data).setCharset(StandardCharsets.ISO_8859_1)
+                .setRejectLocalFileHeaderNameDivergence(true).get();
+                ZipArchiveInputStream stream = 
ZipArchiveInputStream.builder().setByteArray(data).setCharset(StandardCharsets.ISO_8859_1).get())
 {
+            final ZipArchiveEntry entry = zipFile.getEntries().nextElement();
+            final ZipArchiveEntry streamEntry = stream.getNextEntry();
+            assertNotNull(streamEntry);
+            assertArrayEquals(name.getBytes(UTF_8), entry.getRawName());
+            assertArrayEquals(entry.getRawName(), streamEntry.getRawName());
+            assertFalse(entry.isLocalFileHeaderNameDivergent());
+            assertTrue(entry.getGeneralPurposeBit().usesUTF8ForNames());
+            assertFalse(streamEntry.getGeneralPurposeBit().usesUTF8ForNames());
+            assertEquals(name, entry.getName());
+            assertEquals("\u00c3\u00a9.txt", streamEntry.getName());
+            assertEquals(ZipArchiveEntry.NameSource.NAME_WITH_EFS_FLAG, 
entry.getNameSource());
+            assertEquals(ZipArchiveEntry.NameSource.NAME, 
streamEntry.getNameSource());
+        }
+    }
+
+    @Test
+    void 
testRejectDivergentLocalFileHeaderNameAllowsUnicodePathOnlyInCentralDirectory() 
throws IOException {
+        final byte[] rawName = "avatar.png".getBytes(UTF_8);
+        final byte[] data = createZipWithMatchingRawNames("avatar.png", 0, 0, 
new UnicodePathExtraField("avatar.php", rawName));
+        try (ZipFile zipFile = 
ZipFile.builder().setByteArray(data).setRejectLocalFileHeaderNameDivergence(true).get();
+                ZipArchiveInputStream stream = 
ZipArchiveInputStream.builder().setByteArray(data).get()) {
+            final ZipArchiveEntry entry = zipFile.getEntries().nextElement();
+            final ZipArchiveEntry streamEntry = stream.getNextEntry();
+            assertNotNull(streamEntry);
+            assertArrayEquals(rawName, entry.getRawName());
+            assertArrayEquals(entry.getRawName(), streamEntry.getRawName());
+            assertFalse(entry.isLocalFileHeaderNameDivergent());
+            assertNotNull(entry.getExtraField(UnicodePathExtraField.UPATH_ID));
+            
assertNull(streamEntry.getExtraField(UnicodePathExtraField.UPATH_ID));
+            assertEquals("avatar.php", entry.getName());
+            assertEquals("avatar.png", streamEntry.getName());
+            assertEquals(ZipArchiveEntry.NameSource.UNICODE_EXTRA_FIELD, 
entry.getNameSource());
+            assertEquals(ZipArchiveEntry.NameSource.NAME, 
streamEntry.getNameSource());
+        }
+    }
+
     @Test
     void testRejectDivergentLocalFileHeaderNameWithUnicodeExtraFields() throws 
IOException {
         final byte[] data = createZipWithUnicodeExtraFields(true);

Reply via email to