{"record":{"id":"b601a7401dad0263","repo":"elsa-workflows/elsa-core","slug":"python-net-workflow-expression-execution-is-disabled-set","errorCode":null,"errorMessage":"Python.NET workflow expression execution is disabled. Set PythonOptions.AllowHostCodeExecution to true only for trusted workflow authors; Python.NET is not a sandbox.","messagePattern":"Python\\.NET workflow expression execution is disabled\\. Set PythonOptions\\.AllowHostCodeExecution to true only for trusted workflow authors; Python\\.NET is not a sandbox\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Expressions.Python/Services/PythonNetPythonEvaluator.cs","lineNumber":43,"sourceCode":"    /// </summary>\n    public PythonNetPythonEvaluator(INotificationSender notificationSender) : this(notificationSender, Microsoft.Extensions.Options.Options.Create(new PythonOptions()))\n    {\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"PythonNetPythonEvaluator\"/> class.\n    /// </summary>\n    public PythonNetPythonEvaluator(INotificationSender notificationSender, IOptions<PythonOptions> options)\n    {\n        _notificationSender = notificationSender;\n        _options = options;\n    }\n\n    /// <inheritdoc />\n    public async Task<object?> EvaluateAsync(string expression, Type returnType, ExpressionExecutionContext context, CancellationToken cancellationToken = default)\n    {\n        if (!_options.Value.AllowHostCodeExecution)\n            throw new InvalidOperationException(\"Python.NET workflow expression execution is disabled. Set PythonOptions.AllowHostCodeExecution to true only for trusted workflow authors; Python.NET is not a sandbox.\");\n\n        using var gil = Py.GIL();\n        using var scope = Py.CreateScope();\n        var notification = new EvaluatingPython(scope, context);\n        \n        scope.Import(\"System\");\n\n        // Add globals.\n        scope.Set(\"execution_context\", new ExecutionContextProxy(context));\n        scope.Set(\"input\", new InputProxy(context));\n        scope.Set(\"output\", new OutputProxy(context));\n        scope.Set(\"outcome\", new OutcomeProxy(context));\n\n        await _notificationSender.SendAsync(notification, cancellationToken);\n        var wrappedScript = WrapInExecuteScriptFunction(expression);\n        scope.Exec(wrappedScript);\n        var result = scope.Get<object>(ReturnVarName);\n        return result.ConvertTo(returnType);","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Expressions.Python/Services/PythonNetPythonEvaluator.cs#L25-L61","documentation":"The Python.NET evaluator refuses to execute workflow expressions unless PythonOptions.AllowHostCodeExecution is explicitly true, because Python.NET gives scripts full host access and is not a sandbox. The throw is a security guard added by Elsa to prevent untrusted workflow authors from executing arbitrary host code.","triggerScenarios":"Evaluating any Python expression via PythonNetPythonEvaluator.EvaluateAsync while PythonOptions.AllowHostCodeExecution is false/unset (the safe default).","commonSituations":"Deploying an app that uses Python expressions without opting into host code execution, or upgrading Elsa to a version that introduced this opt-in flag and existing workflows stop running.","solutions":["Set PythonOptions.AllowHostCodeExecution to true only if workflow authors are fully trusted: builder.Services.Configure<PythonOptions>(o => o.AllowHostCodeExecution = true;)","If workflows must not run privileged code, rewrite the expressions in a sandboxed language (JavaScript with sandboxing or C#) instead of enabling the flag","Confirm the configuration binding actually reaches the options (e.g. configuration section key casing) so the flag is true at evaluation time"],"exampleFix":"// before\nbuilder.Services.AddPython();\n// after (trusted authors only)\nbuilder.Services.AddPython();\nbuilder.Services.Configure<PythonOptions>(o => o.AllowHostCodeExecution = true);","handlingStrategy":"validation","validationCode":"// at startup, fail fast if the flag is required but unset\nif (usesPythonExpressions && !pythonOptions.AllowHostCodeExecution)\n    throw new InvalidOperationException(\"Enable PythonOptions.AllowHostCodeExecution for trusted authors.\");","typeGuard":null,"tryCatchPattern":"try { await evaluator.EvaluateAsync(expr, typeof(object), ctx, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AllowHostCodeExecution\"))\n{ logger.LogWarning(\"Python execution disabled; reconfigure options.\"); }","preventionTips":["Set AllowHostCodeExecution = true only in trusted-author environments","Document the security implications for operators","Prefer sandboxed JS/C# expressions for untrusted authors"],"tags":["python","security","configuration","feature-flag"],"backgroundTag":"feature-not-enabled","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}