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-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 13e1ccd3 normalize jar entry paths with URI.normalize in 
StscImporter.resolve (#139)
13e1ccd3 is described below

commit 13e1ccd338742e65fcf67b18e81b19f88308de82
Author: PJ Fanning <[email protected]>
AuthorDate: Sun Oct 18 14:25:34 2026 +0100

    normalize jar entry paths with URI.normalize in StscImporter.resolve (#139)
    
    Replace the hand-rolled "/.." removal after "!/" with URI.normalize on
    the entry path. This also resolves "./" segments, no longer treats names
    such as "..b.xsd" or "..." as parent references, and keeps ".." segments
    that climb above the jar root in place (as before) so the lookup fails.
    
    Tests now use a jar path that cannot exist and assert the unresolved
    result for the above-root case.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/xmlbeans/impl/schema/StscImporter.java  | 16 +++--------
 .../impl/schema/StscImporterResolveTest.java       | 31 +++++++++++++++++++---
 2 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/StscImporter.java 
b/src/main/java/org/apache/xmlbeans/impl/schema/StscImporter.java
index 288ac6fc..6a3d5b05 100644
--- a/src/main/java/org/apache/xmlbeans/impl/schema/StscImporter.java
+++ b/src/main/java/org/apache/xmlbeans/impl/schema/StscImporter.java
@@ -240,20 +240,12 @@ public class StscImporter {
             r = r.substring(0, lastslash) + "/" + childUri;
             // Sun's implementation of URI doesn't support references to the
             // parent directory ("/..") in the part after "!/" so we have to
-            // remove these ourselves
+            // normalize the entry path ourselves. ".." segments that climb
+            // above the root of the jar are left in place, so the lookup 
fails.
             int exclPointSlashIndex = r.lastIndexOf("!/");
             if (exclPointSlashIndex > 0) {
-                int slashDotDotIndex = r.indexOf("/..", exclPointSlashIndex);
-                while (slashDotDotIndex > 0) {
-                    int prevSlashIndex = r.lastIndexOf("/", slashDotDotIndex - 
1);
-                    if (prevSlashIndex < exclPointSlashIndex) {
-                        // ".." above the root of the jar, leave it unresolved
-                        break;
-                    }
-                    String temp = r.substring(slashDotDotIndex + 3);
-                    r = r.substring(0, prevSlashIndex).concat(temp);
-                    slashDotDotIndex = r.indexOf("/..", exclPointSlashIndex);
-                }
+                String entry = r.substring(exclPointSlashIndex + 1);
+                r = r.substring(0, exclPointSlashIndex + 1) + new 
URI(entry).normalize();
             }
             return URI.create(r);
         }
diff --git 
a/src/test/java/org/apache/xmlbeans/impl/schema/StscImporterResolveTest.java 
b/src/test/java/org/apache/xmlbeans/impl/schema/StscImporterResolveTest.java
index 77097a10..fab8f5e5 100644
--- a/src/test/java/org/apache/xmlbeans/impl/schema/StscImporterResolveTest.java
+++ b/src/test/java/org/apache/xmlbeans/impl/schema/StscImporterResolveTest.java
@@ -33,20 +33,43 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
 
+
 public class StscImporterResolveTest {
 
-    private static final URI JAR_BASE = 
URI.create("jar:file:/tmp/schemas.jar!/xsd/a.xsd");
+    private static final String JAR = "jar:file:/nonexistent/schemas.jar!";
+    private static final URI JAR_BASE = URI.create(JAR + "/xsd/a.xsd");
 
     @Test
     void resolvesParentInsideJar() throws Exception {
-        assertEquals("jar:file:/tmp/schemas.jar!/common/b.xsd",
+        assertEquals(JAR + "/common/b.xsd",
             StscImporter.resolve(JAR_BASE, "../common/b.xsd").toString());
     }
 
     @Test
-    void parentAboveJarRootTerminates() {
-        assertTimeoutPreemptively(Duration.ofSeconds(10), () ->
+    void resolvesCurrentDirInsideJar() throws Exception {
+        assertEquals(JAR + "/xsd/common/b.xsd",
+            StscImporter.resolve(JAR_BASE, "./common/./b.xsd").toString());
+    }
+
+    @Test
+    void keepsDotDotPrefixedNames() throws Exception {
+        assertEquals(JAR + "/xsd/..b.xsd",
+            StscImporter.resolve(JAR_BASE, "..b.xsd").toString());
+        assertEquals(JAR + "/xsd/.../b.xsd",
+            StscImporter.resolve(JAR_BASE, ".../b.xsd").toString());
+    }
+
+    @Test
+    void keepsFragment() throws Exception {
+        assertEquals(JAR + "/common/b.xsd#frag",
+            StscImporter.resolve(JAR_BASE, "../common/b.xsd#frag").toString());
+    }
+
+    @Test
+    void parentAboveJarRootIsLeftUnresolved() {
+        URI resolved = assertTimeoutPreemptively(Duration.ofSeconds(10), () ->
             StscImporter.resolve(JAR_BASE, "../../b.xsd"));
+        assertEquals(JAR + "/../b.xsd", resolved.toString());
     }
 
     @Test


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

Reply via email to