Skip to content

Commit 8326027

Browse files
committed
remove duplicate paths in FileAccessTree
1 parent c0c0827 commit 8326027

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ private FileAccessTree(
153153
this.writePaths = pruneSortedPaths(writePaths).toArray(new String[0]);
154154
}
155155

156-
private static List<String> pruneSortedPaths(List<String> paths) {
156+
// package private for testing
157+
static List<String> pruneSortedPaths(List<String> paths) {
157158
List<String> prunedReadPaths = new ArrayList<>();
158159
if (paths.isEmpty() == false) {
159160
String currentPath = paths.get(0);
160161
prunedReadPaths.add(currentPath);
161162
for (int i = 1; i < paths.size(); ++i) {
162163
String nextPath = paths.get(i);
163-
if (isParent(currentPath, nextPath) == false) {
164+
if (currentPath.equals(nextPath) == false && isParent(currentPath, nextPath) == false) {
164165
prunedReadPaths.add(nextPath);
165166
currentPath = nextPath;
166167
}

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ public void testInvalidExclusiveAccess() {
316316
assertThat(tree.canWrite(path("a")), is(false));
317317
}
318318

319+
public void testDuplicatePrunedPaths() {
320+
List<String> paths = List.of("/a", "/a", "/a/b", "/a/b", "/b/c", "b/c/d", "b/c/d", "b/c/d", "e/f", "e/f");
321+
paths = FileAccessTree.pruneSortedPaths(paths);
322+
assertEquals(List.of("/a", "/b/c", "b/c/d", "e/f"), paths);
323+
}
324+
319325
FileAccessTree accessTree(FilesEntitlement entitlement, List<ExclusivePath> exclusivePaths) {
320326
return FileAccessTree.of("test-component", "test-module", entitlement, TEST_PATH_LOOKUP, exclusivePaths);
321327
}

0 commit comments

Comments
 (0)