Skip to content

Commit 156c1ed

Browse files
committed
Support backslashes
1 parent d9d1198 commit 156c1ed

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.entitlement.runtime.policy.entitlements.FileEntitlement;
1313

14+
import java.io.File;
1415
import java.nio.file.Path;
1516
import java.util.ArrayList;
1617
import java.util.Arrays;
@@ -27,11 +28,11 @@ private FileAccessTree(List<FileEntitlement> fileEntitlements) {
2728
List<String> readPaths = new ArrayList<>();
2829
List<String> writePaths = new ArrayList<>();
2930
for (FileEntitlement fileEntitlement : fileEntitlements) {
30-
var mode = fileEntitlement.mode();
31-
if (mode == FileEntitlement.Mode.READ_WRITE) {
32-
writePaths.add(fileEntitlement.path());
31+
String path = normalizedPath(fileEntitlement);
32+
if (fileEntitlement.mode() == FileEntitlement.Mode.READ_WRITE) {
33+
writePaths.add(path);
3334
}
34-
readPaths.add(fileEntitlement.path());
35+
readPaths.add(path);
3536
}
3637

3738
readPaths.sort(String::compareTo);
@@ -53,8 +54,12 @@ boolean canWrite(Path path) {
5354
return checkPath(normalize(path), writePaths);
5455
}
5556

57+
private static String normalizedPath(FileEntitlement fileEntitlement) {
58+
return normalize(new File(fileEntitlement.path()).toPath());
59+
}
60+
5661
private static String normalize(Path path) {
57-
return path.toAbsolutePath().normalize().toString();
62+
return path.toAbsolutePath().normalize().toString().replace('\\', '/');
5863
}
5964

6065
private static boolean checkPath(String path, String[] paths) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ public void testNormalizePath() {
8787
assertThat(tree.canRead(path("")), is(false));
8888
}
8989

90+
public void testSlashesAreInterchangeable() {
91+
var tree = FileAccessTree.of(List.of(entitlement("a/b", "read"), entitlement("m\\n", "read")));
92+
assertThat(tree.canRead(path("a/b")), is(true));
93+
assertThat(tree.canRead(path("a\\b")), is(true));
94+
assertThat(tree.canRead(path("m/n")), is(true));
95+
assertThat(tree.canRead(path("m\\n")), is(true));
96+
97+
// Backslash shouldn't be treated as an escape
98+
assertThat(tree.canRead(path("m\n")), is(false));
99+
}
100+
90101
FileEntitlement entitlement(String path, String mode) {
91102
Path p = path(path);
92103
return new FileEntitlement(p.toString(), mode);

0 commit comments

Comments
 (0)