diff --git a/aQute.libg/src/aQute/lib/io/Windows.java b/aQute.libg/src/aQute/lib/io/Windows.java index 3b913aead7..30bc7a900d 100644 --- a/aQute.libg/src/aQute/lib/io/Windows.java +++ b/aQute.libg/src/aQute/lib/io/Windows.java @@ -19,23 +19,28 @@ public File getBasedFile(File base, String subPath) throws IOException { Matcher matcher = WINDOWS_BAD_FILE_NAME_P.matcher(subPath); if (matcher.find()) { - Path normalizedPath = Path.of(subPath) - .normalize(); + try { + Path normalizedPath = Path.of(subPath) + .normalize(); - if (normalizedPath.startsWith(IO.DOTDOT)) { - throw new IOException("io.sub.up invalid path, will escape the designated directory. path='" + subPath - + "', base='" + base + "', normalized='" + normalizedPath + "'"); - } - for (int i = 0; i < normalizedPath.getNameCount(); i++) { - String segment = normalizedPath.getName(i) - .toString(); - if (matcher.reset(segment) - .matches()) { - throw new IOException("io.win.sub.invalid pathcontains reserved names on windows. path='" + subPath - + "', base='" + base + "', pattern='" + WINDOWS_BAD_FILE_NAME_P + "'"); + if (normalizedPath.startsWith(IO.DOTDOT)) { + throw new IOException("io.sub.up invalid path, will escape the designated directory. path='" + + subPath + "', base='" + base + "', normalized='" + normalizedPath + "'"); + } + for (int i = 0; i < normalizedPath.getNameCount(); i++) { + String segment = normalizedPath.getName(i) + .toString(); + if (matcher.reset(segment) + .matches()) { + throw new IOException("io.win.sub.invalid pathcontains reserved names on windows. path='" + + subPath + "', base='" + base + "', pattern='" + WINDOWS_BAD_FILE_NAME_P + "'"); + } } + use = normalizedPath.toString(); + } catch (java.nio.file.InvalidPathException e) { + throw new IOException("io.win.sub.invalid pathcontains reserved names on windows. path='" + subPath + + "', base='" + base + "': '" + e.getMessage() + "'"); } - use = normalizedPath.toString(); } else use = subPath; return new File(base, use); diff --git a/aQute.libg/test/aQute/lib/io/IOTest.java b/aQute.libg/test/aQute/lib/io/IOTest.java index bd97d98278..785196f475 100644 --- a/aQute.libg/test/aQute/lib/io/IOTest.java +++ b/aQute.libg/test/aQute/lib/io/IOTest.java @@ -558,11 +558,12 @@ public void testGetBasedFile() throws IOException { assertThat("foo/💩../bar".contains("..")).isTrue(); assertThat("foo/.💩./bar".contains("..")).isFalse(); File base = new File("base").getAbsoluteFile(); + assertThat(new File(base, "").getParentFile()).isEqualTo(base); + assertThat(new File(base, "./").getParentFile()).isEqualTo(base); assertThat(new File(base, "/bar").getParentFile()).isEqualTo(base); assertThat(IO.getBasedFile(base, "")).isEqualTo(base); assertThat(IO.getBasedFile(base, "foo/../bar")).isEqualTo(new File(base, "bar")); assertThat(IO.getBasedFile(base, ".💩.")).isEqualTo(new File(base, ".💩.")); - assertThat(IO.getBasedFile(base, ".x\u0008.")).isEqualTo(new File(base, ".x\u0008.")); assertThat(except(() -> IO.getBasedFile(base, ".."))).contains("io.sub.up"); assertThat(except(() -> IO.getBasedFile(base, "bar/../../.."))).contains("io.sub.up"); }