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

slachiewicz pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/maven-remote-resources-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 40eebdd  Normalize SCM paths without depending on the host filesystem 
(#334)
40eebdd is described below

commit 40eebdd3d4136c1d0ad7ed045f759de0838f0358
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Wed Oct 28 21:07:50 2026 +0100

    Normalize SCM paths without depending on the host filesystem (#334)
    
    Fixes #274. SCM URL paths keep their trailing slash, drop . segments and 
keep a .. that cannot be resolved, including one above the root of an absolute 
path, using a string tokenizer instead of java.nio.file.Paths. Repeated slashes 
are still collapsed.
    
    Co-authored-by: Elliotte Rusty Harold <[email protected]>
---
 .../remote/ModelInheritanceAssembler.java          | 38 +++++----
 .../remote/ModelInheritanceAssemblerTest.java      | 90 ++++++++++++++++++++++
 2 files changed, 111 insertions(+), 17 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssembler.java
 
b/src/main/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssembler.java
index 1da5c2e..cfc75d1 100644
--- 
a/src/main/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssembler.java
+++ 
b/src/main/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssembler.java
@@ -24,7 +24,6 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-import java.util.StringTokenizer;
 import java.util.TreeMap;
 
 import org.apache.maven.model.Build;
@@ -568,33 +567,31 @@ public class ModelInheritanceAssembler {
             uncleanPath = uncleanPath.substring(protocolIdx + 3);
         }
 
-        if (uncleanPath.startsWith("/")) {
-            cleanedPath += "/";
-        }
-
         return cleanedPath + resolvePath(uncleanPath);
     }
 
-    // TODO Move this to plexus-utils' PathTool.
+    /**
+     * Normalizes the path part of an SCM URL. A {@code ".."} segment that 
cannot be resolved is kept as is.
+     */
     private static String resolvePath(String uncleanPath) {
-        LinkedList<String> pathElements = new LinkedList<>();
+        boolean trailingSeparator = uncleanPath.endsWith("/");
+        boolean absolute = uncleanPath.startsWith("/");
 
-        StringTokenizer tokenizer = new StringTokenizer(uncleanPath, "/");
-
-        while (tokenizer.hasMoreTokens()) {
-            String token = tokenizer.nextToken();
+        LinkedList<String> pathElements = new LinkedList<>();
 
+        for (String token : uncleanPath.split("/", -1)) {
             switch (token) {
                 case "":
-                    // Empty path entry ("...//.."), remove.
+                case ".":
+                    // Redundant separators and current-directory (".") 
segments are removed.
                     break;
                 case "..":
-                    if (pathElements.isEmpty()) {
-                        // FIXME: somehow report to the user
-                        // that there are too many '..' elements.
-                        // For now, ignore the extra '..'.
-                    } else {
+                    if (!pathElements.isEmpty() && 
!"..".equals(pathElements.getLast())) {
                         pathElements.removeLast();
+                    } else {
+                        // Kept rather than rejected: the URL comes from a 
dependency's POM, and an odd value
+                        // there must not fail the build.
+                        pathElements.addLast(token);
                     }
                     break;
                 default:
@@ -604,6 +601,9 @@ public class ModelInheritanceAssembler {
         }
 
         StringBuilder cleanedPath = new StringBuilder();
+        if (absolute) {
+            cleanedPath.append('/');
+        }
 
         while (!pathElements.isEmpty()) {
             cleanedPath.append(pathElements.removeFirst());
@@ -612,6 +612,10 @@ public class ModelInheritanceAssembler {
             }
         }
 
+        if (trailingSeparator && cleanedPath.length() > 0 && 
cleanedPath.charAt(cleanedPath.length() - 1) != '/') {
+            cleanedPath.append('/');
+        }
+
         return cleanedPath.toString();
     }
 
diff --git 
a/src/test/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssemblerTest.java
 
b/src/test/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssemblerTest.java
new file mode 100644
index 0000000..fc64072
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugin/resources/remote/ModelInheritanceAssemblerTest.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.resources.remote;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for the SCM path normalization in {@link 
ModelInheritanceAssembler}.
+ */
+public class ModelInheritanceAssemblerTest {
+
+    private final ModelInheritanceAssembler assembler = new 
ModelInheritanceAssembler();
+
+    @Test
+    public void appendPathPreservesTrailingSlash() {
+        assertEquals(
+                "http://svn.example.com/repo/";,
+                assembler.appendPath("http://svn.example.com/repo/";, null, 
null, false));
+    }
+
+    @Test
+    public void appendPathAppendsChild() {
+        assertEquals(
+                "http://svn.example.com/repo/child";,
+                assembler.appendPath("http://svn.example.com/repo";, "child", 
null, true));
+    }
+
+    @Test
+    public void appendPathCollapsesDotSegment() {
+        assertEquals(
+                "http://svn.example.com/repo/child";,
+                assembler.appendPath("http://svn.example.com/repo/./child";, 
null, null, false));
+    }
+
+    @Test
+    public void appendPathResolvesParentDirectory() {
+        assertEquals(
+                "http://svn.example.com/repos";,
+                
assembler.appendPath("http://svn.example.com/repos/project/..";, null, null, 
false));
+    }
+
+    @Test
+    public void appendPathCollapsesRedundantSeparators() {
+        assertEquals(
+                "http://svn.example.com/repo/child";,
+                assembler.appendPath("http://svn.example.com/repo//child";, 
null, null, false));
+    }
+
+    @Test
+    public void appendPathPreservesExcessParentDirectory() {
+        assertEquals("../x", assembler.appendPath("a/../../x", null, null, 
false));
+    }
+
+    @Test
+    public void appendPathResolvesParentDirectoryInAbsolutePath() {
+        assertEquals("/x", assembler.appendPath("/a/../x", null, null, false));
+    }
+
+    @Test
+    public void appendPathKeepsParentDirectoryAboveRoot() {
+        assertEquals("/../x", assembler.appendPath("/../x", null, null, 
false));
+    }
+
+    /**
+     * A segment containing {@code ':'} (a port, as common in SVN/git SCM 
URLs) must normalize
+     * identically on every platform.
+     */
+    @Test
+    public void appendPathNormalizesUrlWithPort() {
+        assertEquals("http://host:8080/x";, 
assembler.appendPath("http://host:8080/repo/../x";, null, null, false));
+    }
+}

Reply via email to