diff --git a/src/main/java/org/codehaus/plexus/util/FileUtils.java b/src/main/java/org/codehaus/plexus/util/FileUtils.java index 06354eae..0e9d8a69 100644 --- a/src/main/java/org/codehaus/plexus/util/FileUtils.java +++ b/src/main/java/org/codehaus/plexus/util/FileUtils.java @@ -1021,6 +1021,7 @@ private static void mkdirsFor(File destination) { * /foo/../bar/ --> /bar/ * /foo/../bar/../baz --> /baz * //foo//./bar --> /foo/bar + * /foo/bar/.. --> /foo * /../ --> null * * @@ -1029,6 +1030,13 @@ private static void mkdirsFor(File destination) { */ public static String normalize(final String path) { String normalized = path; + // A trailing "/." or "/.." is a segment too: add the slash the loops below look for, and drop it again after + boolean addedTrailingSlash = false; + if (normalized.startsWith("/") && (normalized.endsWith("/.") || normalized.endsWith("/.."))) { + normalized = normalized + "/"; + addedTrailingSlash = true; + } + // Resolve occurrences of "//" in the normalized path while (true) { int index = normalized.indexOf("//"); @@ -1060,6 +1068,10 @@ public static String normalize(final String path) { normalized = normalized.substring(0, index2) + normalized.substring(index + 3); } + if (addedTrailingSlash && normalized.length() > 1) { + normalized = normalized.substring(0, normalized.length() - 1); + } + // Return the normalized path that we have completed return normalized; } diff --git a/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java b/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java index dd4b15f1..c2c62379 100644 --- a/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java @@ -558,7 +558,12 @@ void normalize() { "/foo/.././bar/", "//foo//./bar", "/../", - "/foo/../../" + "/foo/../../", + "/foo/.", + "/foo/..", + "/foo/bar/..", + "/..", + "/foo/../.." }; final String[] dest = { @@ -578,6 +583,11 @@ void normalize() { "/bar/", "/foo/bar", null, + null, + "/foo", + "/", + "/foo", + null, null };