diff --git a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java index 62817f66f530c..16e7239678e62 100644 --- a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java +++ b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java @@ -156,14 +156,15 @@ private FileAccessTree( this.writePaths = pruneSortedPaths(writePaths).toArray(new String[0]); } - private static List pruneSortedPaths(List paths) { + // package private for testing + static List pruneSortedPaths(List paths) { List prunedReadPaths = new ArrayList<>(); if (paths.isEmpty() == false) { String currentPath = paths.get(0); prunedReadPaths.add(currentPath); for (int i = 1; i < paths.size(); ++i) { String nextPath = paths.get(i); - if (isParent(currentPath, nextPath) == false) { + if (currentPath.equals(nextPath) == false && isParent(currentPath, nextPath) == false) { prunedReadPaths.add(nextPath); currentPath = nextPath; } diff --git a/libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java b/libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java index 08625e02b9997..fbcbdc3ad5d18 100644 --- a/libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java +++ b/libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java @@ -347,6 +347,12 @@ public void testInvalidExclusiveAccess() { assertThat(tree.canWrite(path("a")), is(false)); } + public void testDuplicatePrunedPaths() { + List paths = List.of("/a", "/a", "/a/b", "/a/b", "/b/c", "b/c/d", "b/c/d", "b/c/d", "e/f", "e/f"); + paths = FileAccessTree.pruneSortedPaths(paths); + assertEquals(List.of("/a", "/b/c", "b/c/d", "e/f"), paths); + } + public void testWindowsAbsolutPathAccess() { assumeTrue("Specific to windows for paths with a root (DOS or UNC)", Platform.WINDOWS.isCurrent());