You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/09/13 15:53:21 UTC

(poi) branch trunk updated: Fix getRelationshipPartName for percent-encoded part names (#1265)

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 9935676696 Fix getRelationshipPartName for percent-encoded part names (#1265)
9935676696 is described below

commit 9935676696aac1bd8863f2bb04252f91b414bc9c
Author: Burak Kalaycı <ka...@gmail.com>
AuthorDate: Sun Sep 13 18:53:15 2026 +0300

    Fix getRelationshipPartName for percent-encoded part names (#1265)
    
    * Fix getRelationshipPartName for percent-encoded part names
    
    URI.getPath() decodes %20 to a space, so createPartName later throws
    and the method returns null. Keep the raw path when building the
    relationship part name.
    
    Fixes #1264
    
    * Keep percent-encoding when inverting relationship part URIs
    
    getSourcePartUriFromRelationshipPartUri now uses getRawPath, matching
    getRelationshipPartName, so save/remove still work for names like %20.
---
 .../poi/openxml4j/opc/PackagingURIHelper.java      | 29 ++++++++++++++--------
 .../poi/openxml4j/opc/TestPackagePartName.java     | 23 +++++++++++++++++
 2 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
index c2e87ac5e1..7d3001d332 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
@@ -455,15 +455,21 @@ public final class PackagingURIHelper {
         if (relationshipPartUri.compareTo(PACKAGE_RELATIONSHIPS_ROOT_URI) == 0)
             return PACKAGE_ROOT_URI;
 
-        String filename = relationshipPartUri.getPath();
-        String filenameWithoutExtension = getFilenameWithoutExtension(relationshipPartUri);
-        filename = filename
-                .substring(0, ((filename.length() - filenameWithoutExtension
-                        .length()) - RELATIONSHIP_PART_EXTENSION_NAME.length()));
-        filename = filename.substring(0, filename.length()
+        // Use the raw path so percent-encoded characters (e.g. %20) are not
+        // decoded into bytes that later fail URI syntax in getURIFromPath.
+        String fullPath = relationshipPartUri.getRawPath();
+        int lastSlash = fullPath.lastIndexOf(FORWARD_SLASH_CHAR);
+        String filename = (lastSlash >= 0) ? fullPath.substring(lastSlash + 1) : fullPath;
+        if (filename.endsWith(RELATIONSHIP_PART_EXTENSION_NAME)) {
+            filename = filename.substring(0, filename.length()
+                    - RELATIONSHIP_PART_EXTENSION_NAME.length());
+        }
+        fullPath = fullPath.substring(0, fullPath.length() - filename.length()
+                - RELATIONSHIP_PART_EXTENSION_NAME.length());
+        fullPath = fullPath.substring(0, fullPath.length()
                 - RELATIONSHIP_PART_SEGMENT_NAME.length() - 1);
-        filename = combine(filename, filenameWithoutExtension);
-        return getURIFromPath(filename);
+        fullPath = combine(fullPath, filename);
+        return getURIFromPath(fullPath);
     }
 
     /**
@@ -642,8 +648,11 @@ public final class PackagingURIHelper {
         if (partName.isRelationshipPartURI())
             throw new InvalidOperationException("Can't be a relationship part");
 
-        String fullPath = partName.getURI().getPath();
-        String filename = getFilename(partName.getURI());
+        // Use the raw path so percent-encoded characters (e.g. %20) are not
+        // decoded into bytes that later fail URI syntax in createPartName.
+        String fullPath = partName.getURI().getRawPath();
+        int lastSlash = fullPath.lastIndexOf(FORWARD_SLASH_CHAR);
+        String filename = (lastSlash >= 0) ? fullPath.substring(lastSlash + 1) : fullPath;
         fullPath = fullPath.substring(0, fullPath.length() - filename.length());
         fullPath = combine(fullPath,
                 PackagingURIHelper.RELATIONSHIP_PART_SEGMENT_NAME);
diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackagePartName.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackagePartName.java
index c38e802e01..0ad3f54573 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackagePartName.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackagePartName.java
@@ -18,6 +18,7 @@
 package org.apache.poi.openxml4j.opc;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import org.junit.jupiter.api.Test;
 
@@ -33,4 +34,26 @@ public final class TestPackagePartName {
         assertEquals("xml", name1.getExtension());
         assertEquals("", name2.getExtension());
     }
+
+    @Test
+    void testGetRelationshipPartName() throws Exception {
+        PackagePartName partName = PackagingURIHelper.createPartName("/word/document.xml");
+        PackagePartName relName = PackagingURIHelper.getRelationshipPartName(partName);
+        assertEquals("/word/_rels/document.xml.rels", relName.getName());
+    }
+
+    /**
+     * URI.getPath() decodes percent-encoding, so a part name with spaces
+     * (stored as %20) used to make createPartName throw and this method
+     * return null. Keep the raw path so the relationship part name is valid.
+     */
+    @Test
+    void testGetRelationshipPartNameWithPercentEncodedCharacters() throws Exception {
+        PackagePartName partName = PackagingURIHelper.createPartName("/aasx/test%20document.txt");
+        PackagePartName relName = PackagingURIHelper.getRelationshipPartName(partName);
+        assertNotNull(relName);
+        assertEquals("/aasx/_rels/test%20document.txt.rels", relName.getName());
+        assertEquals(partName.getURI(),
+                PackagingURIHelper.getSourcePartUriFromRelationshipPartUri(relName.getURI()));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org