-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure DirectoryPathTree does not allow reading outside the root dir
- Loading branch information
1 parent
b50232c
commit 35794f6
Showing
7 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
...nt-projects/bootstrap/app-model/src/test/java/io/quarkus/paths/DirectoryPathTreeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package io.quarkus.paths; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DirectoryPathTreeTest { | ||
|
||
private static final String BASE_DIR = "paths/directory-path-tree"; | ||
|
||
private static volatile Path baseDir; | ||
|
||
@BeforeAll | ||
public static void staticInit() throws Exception { | ||
final URL url = Thread.currentThread().getContextClassLoader().getResource(BASE_DIR); | ||
if (url == null) { | ||
throw new IllegalStateException("Failed to locate " + BASE_DIR + " on the classpath"); | ||
} | ||
baseDir = Path.of(url.getPath()).toAbsolutePath(); | ||
if (!Files.exists(baseDir)) { | ||
throw new IllegalStateException("Failed to locate " + baseDir); | ||
} | ||
} | ||
|
||
@Test | ||
public void acceptExistingPath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final Path absolute = root.resolve("README.md"); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept("README.md", visit -> { | ||
assertThat(visit).isNotNull(); | ||
assertThat(visit.getRelativePath("/")).isEqualTo("README.md"); | ||
assertThat(visit.getPath()).exists(); | ||
assertThat(visit.getPath()).isEqualTo(absolute); | ||
assertThat(visit.getRoot()).isEqualTo(root); | ||
try { | ||
assertThat(Files.readString(visit.getPath())).isEqualTo("test readme"); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void acceptNonExistentPath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept("non-existent", visit -> { | ||
assertThat(visit).isNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void acceptLegalAbsolutePath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final Path absolute = root.resolve("README.md"); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept(absolute.toString(), visit -> { | ||
assertThat(visit).isNotNull(); | ||
assertThat(visit.getRelativePath("/")).isEqualTo("README.md"); | ||
assertThat(visit.getPath()).exists(); | ||
assertThat(visit.getPath()).isEqualTo(absolute); | ||
assertThat(visit.getRoot()).isEqualTo(root); | ||
try { | ||
assertThat(Files.readString(visit.getPath())).isEqualTo("test readme"); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void acceptIllegalAbsolutePath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final Path absolute = root.getParent().resolve("external.txt"); | ||
assertThat(absolute).exists(); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept(absolute.toString(), visit -> { | ||
assertThat(visit).isNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void acceptExistingAbsoluteNonNormalizedPath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final Path externalTxt = resolveRelative("external.txt"); | ||
assertThat(externalTxt).exists(); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept(externalTxt.toString() + "/../root/./README.md", visit -> { | ||
assertThat(visit).isNotNull(); | ||
assertThat(visit.getRelativePath("/")).isEqualTo("README.md"); | ||
assertThat(visit.getPath()).exists(); | ||
assertThat(visit.getRoot()).isEqualTo(root); | ||
try { | ||
assertThat(Files.readString(visit.getPath())).isEqualTo("test readme"); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void acceptExistingRelativeNonNormalizedPath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final Path absolute = root.resolve("README.md"); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept("../root/./other/../README.md", visit -> { | ||
assertThat(visit).isNotNull(); | ||
assertThat(visit.getRelativePath("/")).isEqualTo("README.md"); | ||
assertThat(visit.getPath()).exists(); | ||
assertThat(visit.getPath()).isEqualTo(absolute); | ||
assertThat(visit.getRoot()).isEqualTo(root); | ||
try { | ||
assertThat(Files.readString(visit.getPath())).isEqualTo("test readme"); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void acceptNonExistentRelativeNonNormalizedPath() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
tree.accept("../root/./README.md/../non-existent.txt", visit -> { | ||
assertThat(visit).isNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void walk() throws Exception { | ||
final Path root = resolveRelative("root"); | ||
final PathTree tree = PathTree.ofDirectoryOrArchive(root); | ||
|
||
final Set<String> visited = new HashSet<>(); | ||
final PathVisitor visitor = new PathVisitor() { | ||
@Override | ||
public void visitPath(PathVisit visit) { | ||
visited.add(visit.getRelativePath("/")); | ||
} | ||
}; | ||
tree.walk(visitor); | ||
|
||
assertThat(visited).isEqualTo(Set.of( | ||
"", | ||
"README.md", | ||
"src", | ||
"src/main", | ||
"src/main/java", | ||
"src/main/java/Main.java")); | ||
} | ||
|
||
/** | ||
* Returns a path relative to src/test/resources/paths/directory-path-tree/ | ||
* | ||
* @param relative relative path | ||
* @return Path instance | ||
*/ | ||
private static Path resolveRelative(String relative) { | ||
return baseDir.resolve(relative); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...nt-projects/bootstrap/app-model/src/test/resources/paths/directory-path-tree/external.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
external |
1 change: 1 addition & 0 deletions
1
...bootstrap/app-model/src/test/resources/paths/directory-path-tree/root/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test readme |
2 changes: 2 additions & 0 deletions
2
...strap/app-model/src/test/resources/paths/directory-path-tree/root/src/main/java/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
public class Main { | ||
} |