| | | 1 | | using Elsa.Expressions.Models; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Expressions.JavaScript.Extensions; |
| | | 4 | | using Elsa.Expressions.JavaScript.Notifications; |
| | | 5 | | using Elsa.Expressions.JavaScript.Options; |
| | | 6 | | using Elsa.Mediator.Contracts; |
| | | 7 | | using Humanizer; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Jint; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Expressions.JavaScript.Handlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A handler that configures the Jint engine with workflow input and output accessors. |
| | | 16 | | /// </summary> |
| | | 17 | | [UsedImplicitly] |
| | 909 | 18 | | public class ConfigureEngineWithVariablesAndInputOutputAccessors(IOptions<JintOptions> options) : INotificationHandler<E |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Identifiers whose presence means the expression can reach a global it never names, so that no accessor may |
| | | 22 | | /// be filtered out. See the remarks on <see cref="GetReferencedGlobalsFilter"/>. |
| | | 23 | | /// </summary> |
| | 3 | 24 | | private static readonly string[] DynamicCodeSignals = ["eval", "Function", "globalThis"]; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken) |
| | | 28 | | { |
| | 285 | 29 | | if (options.Value.DisableWrappers) |
| | 0 | 30 | | return; |
| | | 31 | | |
| | 285 | 32 | | var engine = notification.Engine; |
| | 285 | 33 | | var context = notification.Context; |
| | 285 | 34 | | var referencedGlobals = GetReferencedGlobalsFilter(notification); |
| | | 35 | | |
| | | 36 | | // The order of the next 3 lines is important. |
| | 285 | 37 | | CreateVariableAccessors(engine, context, referencedGlobals); |
| | 285 | 38 | | CreateWorkflowInputAccessors(engine, context, referencedGlobals); |
| | 285 | 39 | | await CreateActivityOutputAccessorsAsync(engine, context, referencedGlobals); |
| | 285 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Returns the identifiers the expression references, to be used as a filter over the accessors that would |
| | | 44 | | /// otherwise all be registered, or <see langword="null"/> when no filtering may be applied and every accessor |
| | | 45 | | /// has to be registered the way it always was. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <remarks> |
| | | 48 | | /// <para> |
| | | 49 | | /// The filter is sound for an expression that names an accessor the way one is meant to be named, as an |
| | | 50 | | /// identifier. It is not sound for one that builds or reaches a name at run time, which leaves no identifier |
| | | 51 | | /// for the parser to report. Four such forms are detectable and each turns the filter off: |
| | | 52 | | /// </para> |
| | | 53 | | /// <list type="bullet"> |
| | | 54 | | /// <item><description>a direct <c>eval</c> call, reported as <see cref="Jint.ReferencedGlobals.HasDirectEvalCall"/> |
| | | 55 | | /// <item><description>an indirect <c>eval</c> call, which is <em>not</em> flagged as a direct one — the identifier |
| | | 56 | | /// <item><description>the <c>Function</c> constructor, likewise signalled by the identifier <c>Function</c>, and wh |
| | | 57 | | /// <item><description>a reference to <c>globalThis</c>, which reaches a global without naming it.</description></it |
| | | 58 | | /// </list> |
| | | 59 | | /// <para> |
| | | 60 | | /// What stays undetectable is reaching the global object without naming it at all: a sloppy-mode top-level |
| | | 61 | | /// <c>this</c>, or <c>[].constructor.constructor(…)</c>. An expression written that way loses the generated |
| | | 62 | | /// accessor, not the data: <c>getVariable(name)</c>, <c>getInput(name)</c> and |
| | | 63 | | /// <c>getOutputFrom(activityId, outputName)</c> are always registered and reach the same values. |
| | | 64 | | /// </para> |
| | | 65 | | /// </remarks> |
| | | 66 | | private static ReferencedGlobals? GetReferencedGlobalsFilter(EvaluatingJavaScript notification) |
| | | 67 | | { |
| | 285 | 68 | | var referencedGlobals = notification.ReferencedGlobals; |
| | | 69 | | |
| | 285 | 70 | | if (referencedGlobals is null) |
| | 0 | 71 | | return null; |
| | | 72 | | |
| | 285 | 73 | | if (referencedGlobals.HasDirectEvalCall) |
| | 1 | 74 | | return null; |
| | | 75 | | |
| | 2257 | 76 | | foreach (var dynamicCodeSignal in DynamicCodeSignals) |
| | | 77 | | { |
| | 849 | 78 | | if (referencedGlobals.Contains(dynamicCodeSignal)) |
| | 9 | 79 | | return null; |
| | | 80 | | } |
| | | 81 | | |
| | 275 | 82 | | return referencedGlobals; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private void CreateVariableAccessors(Engine engine, ExpressionExecutionContext context, ReferencedGlobals? reference |
| | | 86 | | { |
| | 285 | 87 | | var variableNames = context.GetVariableNamesInScope().FilterInvalidVariableNames().ToList(); |
| | | 88 | | |
| | 886 | 89 | | foreach (var variableName in variableNames) |
| | | 90 | | { |
| | 158 | 91 | | var pascalName = variableName.Pascalize(); |
| | 158 | 92 | | var getterName = $"get{pascalName}"; |
| | 158 | 93 | | var setterName = $"set{pascalName}"; |
| | | 94 | | |
| | 158 | 95 | | if (IsReferenced(referencedGlobals, getterName)) |
| | 79 | 96 | | engine.SetValue(getterName, (Func<object?>)(() => context.GetVariableInScope(variableName))); |
| | | 97 | | |
| | 158 | 98 | | if (IsReferenced(referencedGlobals, setterName)) |
| | 9 | 99 | | engine.SetValue(setterName, (Action<object?>)(value => |
| | 9 | 100 | | { |
| | 4 | 101 | | engine.SyncVariablesContainer(options, variableName, value); |
| | 4 | 102 | | context.SetVariableInScope(variableName, value); |
| | 13 | 103 | | })); |
| | | 104 | | } |
| | 285 | 105 | | } |
| | | 106 | | |
| | | 107 | | private void CreateWorkflowInputAccessors(Engine engine, ExpressionExecutionContext context, ReferencedGlobals? refe |
| | | 108 | | { |
| | | 109 | | // Create workflow input accessors - only if the current activity is not part of a composite activity definition |
| | | 110 | | // Otherwise, the workflow input accessors will hide the composite activity input accessors which rely on variab |
| | 285 | 111 | | if (context.IsContainedWithinCompositeActivity()) |
| | 8 | 112 | | return; |
| | | 113 | | |
| | 339 | 114 | | var inputs = context.GetWorkflowInputs().Where(x => x.Name.IsValidVariableName()).ToDictionary(x => x.Name, Stri |
| | | 115 | | |
| | 277 | 116 | | if (!context.TryGetWorkflowExecutionContext(out var workflowExecutionContext)) |
| | 123 | 117 | | return; |
| | | 118 | | |
| | 154 | 119 | | var inputDefinitions = workflowExecutionContext.Workflow.Inputs; |
| | | 120 | | |
| | 318 | 121 | | foreach (var inputDefinition in inputDefinitions) |
| | | 122 | | { |
| | 5 | 123 | | var accessorName = $"get{inputDefinition.Name}"; |
| | | 124 | | |
| | 5 | 125 | | if (!IsReferenced(referencedGlobals, accessorName)) |
| | | 126 | | continue; |
| | | 127 | | |
| | 4 | 128 | | var input = inputs.GetValueOrDefault(inputDefinition.Name); |
| | 8 | 129 | | engine.SetValue(accessorName, (Func<object?>)(() => input?.Value)); |
| | | 130 | | } |
| | 154 | 131 | | } |
| | | 132 | | |
| | | 133 | | private static async Task CreateActivityOutputAccessorsAsync(Engine engine, ExpressionExecutionContext context, Refe |
| | | 134 | | { |
| | | 135 | | // Naming the accessors means walking every node of the enclosing container and resolving each one against |
| | | 136 | | // the activity registry, so this is the one piece of engine setup whose cost grows with the size of the |
| | | 137 | | // workflow rather than the size of the expression. Every name it can produce is get{Output}From{Activity}, |
| | | 138 | | // so an expression referencing no identifier of that shape needs none of the walk. |
| | 285 | 139 | | if (referencedGlobals is not null && !referencedGlobals.Any(IsActivityOutputAccessorName)) |
| | 272 | 140 | | return; |
| | | 141 | | |
| | 13 | 142 | | var activityOutputs = context.GetActivityOutputs(); |
| | | 143 | | |
| | 28 | 144 | | await foreach (var activityOutput in activityOutputs) |
| | 4 | 145 | | foreach (var outputName in activityOutput.OutputNames.FilterInvalidVariableNames()) |
| | | 146 | | { |
| | 1 | 147 | | var accessorName = $"get{outputName}From{activityOutput.ActivityName.Pascalize()}"; |
| | | 148 | | |
| | 1 | 149 | | if (IsReferenced(referencedGlobals, accessorName)) |
| | 2 | 150 | | engine.SetValue(accessorName, (Func<object?>)(() => context.GetOutput(activityOutput.ActivityId, outputN |
| | | 151 | | } |
| | 285 | 152 | | } |
| | | 153 | | |
| | 285 | 154 | | private static bool IsActivityOutputAccessorName(string name) => name.StartsWith("get", StringComparison.Ordinal) && |
| | | 155 | | |
| | 322 | 156 | | private static bool IsReferenced(ReferencedGlobals? referencedGlobals, string name) => referencedGlobals is null || |
| | | 157 | | } |