Skip to content

Commit

Permalink
Of course ... windows
Browse files Browse the repository at this point in the history
---
 Signed-off-by: Peter Kriens <[email protected]>

Signed-off-by: Peter Kriens <[email protected]>
  • Loading branch information
pkriens committed Feb 7, 2024
1 parent b2a972f commit 45ea868
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
33 changes: 19 additions & 14 deletions aQute.libg/src/aQute/lib/io/Windows.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion aQute.libg/test/aQute/lib/io/IOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit 45ea868

Please sign in to comment.