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

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new a47504df53 Fix two OPC part-name handling issues (percent-escape 
bounds check, marshaller round-trip) (#1228)
a47504df53 is described below

commit a47504df53786b9f802cbac4b545f60fec2cd594
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Aug 31 16:07:56 2026 +0100

    Fix two OPC part-name handling issues (percent-escape bounds check, 
marshaller round-trip) (#1228)
    
    Fix two OPC part-name handling issues
    
    1. PackagePartName.checkPCharCompliant: the percent-escape length check used
       "(length - i) < 2" where it should be "< 3" (a "%" plus two hex digits 
needs
       three chars). With i == length-2 the code would index charAt(length) and 
throw
       StringIndexOutOfBoundsException instead of a clean 
InvalidFormatException. Not
       reachable from a parsed URI today (new URI(...) rejects a truncated 
escape
       first), but corrected defensively.
    
    2. ZipPartMarshaller wrote the zip item name from the decoded 
getURI().getPath()
       instead of the ASCII part name getName(). The zip item name is 
round-tripped
       verbatim on read (ZipHelper.getOPCNameFromZipItemName) and the 
content-type
       registration also uses getName(), so a part name containing 
percent-encoded
       characters was renamed on save/open. Use getName() for consistency. 
Added a
       save/open round-trip test.
    
    Co-authored-by: Claude Fable 5 <[email protected]>
---
 .../apache/poi/openxml4j/opc/PackagePartName.java  |  2 +-
 .../internal/marshallers/ZipPartMarshaller.java    |  7 +++--
 .../org/apache/poi/openxml4j/opc/TestPackage.java  | 31 ++++++++++++++++++++++
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java 
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
index f3232d71f5..8b7dd221f0 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
@@ -317,7 +317,7 @@ public final class PackagePartName implements 
Comparable<PackagePartName> {
 
             // We certainly found an encoded character, check for length
             // now ( '%' HEXDIGIT HEXDIGIT)
-            if ((length - i) < 2 || !isHexDigit(segment.charAt(i+1)) || 
!isHexDigit(segment.charAt(i+2))) {
+            if ((length - i) < 3 || !isHexDigit(segment.charAt(i+1)) || 
!isHexDigit(segment.charAt(i+2))) {
                 throw new InvalidFormatException("The segment " + segment + " 
contain invalid encoded character !");
             }
 
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java
 
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java
index b9d878a1af..fdfcfacd70 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java
@@ -79,9 +79,12 @@ public final class ZipPartMarshaller implements 
PartMarshaller {
         }
 
         ZipArchiveOutputStream zos = (ZipArchiveOutputStream) os;
+        // Use getName() (the ASCII part name, preserving any 
percent-encoding) rather than the
+        // decoded getURI().getPath(): the zip item name is round-tripped 
verbatim on read
+        // (see ZipHelper.getOPCNameFromZipItemName), so a decoded name would 
corrupt a part name
+        // containing percent-encoded characters.
         ZipArchiveEntry partEntry = new ZipArchiveEntry(ZipHelper
-                .getZipItemNameFromOPCName(part.getPartName().getURI()
-                        .getPath()));
+                .getZipItemNameFromOPCName(part.getPartName().getName()));
         try {
             ZipHelper.adjustEntryTime(partEntry);
 
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java 
b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java
index e7203bd67f..eaf3befdaa 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java
@@ -169,6 +169,37 @@ public final class TestPackage {
         }
     }
 
+    /**
+     * A part name containing percent-encoded characters must survive a 
save/open round-trip.
+     * The zip item name is written verbatim and re-read verbatim, so the 
marshaller has to use the
+     * ASCII part name (getName()) rather than the decoded URI path - 
otherwise the part is renamed
+     * (and here, being an invalid escape after decoding, silently dropped) on 
reload.
+     */
+    @Test
+    void percentEncodedPartNameRoundTrips() throws IOException, 
InvalidFormatException {
+        // "%40" is a valid percent-encoded part-name character (encodes '@', 
which is an authorized
+        // pchar, so it is neither a separator [M1.7] nor an unreserved 
character [M1.8]).
+        final PackagePartName partName = createPartName("/foo/a%40b.xml");
+        final String content = "hello";
+
+        UnsynchronizedByteArrayOutputStream bos = 
UnsynchronizedByteArrayOutputStream.builder().get();
+        try (OPCPackage pkg = OPCPackage.create(bos)) {
+            PackagePart part = pkg.createPart(partName, "application/xml");
+            try (OutputStream out = part.getOutputStream()) {
+                out.write(content.getBytes(StandardCharsets.UTF_8));
+            }
+        }
+
+        try (OPCPackage pkg = OPCPackage.open(bos.toInputStream())) {
+            PackagePart part = pkg.getPart(partName);
+            assertNotNull(part, "part with a percent-encoded name should 
survive a save/open round-trip");
+            assertEquals("/foo/a%40b.xml", part.getPartName().getName());
+            try (InputStream in = part.getInputStream()) {
+                assertEquals(content, new String(IOUtils.toByteArray(in), 
StandardCharsets.UTF_8));
+            }
+        }
+    }
+
     /**
      * Test package creation.
      */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to