Add subpath guard function to avoid non-relative subpath parsed URI

This commit is contained in:
Hyoseop Song
2024-11-20 18:16:56 +09:00
committed by Loïc Mathieu
parent e850764403
commit ba0e770a05

View File

@@ -233,14 +233,20 @@ public class LocalStorage implements StorageInterface {
.toList();
}
}
private URI getKestraUri(String tenantId, Path path) {
Path prefix = (tenantId == null) ?
basePath.toAbsolutePath() :
Path.of(basePath.toAbsolutePath().toString(), tenantId);
basePath.toAbsolutePath():
basePath.toAbsolutePath().resolve(tenantId);
subPathParentGuard(path, prefix);
return URI.create("kestra:///" + prefix.relativize(path).toString().replace("\\", "/"));
}
private void subPathParentGuard(Path path, Path prefix) {
if (!path.toAbsolutePath().startsWith(prefix)) {
throw new IllegalArgumentException("The path must be a subpath of the base path with the tenant ID.");
}
}
private void parentTraversalGuard(URI uri) {
if (uri.toString().contains("..")) {
throw new IllegalArgumentException("File should be accessed with their full path and not using relative '..' path.");