fix(scripting): #971 the script sandbox is an allow-list — the K-1 bypasses (CreateDelegate, Expressions, XML/Tar I/O, X509Store, Timers/BackgroundWorker/AsParallel, Serilog.Log) are refused

ForbiddenTypeAnalyzer inverted from a BCL deny-list to an allow-list of types/members (ctx API, value/time/maths
types, collections, LINQ-to-objects, string/Regex/formatting; System.Type and CancellationToken with member
allow-lists). Every named type, expression type and used member (incl. return type and generic args) is checked;
__makeref/__arglist, lock and extern local functions refused syntactically. ScriptSandbox references only eight BCL
assemblies (defence in depth). The Monaco editor and the publish gate share both, so parity holds.

Tests: theory over the K-1 snippets (publish gate + analyzer alone against the full host), every-public-type
enumeration, shipped-sample + idiom regression, editor/publish parity. Differential over every script-shaped literal
in src/tests: all 511 previously-compiling scripts still compile (one expression-tree test intentionally refused).
PE cache signing by the host (option 4) deferred as a follow-up.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-03 10:09:39 -05:00
parent 65fd71de7a
commit c00ebf1320
13 changed files with 1149 additions and 477 deletions
@@ -43,10 +43,28 @@ public sealed class ScriptStackGuardInjectionTests
/// <summary>A lambda converted to an expression tree is left as it is (a block body is illegal there and an
/// expression tree runs no script frame); rewriting it would turn a compiling script into an emit failure.</summary>
/// <remarks>
/// Since lmxopcua#971 a script cannot write an expression tree at all (<c>System.Linq.Expressions</c> is off the
/// sandbox allow-list and out of the reference set — it was a by-name reflection escape), so the injector's
/// expression-tree carve-out is exercised directly on a compilation that references it.
/// </remarks>
[Fact]
public void An_expression_tree_lambda_is_not_rewritten()
{
Run("System.Linq.Expressions.Expression<System.Func<int, int>> e = x => x + 1; return (int)e.Body.NodeType;")
.ShouldBe((int)System.Linq.Expressions.ExpressionType.Add);
Should.Throw<CompilationErrorException>(() => ScriptEvaluator<FakeScriptContext, object?>.Compile(
"System.Linq.Expressions.Expression<System.Func<int, int>> e = x => x + 1; return 0;"));
var tree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(
"class C { static object Run() { System.Linq.Expressions.Expression<System.Func<int, int>> e = x => x + 1; return e; } }",
cancellationToken: TestContext.Current.CancellationToken);
var tpa = ((string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES")!).Split(Path.PathSeparator);
var compilation = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(
"ExprTree", [tree], tpa.Select(p => Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(p)),
new Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary));
var rewritten = ScriptStackGuardInjector.Inject(compilation, tree).GetRoot(TestContext.Current.CancellationToken).ToFullString();
rewritten.ShouldContain("e = x => x + 1;"); // the expression lambda is untouched
rewritten.Split("ScriptStackGuard.Check()").Length.ShouldBe(2); // only Run's body got the check
}
}