Skip to content

Commit 3e2597f

Browse files
jdconradldematteprdoyle
authored
remove duplicate paths in FileAccessTree (#123776)
Co-authored-by: Lorenzo Dematté <[email protected]> Co-authored-by: Patrick Doyle <[email protected]>
1 parent be577e3 commit 3e2597f

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
@@ -347,6 +347,12 @@ public void testInvalidExclusiveAccess() {
347347
assertThat(tree.canWrite(path("a")), is(false));
348348
}
349349

350+
public void testDuplicatePrunedPaths() {
351+
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");
352+
paths = FileAccessTree.pruneSortedPaths(paths);
353+
assertEquals(List.of("/a", "/b/c", "b/c/d", "e/f"), paths);
354+
}
355+
350356
public void testWindowsAbsolutPathAccess() {
351357
assumeTrue("Specific to windows for paths with a root (DOS or UNC)", Platform.WINDOWS.isCurrent());
352358

0 commit comments

Comments
 (0)