Skip to content

Commit 07f6457

Browse files
jdconradldematteprdoyle
committed
remove duplicate paths in FileAccessTree (elastic#123776)
Co-authored-by: Lorenzo Dematté <[email protected]> Co-authored-by: Patrick Doyle <[email protected]>
1 parent 030cbaf commit 07f6457

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
@@ -156,14 +156,15 @@ private FileAccessTree(
156156
this.writePaths = pruneSortedPaths(writePaths).toArray(new String[0]);
157157
}
158158

159-
private static List<String> pruneSortedPaths(List<String> paths) {
159+
// package private for testing
160+
static List<String> pruneSortedPaths(List<String> paths) {
160161
List<String> prunedReadPaths = new ArrayList<>();
161162
if (paths.isEmpty() == false) {
162163
String currentPath = paths.get(0);
163164
prunedReadPaths.add(currentPath);
164165
for (int i = 1; i < paths.size(); ++i) {
165166
String nextPath = paths.get(i);
166-
if (isParent(currentPath, nextPath) == false) {
167+
if (currentPath.equals(nextPath) == false && isParent(currentPath, nextPath) == false) {
167168
prunedReadPaths.add(nextPath);
168169
currentPath = nextPath;
169170
}

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
@@ -321,6 +321,12 @@ public void testInvalidExclusiveAccess() {
321321
assertThat(tree.canWrite(path("a")), is(false));
322322
}
323323

324+
public void testDuplicatePrunedPaths() {
325+
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");
326+
paths = FileAccessTree.pruneSortedPaths(paths);
327+
assertEquals(List.of("/a", "/b/c", "b/c/d", "e/f"), paths);
328+
}
329+
324330
FileAccessTree accessTree(FilesEntitlement entitlement, List<ExclusivePath> exclusivePaths) {
325331
return FileAccessTree.of("test-component", "test-module", entitlement, TEST_PATH_LOOKUP, exclusivePaths);
326332
}

0 commit comments

Comments
 (0)