|
| 1 | +package io.kestra.core.runners.pebble.functions; |
| 2 | + |
| 3 | +import io.kestra.core.services.FlowService; |
| 4 | +import io.kestra.core.utils.Slugify; |
| 5 | +import io.pebbletemplates.pebble.error.PebbleException; |
| 6 | +import io.pebbletemplates.pebble.extension.Function; |
| 7 | +import io.pebbletemplates.pebble.template.EvaluationContext; |
| 8 | +import io.pebbletemplates.pebble.template.PebbleTemplate; |
| 9 | +import jakarta.inject.Inject; |
| 10 | + |
| 11 | +import java.net.URI; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +abstract class AbstractFileFunction implements Function { |
| 16 | + static final String KESTRA_SCHEME = "kestra:///"; |
| 17 | + static final String TRIGGER = "trigger"; |
| 18 | + static final String NAMESPACE = "namespace"; |
| 19 | + static final String TENANT_ID = "tenantId"; |
| 20 | + static final String ID = "id"; |
| 21 | + |
| 22 | + private static final Pattern EXECUTION_FILE = Pattern.compile(".*/.*/executions/.*/tasks/.*/.*"); |
| 23 | + |
| 24 | + @Inject |
| 25 | + private FlowService flowService; |
| 26 | + |
| 27 | + URI getUriFromThePath(Object path, int lineNumber, PebbleTemplate self) { |
| 28 | + if (path instanceof URI u) { |
| 29 | + return u; |
| 30 | + } else if (path instanceof String str && str.startsWith(KESTRA_SCHEME)) { |
| 31 | + return URI.create(str); |
| 32 | + } else { |
| 33 | + throw new PebbleException(null, "Unable to create the URI from the path " + path, lineNumber, self.getName()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + boolean isFileUriValid(String namespace, String flowId, String executionId, URI path) { |
| 38 | + // Internal storage URI should be: kestra:///$namespace/$flowId/executions/$executionId/tasks/$taskName/$taskRunId/$random.ion or kestra:///$namespace/$flowId/executions/$executionId/trigger/$triggerName/$random.ion |
| 39 | + // We check that the file is for the given flow execution |
| 40 | + if (namespace == null || flowId == null || executionId == null) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + String authorizedBasePath = KESTRA_SCHEME + namespace.replace(".", "/") + "/" + Slugify.of(flowId) + "/executions/" + executionId + "/"; |
| 45 | + return path.toString().startsWith(authorizedBasePath); |
| 46 | + } |
| 47 | + |
| 48 | + @SuppressWarnings("unchecked") |
| 49 | + String checkAllowedFileAndReturnNamespace(EvaluationContext context, URI path) { |
| 50 | + Map<String, String> flow = (Map<String, String>) context.getVariable("flow"); |
| 51 | + Map<String, String> execution = (Map<String, String>) context.getVariable("execution"); |
| 52 | + |
| 53 | + // check if the file is from the current execution, the parent execution or an allowed namespaces |
| 54 | + boolean isFileFromCurrentExecution = isFileUriValid(flow.get(NAMESPACE), flow.get(ID), execution.get(ID), path); |
| 55 | + if (isFileFromCurrentExecution) { |
| 56 | + return flow.get(NAMESPACE); |
| 57 | + } else { |
| 58 | + if (isFileFromParentExecution(context, path)) { |
| 59 | + Map<String, String> trigger = (Map<String, String>) context.getVariable(TRIGGER); |
| 60 | + return trigger.get(NAMESPACE); |
| 61 | + } |
| 62 | + else { |
| 63 | + return checkIfFileFromAllowedNamespaceAndReturnIt(context, path, flow.get(TENANT_ID), flow.get(NAMESPACE)); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @SuppressWarnings("unchecked") |
| 69 | + private boolean isFileFromParentExecution(EvaluationContext context, URI path) { |
| 70 | + if (context.getVariable(TRIGGER) != null) { |
| 71 | + // if there is a trigger of type execution, we also allow accessing a file from the parent execution |
| 72 | + Map<String, String> trigger = (Map<String, String>) context.getVariable(TRIGGER); |
| 73 | + |
| 74 | + if (!isFileUriValid(trigger.get(NAMESPACE), trigger.get("flowId"), trigger.get("executionId"), path)) { |
| 75 | + throw new IllegalArgumentException("Unable to read the file '" + path + "' as it didn't belong to the parent execution"); |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + private String checkIfFileFromAllowedNamespaceAndReturnIt(EvaluationContext context, URI path, String tenantId, String fromNamespace) { |
| 83 | + // Extract namespace from the path, it should be of the form: kestra:///({tenantId}/){namespace}/{flowId}/executions/{executionId}/tasks/{taskId}/{taskRunId}/{fileName}' |
| 84 | + // To extract the namespace, we must do it step by step as tenantId, namespace and taskId can contain the words 'executions' and 'tasks' |
| 85 | + String namespace = path.toString().substring(KESTRA_SCHEME.length()); |
| 86 | + if (!EXECUTION_FILE.matcher(namespace).matches()) { |
| 87 | + throw new IllegalArgumentException("Unable to read the file '" + path + "' as it is not an execution file"); |
| 88 | + } |
| 89 | + |
| 90 | + // 1. remove the tenantId if existing |
| 91 | + if (tenantId != null) { |
| 92 | + namespace = namespace.substring(tenantId.length() + 1); |
| 93 | + } |
| 94 | + // 2. remove everything after tasks |
| 95 | + namespace = namespace.substring(0, namespace.lastIndexOf("/tasks/")); |
| 96 | + // 3. remove everything after executions |
| 97 | + namespace = namespace.substring(0, namespace.lastIndexOf("/executions/")); |
| 98 | + // 4. remove the flowId |
| 99 | + namespace = namespace.substring(0, namespace.lastIndexOf('/')); |
| 100 | + // 5. replace '/' with '.' |
| 101 | + namespace = namespace.replace("/", "."); |
| 102 | + |
| 103 | + flowService.checkAllowedNamespace(tenantId, namespace, tenantId, fromNamespace); |
| 104 | + |
| 105 | + return namespace; |
| 106 | + } |
| 107 | +} |
0 commit comments