Skip to content

Commit 91221e3

Browse files
committed
check link target and source at once for more informative exception
1 parent 1bf3082 commit 91221e3

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,9 @@ public void checkPathToRealPath(Class<?> callerClass, Path that, LinkOption... o
27622762
}
27632763
if (followLinks) {
27642764
try {
2765-
policyManager.checkFileRead(callerClass, Files.readSymbolicLink(that));
2765+
Path symbolicLink = Files.readSymbolicLink(that);
2766+
policyManager.checkFileRead(callerClass, that, symbolicLink);
2767+
return;
27662768
} catch (IOException | UnsupportedOperationException e) {
27672769
// that is not a link, or unrelated IOException or unsupported
27682770
}

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.lang.module.ModuleReference;
3737
import java.nio.file.Path;
3838
import java.util.ArrayList;
39+
import java.util.Arrays;
3940
import java.util.HashSet;
4041
import java.util.List;
4142
import java.util.Map;
@@ -324,28 +325,30 @@ private static boolean isPathOnDefaultFilesystem(Path path) {
324325
return true;
325326
}
326327

327-
public void checkFileRead(Class<?> callerClass, Path path) {
328-
if (isPathOnDefaultFilesystem(path) == false) {
329-
return;
330-
}
331-
var requestingClass = requestingClass(callerClass);
332-
if (isTriviallyAllowed(requestingClass)) {
333-
return;
334-
}
328+
public void checkFileRead(Class<?> callerClass, Path... paths) {
329+
for (var path : paths) {
330+
if (isPathOnDefaultFilesystem(path) == false) {
331+
return;
332+
}
333+
var requestingClass = requestingClass(callerClass);
334+
if (isTriviallyAllowed(requestingClass)) {
335+
return;
336+
}
335337

336-
ModuleEntitlements entitlements = getEntitlements(requestingClass);
337-
if (entitlements.fileAccess().canRead(path) == false) {
338-
logger.info(entitlements.fileAccess().toDebugString());
339-
notEntitled(
340-
Strings.format(
341-
"Not entitled: component [%s], module [%s], class [%s], entitlement [file], operation [read], path [%s]",
342-
entitlements.componentName(),
343-
requestingClass.getModule().getName(),
344-
requestingClass,
345-
FileAccessTree.normalizePath(path)
346-
),
347-
callerClass
348-
);
338+
ModuleEntitlements entitlements = getEntitlements(requestingClass);
339+
if (entitlements.fileAccess().canRead(path) == false) {
340+
logger.info(entitlements.fileAccess().toDebugString());
341+
notEntitled(
342+
Strings.format(
343+
"Not entitled: component [%s], module [%s], class [%s], entitlement [file], operation [read], path [%s]",
344+
entitlements.componentName(),
345+
requestingClass.getModule().getName(),
346+
requestingClass,
347+
Arrays.stream(paths).map(FileAccessTree::normalizePath).collect(Collectors.joining(", "))
348+
),
349+
callerClass
350+
);
351+
}
349352
}
350353
}
351354

0 commit comments

Comments
 (0)